Quick multiple inheritance question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hypnotik
    New Member
    • Jun 2007
    • 87

    Quick multiple inheritance question

    Hello everyone. I'm working on multiple inheritance program. I have a pretty specific problem that I'm not sure how to overcome. I'm going to try to explain this as simple as possible. There is a person class which has some info. A student class, which inherits the person info, and has some additional info. A worker class which has it's own info, and inherits the person info. A student_worker class which inherits from all of the above classes. Now the problem. When I go into the student worker, it prompts for the "person" info 2 times and I cannot figure out how to get around it. The program is posted below:

    Code:
    #include <iostream>
    #include <ctype.h>
    using namespace std;
    class Person
    {
    private:
    	char fname[10];
    	char lname[10];
    	char gender;
    	int age;
    	int ssn;
    public:
    	Person();
    	~Person(){}
    	void getinfo();
    };
    Person::Person()
    {
    	int i;
    	for (i=0;i<10;++i)
    	{
    		fname[i]=0;
    		lname[i]=0;
    	}
    	gender=0;
    	age=0;
    	ssn=0;
    }
    void Person::getinfo()
    {
    	cout<<"Enter the first name of your person: "<<endl;
    	cin>>fname;
    	cout<<"Enter the last name of your person: "<<endl;
    	cin>>lname;
    	cout<<"Enter the gender of your person: "<<endl;
    	cin>>gender;
    	gender=toupper(gender);
    	cout<<"Enter the age of your person: "<<endl;
    	cin>>age;
    	cout<<"Enter the social security number of your person: "<<endl;
    	cin>>ssn;
    }
    class Student:public Person
    {
    private:
    	float GPA;
    	char major[40];
    	int hours;
    	char school[40];
    public:
    	Student();
    	~Student(){};
    	void getinfo();
    };
    Student::Student()
    {
    	int i;
    	for (i=0;i<40;++i)
    	{
    		major[i]=0;
    		school[i]=0;
    	}
    	GPA=0.0;
    	hours=0;
    }
    void Student::getinfo()
    {
    	Person::getinfo();
    	cout<<"Enter the GPA of the student: "<<endl;
    	cin>>GPA;
    	cout<<"Enter the hours completed by the student: "<<endl;
    	cin>>hours;
    	cout<<"Enter the major of the student: "<<endl;
    	cin>>major;
    	cout<<"Enter the school the student attends: "<<endl;
    	cin>>school;
    }
    class Worker:public Person
    {
    private:
    	char occupation[20];
    	char company[20];
    	float salary;
    public:
    	Worker();
    	~Worker(){};
    	void getinfo();
    };
    Worker::Worker()
    {
    	int i;
    	for (i=0;i<20;++i)
    	{
    		occupation[i]=0;
    		company[i]=0;
    	}
    	salary=0.0;
    }
    void Worker::getinfo()
    {
    	Person::getinfo();
    	cout<<"Enter the occupation of the worker: "<<endl;
    	cin>>occupation;
    	cout<<"Enter the company: "<<endl;
    	cin>>company;
    	cout<<"Enter the salary of the worker: "<<endl;
    	cin>>salary;
    }
    class Student_Worker:public Worker,public Student
    {
    private:
    	char slip[10];
    public:
    	Student_Worker();
    	~Student_Worker(){};
    	void getinfo();
    };
    Student_Worker::Student_Worker()
    {
    	int i;
    	for (i=0;i<10;++i)
    	{
    		slip[i]=0;
    	}
    }
    void Student_Worker::getinfo()
    {
    	Student::getinfo();
    	Worker:: getinfo();
    }
    void main()
    {
    	Person P;
    	Student S;
    	Worker W;
    	Student_Worker SW;
    //	P.getinfo();
    //	S.getinfo();
    //	W.getinfo();
    	SW.getinfo();
    }
    Thanks for looking,

    J
  • svlsr2000
    Recognized Expert New Member
    • Feb 2007
    • 181

    #2
    Have a look at virtual inheritance and virtual class.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      What is meant is that you need a virtual base class.

      There's info on this in the "C++ Primer Plus" 5th Ed. by Stephen Prata

      Comment

      • Hypnotik
        New Member
        • Jun 2007
        • 87

        #4
        Originally posted by weaknessforcats
        What is meant is that you need a virtual base class.

        There's info on this in the "C++ Primer Plus" 5th Ed. by Stephen Prata

        Hey weakness. I've tried using the virtual void getinfo function, and inheriting the virtual public classname however I still receive the same results. In my student worker class I call both getinfo functions, should I only be calling one of them? I don't think that would correct the problem because I still need some info that is not in (for instance) the worker class.

        J

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Using multiple inheritance is not easy. For example, the constructors on your base classes are not called automatically. You have to do this by hand.

          That is, in the initialization list of the Student_Worker constructor you have to call the Student constructor, the Worker constructor and the Person constructor. Otherwise your object is not initialized.

          This is on page 731 of the Prata book I referred to.

          In fact your exact problem is illustrated, complete with code, on pages 723-743.

          There are other problems I see in your code.

          Please get that book.

          Otherwise, change the design to not use multiple inheritance (which is never required anyway).

          [code=cpp]
          class Person
          {
          };

          class Student
          {
          Person* p;
          {;
          class Worker
          {

          };

          class Student_Worker
          {
          Student* s;
          }
          [/code]

          Comment

          • Hypnotik
            New Member
            • Jun 2007
            • 87

            #6
            Originally posted by weaknessforcats
            Otherwise, change the design to not use multiple inheritance (which is never required anyway).
            Unfortunately it is required in the assignment. I'll get to borders or barnes and check the book out, although I may not be able to throw out $60 for 1 assignment. What I've gathered from reading articles and examples on the net is that multiple inheritance seems to cause more problems than it's worth. Oh well...I'll meet with the instructor and see what his suggestions are.

            Thanks for taking a look.

            J

            Comment

            Working...