CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28

Thread: how to return an other object's value

  1. #1
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    how to return an other object's value

    How can I return other object's value in a function(see code and comments)?

    Person.h
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Person{
    
    public:
    	Person();
    	Person(string pname, int page);
    	void get_name() const;
    	void get_age() const;
    private:
    	string name;
    	int age; // 0 if unknown
    
    };
    PEmployee.h
    Code:
    #include "Person.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    class PEmployee
    {
    public:
    	PEmployee();
    	PEmployee(string employee_name, double initial_salary);
    	void set_salary(double new_salary);
    	double get_salary() const;
    	string get_name() const;
    private:
    	Person person_data;
    	double salary;
    };
    Person.cpp
    Code:
    #include "Person.h"
    
    using namespace std;
    
    
    Person::Person(){
    
    	name = "noname";
    	age = 0;
    
    }
    
    Person::Person(string pname, int page){
    
    	name = pname;
    	age = page;
    
    }
    
    void Person::get_name() const{
    
    	cout << name << '\n';
    
    }
    
    void Person::get_age() const{
    
    	cout << age << '\n';
    
    }
    PEmployee.cpp
    Code:
    #include "PEmployee.h"
    
    using namespace std;
    
    PEmployee::PEmployee(){
    
    	
    }
    
    PEmployee::PEmployee(string employee_name, double initial_salary){
    
    	Person(employee_name, 0);
    	salary = initial_salary;
    
    }
    
    
    void PEmployee::set_salary(double new_salary){
    	
    	salary = new_salary;
    }
    
    
    double PEmployee::get_salary() const{
    	
    	return salary;
    
    }
    
    string PEmployee::get_name() const{
    
    	return Person::get_name();  //?? how to call other object's non-static function
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,588

    Re: how to return an other object's value

    You'd need an instance of the other object, but shouldn't PEmployee be derived from Person in this case?

  3. #3
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: how to return an other object's value

    I think no.

    Code:
    Exercise P5.2. Implement a class PEmployee that is just like the Employee class except
    that it stores an object of type Person as developed in Exercise P5.1.
    class PEmployee
    {
    public:
    PEmployee();
    PEmployee(string employee_name, double initial_salary);
    void set_salary(double new_salary);
    double get_salary() const;
    string get_name() const;
    private:
    Person person_data;
    double salary;
    };

  4. #4
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: how to return an other object's value

    You'd need an instance of the other object, but shouldn't PEmployee be derived from Person in this case?
    So how should the code look like ?

  5. #5
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: how to return an other object's value

    I think this is not correct also?
    Code:
    string PEmployee::get_name() const{
    
    return person_data.name; 
    
    }

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,588

    Re: how to return an other object's value

    Quote Originally Posted by flex567 View Post
    I think this is not correct also?
    Code:
    string PEmployee::get_name() const{
    
    return person_data.name; 
    
    }
    I didn't notice your Person member. That's close. person_data is the instance of your Person object. Use it, but use its methods. So return person_data.get_name();

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,145

    Re: how to return an other object's value

    Code:
    PEmployee::PEmployee(string employee_name, double initial_salary){
    
    	Person(employee_name, 0);
    	salary = initial_salary;
    
    }
    Your first issue is here as you are creating an instance of the class Person but doing nothing with it! Where are you setting the contents of person_data?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.3)

  8. #8
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: how to return an other object's value

    return person_data.get_name();
    I don't think this is correct because get_name() returns nothing.

    Your first issue is here as you are creating an instance of the class Person but doing nothing with it! Where are you setting the contents of person_data?
    I corrected the code in constructors:
    Code:
    PEmployee::PEmployee(){
            
    	
            person_data = Person ob1;
    	salary = 0;
    	
    }
    
    PEmployee::PEmployee(string employee_name, double initial_salary){
    
    	person_data = Person ob1(employee_name, 0);
    	salary = initial_salary;
    
    }
    Last edited by flex567; November 8th, 2014 at 10:33 AM.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: how to return an other object's value

    Quote Originally Posted by flex567
    I don't think this is correct because get_name() returns nothing.
    Why does get_name() have a return type of void in the first place? By its name one would expect it to return a value, either by actually returning a value, or via an out parameter.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: how to return an other object's value

    this is why

    Code:
    Exercise P5.1. Implement all member functions of the following class:
    class Person
    {
    public:
    Person();
    Person(string pname, int page);
    void get_name() const;
    void get_age() const;
    private:
    string name;
    int age; // 0 if unknown
    };

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: how to return an other object's value

    I see. What is the get_name member function supposed to do?

    EDIT:
    Oh, nevermind. I see from your original post that it presumably is meant to print the name to standard output, in which case it is poorly named, and the unnecessary coupling of the class to standard output is poor design.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: how to return an other object's value

    Code is in the #1 post of this thread.

    Code:
    void Person::get_name() const{
    
    	cout << name << '\n';
    
    }

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: how to return an other object's value

    Well, as it stands, I think your task cannot be done. Let's look at the two class definitions:
    Code:
    class Person{
    
    public:
    	Person();
    	Person(string pname, int page);
    	void get_name() const;
    	void get_age() const;
    private:
    	string name;
    	int age; // 0 if unknown
    
    };
    Code:
    class PEmployee
    {
    public:
    	PEmployee();
    	PEmployee(string employee_name, double initial_salary);
    	void set_salary(double new_salary);
    	double get_salary() const;
    	string get_name() const;
    private:
    	Person person_data;
    	double salary;
    };
    The Person class' name member variable is private. It has a get_name member function, but this only prints the name to standard output. There is no way to obtain the name of the Person object. Now, to implement PEmployee's get_name function, we need to somehow obtain the name of the Person member object named person_data. We can call person_data.get_name(), but that is futile since it merely prints to standard output. We cannot access person_data.name because it is private. Therefore, you're stuck.

    I suggest that you check with your teacher. Chances are, in the Person class, the void return type for get_name should have been string.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #14
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: how to return an other object's value

    This is exually an example from this book: http://www.amazon.com/Big-2nd-Editio.../dp/0470383283

    Ok, So i have to change
    Code:
     void Person::get_name() const{
    
    	cout << name << '\n';
    
    }
    to


    Code:
     string Person::get_name(){ return name; }

  15. #15
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: how to return an other object's value

    Yes, but not quite. It should be:
    Code:
    string Person::get_name() const {
        return name;
    }
    Also, remember that using directives like using namespace std; should not be placed at file scope in header files. You should fully qualify those names in the header file.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)