Hi I was wondering if someone could tell me what I am doing wrong. I am trying to call the age function from the person class within the PEmployee class. but the name does not display and the age states 0. thank you for any insight you may give me. Have a nice day, C.D.

#ifndef Lab10

#define Lab10

#include <string>

using namespace std ;

class person

{

public:

    /**
    Constructs a person with no name and no age.
    */

    person()    ;


    /**
    Constructs a person with a given name and age.
    @param pname is the name.
    @param page is the age.
    */

    person( string pname, int page ) ;


    /**
    Gets the name of the person and returns the name.
    */
    string get_name() const ;


    /**
    Gets the age of the person and returns the age.
    */

    int get_age() const ;


    /**
    Gets a name for person from the user.
    */

    void read_name() ;


    /**
    Gets an age for person from the user.
    */

    void read_age() ;


    /**
    Displays the name of person.
    */

    void display_name() ;


    /**
    Displays the age of person with the name.
    */

    void display_age() ;


private:

    string name ;

    int age ;

} ;

#endif


#ifndef PEMPLOYEE_H

#define PEMPLOYEE_H

#include <string>

#include "Lab10.h"

using namespace std ;

    class PEmployee

   {

        public:

            /**
            Constructs an PEmployee with empty name and no salary.
            */  

            PEmployee() ;


            /** 
            Constructs a PEmployee with a set name and salary
            @param employee_name     the PEmployee name.
            @param initial_salary the PEmployee salary.
            */

         PEmployee(string employee_name, double initial_salary) ;


            /**
            Sets the salary of the PEmployee.
            */

         void set_salary () const ;


            /** 
            Sets the salary of this employee.
            @param new_salary the new salary value
            */

         void set_salary(double new_salary) const ;


            /**
            Gets the name of this employee.
            @return the employee name
            */

         string get_name() const ;



            /**
            Gets the PEmployees name from the user
            */

            void read_salary() ;


            /**
            Gets the age of person from person Class
            */

            void get_data() ;


            /**
            Gets the PEmloyee salary.
            @return the employee salary
            */

            double get_salary() const ;


            /**
            Displays the age of PEmployee from person class.
            */

            void display_age() ;


        private:

        person person_data ;

        double salary ;

        string employee_name ;

 } ;

#endif

#include <iostream>

#include "Lab10.h"

#include <string>

using namespace std ;

    person :: person()
        {
             age = 0 ;

        }

     person :: person(string pname, int page)

     {

         name = pname ;

         age = page ;

     }

    int person :: get_age() const

    {

        return age ;

    }

     void person :: read_age()

     {

      cout << "Please enter the age of the person "   ;

          cin >> age ;

     }

     void person :: read_name()

     {

         cout << "Please enter the name of the person " ;

         cin >> name ;

     }

     void person :: display_name()

     {

         cout << "The name of the person is " << name ;

     }

     void person :: display_age() 

     {

         cout << "The age of " << name << "person is " << age ;

     }

     string person :: get_name() const 

     {

         return name ;

     }




      #include <iostream>

#include <string>

#include "Lab10.h"

#include "PEmployee.h"

using namespace std ;

PEmployee :: PEmployee()

    {

        salary = 0 ;

    }

PEmployee ::PEmployee(string employee_name, double initial_salary)

    {

        salary = initial_salary ;

        string name = employee_name ;

    }

void PEmployee :: set_salary(double new_salary) const

    {

        double salary = new_salary ;

    }

string PEmployee :: get_name() const

{

    return employee_name ;

}



    void PEmployee :: read_salary() 

    {

            cout << "Please enter the salary for the person " ;

            cin >> salary ;

    }

    double PEmployee :: get_salary()    const

    {

        return salary ;

    }

    void PEmployee :: display_age() 

     {

         cout << "The age of " << employee_name << " is " << person_data << " and thier salary is $ " << salary << "\n" ;

     }



     #include <iostream>

#include <string>

#include "Lab10.h"

#include "PEmployee.h"

using namespace std ;

int main()

{
        cin.clear() ; // clears the buffer.

        person next ;

        next.read_name() ; // Calls read_name to get name of person from user.

        next.read_age() ;   // Calls read_age to get age of person from user.   

        next.get_name() ; // Calls get_name to return the name of person.


    PEmployee enter ;

        enter.read_salary() ; // calls read_salary to get salary of PEmployee.

        enter.get_salary() ;     // returns salary of PEmployee.

        enter.display_age() ; // Displays the name and age of person and salary of PEmployee


            system("PAUSE") ;

            return 0    ;

line 348: that is not related to PEmployee class -- it's completly separate object. What you want to do is to use the person member of PEmployee.

delete lines 350-354 and use person_data instead of next.

Hi I changed the code but I do not think I did it properly because it still does not work.

void PEmployee :: display_age() 

     {

         int person_data = 0 ;

         cout << "The age of " << employee_name << " is " << person_data << " and thier salary is $ " << salary << "\n" ;

     }



     #include <iostream>

#include <string>

#include "Lab10.h"

#include "PEmployee.h"

using namespace std ;

int main()

{
        cin.clear() ; // clears the buffer.

    person person_data ;

    person_data.read_name() ; // Asks user for name of person.

    person_data.read_age() ;  // Asks user for age of person.

    person_data.get_name() ; // Returns the name of person



    PEmployee enter ;

        enter.read_salary() ; // Calls read_salary to get salary of PEmployee.

        enter.get_salary() ;     // Returns salary of PEmployee.

        enter.display_age() ; // Displays the name and age of person and salary of PEmployee.


            system("PAUSE") ;

            return 0    ;

}

The reason why you keep getting zero is because the person_data object inside the PEmployee class is never initialize. You need to initialize the variables of the person_data object which are "age" and "name". Then you can use the object person_data to call the respective function to get the values of variables. Here is an example on how you can call the function.

person_data.somefunction();

Also, a few pretty basic coding suggestions ...

int main()
{
    //cin.clear() ; // clears the buffer// <- NO ... it clears cin (error) flags that were set on some cin input //

    // ...

    //system("PAUSE") ; // Not portable code //
    cout << "\nPress 'Enter' to continue/exit ... " << flush;
    string dummy;
    cin.clear(); // may need this if cin flags were set above and not yet cleared //
    getline( cin, dummy );
    return 0 ;
}

Also ...

Good to get into habit of using initialization list ...

// rather than code ... (in constructors)
person :: person(string pname, int page)
{
name = pname ;
age = page ;
}

Use this instead ...

// code like this ...
person :: person( string pname, int page )
: name( pname ), age ( page ) {}

Note: the order of assignments must be the same as the top down order that the data members appeared in the class

Recall that in a class the data is ALL supposed to have initial values

The default for a string type is ""

But you must supply initial values for types like ...
int
float
double
??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.