简体   繁体   中英

Cannot inherit data from parent class to child class in C++

I am trying to inherit data from two parent classes of Employee and Student to a child class of Manager .I have created set and get functions of each class and i have created a show function in child class which will also show data of both parent classes.But when i make objects and call functions of set values and then show data, only the data of child class is shown. Can anyone tell my why is that and how do i solve it? Thanks for any help.

Code is below:

class Employee{
protected:
    string name;
    int number;
public:
    Employee(){
        name = "";
        number = 0;
    }
    void set_name(string a){
        name = a;
    }
    void set_number(int a){
        number = a;
    }
    string get_name(){
        return name;
    }
    int get_number(){
        return number;
    }
};

class Student{
    protected:
        string school;
        string degree;
    public:
        Student(){
            school = "";
            degree = "";
        }
        void set_school(string a){
            school = a;
        }
        void set_degree(string a){
            degree = a;
        }
        string get_school(){
            return school;
        }
        string get_degree(){
            return degree;
        }
};

class Manager:protected Employee, protected Student{
    protected:
        string title;
        int dues;
    public:
        Manager(){
            title = "";
            dues = 0;
        }
        void set_title(string a){
            title = a;
        }
        void set_dues(int a){
            dues = a;
        }
        string get_title(){
            return title;
        }
        int get_dues(){
            return dues;
        }
        void show_data(){
            cout << Employee::get_name();
            cout << Employee::get_number() << endl;
            cout << Student::get_school() << endl;
            cout << Student::get_degree() << endl;
            cout << get_title() << endl;
            cout << get_dues() << endl;
        }
};
int main(){
    Employee emp;
    Student stu;
    Manager man;
    emp.set_name("Fahad");
    emp.set_number(10);
    stu.set_school("COMSAT");
    stu.set_degree("BSCS");
    man.set_title("Manager Title");
    man.set_dues(100);
    man.show_data();
    return 1;
}

You have 3 different objects, each contains its own data!

For example, your manager class contains all attributes from manager, employee and student. In your main function:

Employee emp;
Student stu;
Manager man;
emp.set_name("Fahad");
emp.set_number(10);

stu.set_school("COMSAT");
stu.set_degree("BSCS");

man.set_title("Manager Title");
man.set_dues(100);
man.show_data();

you only set some of the attributes of your objects. For manager, you only set title and dues. If you want to set also the name, you have to do it!

BTW: there is no need to use Student::get_school() to access the members of parent classes if you have not used the same attribute name multiple times or inherit multiple times from the same class.

I believe you want to do the following:

class Manager:public Employee, public Student{ ... };

int main(){
    Manager man;
    man.set_name("Fahad");
    man.set_number(10);
    man.set_school("COMSAT");
    man.set_degree("BSCS");
    man.set_title("Manager Title");
    man.set_dues(100);
    man.show_data();
    return 1;
}

If you change to public for deriving from your parent classes, you can directly access the getter/setter functions from the object as you can see. As you have already getter and setter functions, it is a good idea to make the member vars now private .

BTW: The return value of main is typically 0 to tell that there is no error. If you want to return with "no error", you can simply omit the return statement. main() will than return 0 by default.

The following lines create three objects.

Employee emp;
Student stu;
Manager man;

The emp object is not related to the Employee sub-object of man and the stu object is not related to the Student sub-object of man .

The lines

emp.set_name("Fahad");
emp.set_number(10);

change the state of the independent emp object. They don't change the state of the Employee sub-object of man .

You need to use:

int main(){
    // Employee emp;   Not needed
    // Student stu;    Not needed

    Manager man;
    man.set_name("Fahad");
    man.set_number(10);
    man.set_school("COMSAT");
    man.set_degree("BSCS");
    man.set_title("Manager Title");
    man.set_dues(100);
    man.show_data();
    return 1;
}

to see the values set on the same object. However, to do that you need to change the inheritances of Manager . Use

class Manager : public Employee, public Student {
   ...
};

Your code does not work as expected because:

  1. You allocate separate objects/instances of each Employee, Student and Manager, eg emp, stud and man and set the data on each separate instance (for example, the emp.set_name() will only affect the emp object, not the man object).
  2. You print the data member of the man object which does not include the data set in emp and man.

I do not know what the ideas behind your code design is, I do recommend to leave out multiple inheritance as it quickly leads to unmaintainable code. But to get your example code to work, do the following:

  1. Delete the allocation of emp and stu;
  2. change the emp and stu variable names in the setter calls to man.
  3. Change protected to public in the Manager declaration where Manager extends Student and Employee.

     class Manager:public Employee, public Student{ int main(){ Manager man; man.set_name("Fahad"); man.set_number(10); man.set_school("COMSAT"); man.set_degree("BSCS"); man.set_title("Manager Title"); man.set_dues(100); man.show_data(); return 1;

    }

And you will get the output I assume you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM