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

Thread: how to return an other object's value

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

    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
    The comments on the site speak for themselves!
    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)

  2. #17
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: how to return an other object's value

    I still get some errors in the counstructors:

    Code:
    	1	IntelliSense: type name is not allowed	
    	2	IntelliSense: expected a ';'	
    	3	IntelliSense: type name is not allowed	
    	4	IntelliSense: expected a ';'

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

    Re: how to return an other object's value

    The comments on the site speak for themselves!
    there are a lot of positive comments as well.

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

    Re: how to return an other object's value

    What is your current code? Also, compile and show the compile errors, not just the errors that are reported by intellisense.
    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

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

    Re: how to return an other object's value

    Current code

    Main
    Code:
    #include <iostream>
    #include "PEmployee.h"
    #include "Person.h"
    
    using namespace std;
    
    int main(){
    
    	PEmployee one;
    	PEmployee two;
    
    	PEmployee three("James", 1000);
    	three.set_salary(1200);
    	
    	cout << three.get_name() << "  " << three.get_salary;
    
    	return 0;
    }
    Person.h
    Code:
    #include <iostream>
    #include <string>
    
    class Person{
    
    public:
    	Person();
    	Person(string pname, int page);
    	string 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>
    
    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;
    
    }
    
    string Person::get_name() const{
    
    	return name;
    
    }
    
    void Person::get_age() const{
    
    	cout << age << '\n';
    
    }
    PEmployee.cpp
    Code:
    #include "PEmployee.h"
    
    using namespace std;
    
    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;
    
    }
    
    
    void PEmployee::set_salary(double new_salary){
    	
    	salary = new_salary;
    }
    
    
    double PEmployee::get_salary() const{
    	
    	return salary;
    
    }
    
    string PEmployee::get_name() const{
    
    	return person_data.get_name();  //?? how to call other object's non-static function
    }
    Last edited by flex567; November 8th, 2014 at 12:57 PM.

  6. #21
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: how to return an other object's value

    Errors
    Code:
    Error	1	error C2061: syntax error : identifier 'string'	c:\users\seba\desktop\seba-docs-d\seba_programming\programiranje_visual_studio_2013\big_cpp_exercise p5_2_\person.h	8	1	Big_CPP_Exercise P5_2_
    Code:
    Error	2	error C2535: 'Person::Person(void)' : member function already defined or declared	c:\users\seba\desktop\seba-docs-d\seba_programming\programiranje_visual_studio_2013\big_cpp_exercise p5_2_\person.h	8	1	Big_CPP_Exercise P5_2_
    Code:
    Error	3	error C2146: syntax error : missing ';' before identifier 'get_name'	c:\users\seba\desktop\seba-docs-d\seba_programming\programiranje_visual_studio_2013\big_cpp_exercise p5_2_\person.h	9	1	Big_CPP_Exercise P5_2_
    Code:
    Error	4	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\seba\desktop\seba-docs-d\seba_programming\programiranje_visual_studio_2013\big_cpp_exercise p5_2_\person.h	9	1	Big_CPP_Exercise P5_2_
    ...
    Last edited by flex567; November 8th, 2014 at 12:53 PM.

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

    Re: how to return an other object's value

    Okay, now that you have correctly removed the using directive from the header files, you must remember to fully qualify the names. For example, in Person.h, string should be std::string.

    (I think you accidentally pasted the contents of PEmployee.cpp as PEmployee.h so I cannot comment on PEmployee.h)
    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

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

    Re: how to return an other object's value

    ok but there are still errors.

    Code:
    Error	1	error C2275: 'Person' : illegal use of this type as an expression	c:\users\seba\desktop\seba-docs-d\seba_programming\programiranje_visual_studio_2013\big_cpp_exercise p5_2_\pemployee.cpp	7	1	Big_CPP_Exercise P5_2_
    Code:
    Error	2	error C2146: syntax error : missing ';' before identifier 'ob1'	c:\users\seba\desktop\seba-docs-d\seba_programming\programiranje_visual_studio_2013\big_cpp_exercise p5_2_\pemployee.cpp	7	1	Big_CPP_Exercise P5_2_
    Code:
    Error	3	error C2065: 'ob1' : undeclared identifier	c:\users\seba\desktop\seba-docs-d\seba_programming\programiranje_visual_studio_2013\big_cpp_exercise p5_2_\pemployee.cpp	7	1	Big_CPP_Exercise P5_2_
    Code:
    Error	4	error C2275: 'Person' : illegal use of this type as an expression	c:\users\seba\desktop\seba-docs-d\seba_programming\programiranje_visual_studio_2013\big_cpp_exercise p5_2_\pemployee.cpp	14	1	Big_CPP_Exercise P5_2_

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

    Re: how to return an other object's value

    This is wrong:
    Code:
    person_data = Person ob1;
    You can just remove that line.

    This is wrong:
    Code:
    PEmployee::PEmployee(string employee_name, double initial_salary){
    
    	person_data = Person ob1(employee_name, 0);
    	salary = initial_salary;
    
    }
    You should use the initialiser list instead:
    Code:
    PEmployee::PEmployee(string employee_name, double initial_salary) : person_data(employee_name, 0), salary(initial_salary) {}
    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. #25
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: how to return an other object's value

    What about the Constructor with no Arguments?

    Initialization without initialiser list would be something like that?:
    Code:
    PEmployee::PEmployee(string employee_name, double initial_salary) {
              Person person_data(employee_name, 0);
              salary = initial_salary;
    }
    Last edited by flex567; November 10th, 2014 at 08:03 AM.

  11. #26
    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
    there are a lot of positive comments as well.
    Based on this thread, I'd recommend a new book. The whole making a Person a member of PEmployee is flawed. An Employee is a Person and should be derived from it. As mentioned, a function designed to get a name shouldn't be outputting anything. You're learning a lot of bad habits.

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

    Re: how to return an other object's value

    I think it is a good book because guidance should not be 100% correct.

    An Employee is a Person and should be derived from it
    There is a chapter only about inheritance.

  13. #28
    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 it is a good book because guidance should not be 100% correct.

    There is a chapter only about inheritance.
    You lack the skill set to make that determination. A tutorial book should absolutely be 100% correct. This is worse than having a few typos. It's teaching you bad habits. The fact that you're here and as confused as you are should attest to that.

Page 2 of 2 FirstFirst 12

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)