473,395 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <ctype.h>
  4. using namespace std;
  5. class Person
  6. {
  7. private:
  8.     char fname[10];
  9.     char lname[10];
  10.     char gender;
  11.     int age;
  12.     int ssn;
  13. public:
  14.     Person();
  15.     ~Person(){}
  16.     void getinfo();
  17. };
  18. Person::Person()
  19. {
  20.     int i;
  21.     for (i=0;i<10;++i)
  22.     {
  23.         fname[i]=0;
  24.         lname[i]=0;
  25.     }
  26.     gender=0;
  27.     age=0;
  28.     ssn=0;
  29. }
  30. void Person::getinfo()
  31. {
  32.     cout<<"Enter the first name of your person: "<<endl;
  33.     cin>>fname;
  34.     cout<<"Enter the last name of your person: "<<endl;
  35.     cin>>lname;
  36.     cout<<"Enter the gender of your person: "<<endl;
  37.     cin>>gender;
  38.     gender=toupper(gender);
  39.     cout<<"Enter the age of your person: "<<endl;
  40.     cin>>age;
  41.     cout<<"Enter the social security number of your person: "<<endl;
  42.     cin>>ssn;
  43. }
  44. class Student:public Person
  45. {
  46. private:
  47.     float GPA;
  48.     char major[40];
  49.     int hours;
  50.     char school[40];
  51. public:
  52.     Student();
  53.     ~Student(){};
  54.     void getinfo();
  55. };
  56. Student::Student()
  57. {
  58.     int i;
  59.     for (i=0;i<40;++i)
  60.     {
  61.         major[i]=0;
  62.         school[i]=0;
  63.     }
  64.     GPA=0.0;
  65.     hours=0;
  66. }
  67. void Student::getinfo()
  68. {
  69.     Person::getinfo();
  70.     cout<<"Enter the GPA of the student: "<<endl;
  71.     cin>>GPA;
  72.     cout<<"Enter the hours completed by the student: "<<endl;
  73.     cin>>hours;
  74.     cout<<"Enter the major of the student: "<<endl;
  75.     cin>>major;
  76.     cout<<"Enter the school the student attends: "<<endl;
  77.     cin>>school;
  78. }
  79. class Worker:public Person
  80. {
  81. private:
  82.     char occupation[20];
  83.     char company[20];
  84.     float salary;
  85. public:
  86.     Worker();
  87.     ~Worker(){};
  88.     void getinfo();
  89. };
  90. Worker::Worker()
  91. {
  92.     int i;
  93.     for (i=0;i<20;++i)
  94.     {
  95.         occupation[i]=0;
  96.         company[i]=0;
  97.     }
  98.     salary=0.0;
  99. }
  100. void Worker::getinfo()
  101. {
  102.     Person::getinfo();
  103.     cout<<"Enter the occupation of the worker: "<<endl;
  104.     cin>>occupation;
  105.     cout<<"Enter the company: "<<endl;
  106.     cin>>company;
  107.     cout<<"Enter the salary of the worker: "<<endl;
  108.     cin>>salary;
  109. }
  110. class Student_Worker:public Worker,public Student
  111. {
  112. private:
  113.     char slip[10];
  114. public:
  115.     Student_Worker();
  116.     ~Student_Worker(){};
  117.     void getinfo();
  118. };
  119. Student_Worker::Student_Worker()
  120. {
  121.     int i;
  122.     for (i=0;i<10;++i)
  123.     {
  124.         slip[i]=0;
  125.     }
  126. }
  127. void Student_Worker::getinfo()
  128. {
  129.     Student::getinfo();
  130.     Worker:: getinfo();
  131. }
  132. void main()
  133. {
  134.     Person P;
  135.     Student S;
  136.     Worker W;
  137.     Student_Worker SW;
  138. //    P.getinfo();
  139. //    S.getinfo();
  140. //    W.getinfo();
  141.     SW.getinfo();
  142. }
  143.  
  144.  
Thanks for looking,

J
Jul 21 '07 #1
5 1550
svlsr2000
181 Expert 100+
Have a look at virtual inheritance and virtual class.
Jul 21 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
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
Jul 22 '07 #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

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
Jul 22 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
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).

Expand|Select|Wrap|Line Numbers
  1. class Person
  2. {
  3. };
  4.  
  5. class Student
  6. {
  7.     Person* p;
  8. {;
  9. class Worker
  10. {
  11.  
  12. };
  13.  
  14. class Student_Worker
  15. {
  16.     Student* s;
  17. }
  18.  
Jul 22 '07 #5
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
Jul 23 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
30
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
8
by: mmu2643 | last post by:
Hi, I had a question regarding multiple inheritance. class B1{ public: int a; };
4
by: Matt Kruse | last post by:
While developing an internal IE6-only webapp, a discussion started about the 'best' way to apply classes to data tables across multiple pages. The two arguments were: 1. Apply a single class to...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
2
by: mike | last post by:
Hello fellow C++ experts, is there any dramatic difference between multiple inheritance: struct MyType4 : MyType1, MyType2, MyType3 { int MyInt; }; and: struct MyType4 {
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.