I have the following code:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
#define MNAME 30
class Person {
public:
char name[MNAME + 1];
};
class Student : public Person {
};
class Staff : public Person {
};
class Faculty : public Student, public Staff {
};
int _tmain(int argc, _TCHAR* argv[])
{
Faculty faculty;
cout << "Address of faculty.Person::name: " << &faculty.Person::name << endl;
cout << "Address of faculty.Student::name: " << &faculty.Student::name << endl;
cout << "Address of faculty.Staff::name: " << &faculty.Staff::name << endl;
getch();
return 0;
}
When executed, the program gives the results:
Address of faculty.Person::name: 0012FF20 // **Line 1**
Address of faculty.Student::name: 0012FF20 // **Line 2**
Address of faculty.Staff::name: 0012FF3F // **Line 3**
I don't get it. Why the address in Line 1
and Line 2
is different from Line 3
, while both Student and Staff inherits name from Person?
Multiple inheritance has been a controversial issue for many years, with opponents pointing to its increased complexity and ambiguity in situations such as the "diamond problem", where it may be ambiguous as to which parent class a particular feature is inherited from if more than one parent class implements said ...
Master C and Embedded C Programming- Learn as you go Multiple inheritance occurs when a class inherits from more than one base class. So the class can inherit features from multiple base classes using multiple inheritance. This is an important feature of object oriented programming languages such as C++.
The ambiguity is shown by the derived class object which invokes one of the same-named functions. Inorder to resolve the ambiguity problem in multiple inheritance we use the resolution '::' operator along with which we specify the class name from which the member function is to be invoked.
Multiple inheritance is useful when a subclass needs to combine multiple contracts and inherit some, or all, of the implementation of those contracts. For example, the AmericanStudent class needs to inherit from both the Student class and the American class.
When you do multiple inheritance this way you get two copies of the grandparent class. This is the classic dreaded diamond problem, where you try to do this:
Person / \ Student Staff \ / Faculty
but through normal inheritance you actually get this:
Person Person | | Student Staff \ / Faculty
So there's really 2 Person's in an instance of Faculty, meaning youll get 2 names.
To get the diamond in the first diagram above, you want to use virtual inheritance.
class Staff : public virtual Person {
};
class Student : public virtual Person {
};
With regular multiple inheritance you get multiple copies of shared base classes. If you want one copy, use virtual inheritance.
Explained well in Wikipedia
class Student : public virtual Person {
};
class Staff : public virtual Person {
};
Will get you what you expected
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With