-
Notifications
You must be signed in to change notification settings - Fork 28
/
Multiple Inheritance II.cpp
237 lines (195 loc) · 5.42 KB
/
Multiple Inheritance II.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <bits/stdc++.h>
using namespace std;
/*
Multiple Inheritance Part - II
-Diamond Problem
-Virtual Inheritance - Virtual Base Class
-Ambiguity
*/
///////////////////Diamond Problem/////////////////
class Person {
public:
Person(int x) {
cout << "Person::Preson(int)" << endl;
}
};
class Faculty : public Person {
public:
Faculty(int x) : Person(x) {
cout << "Faculty::Faculty(int)" << endl;
}
};
class Student : public Person {
public:
Student(int x) : Person(x) {
cout << "Student::Student(int)" << endl;
}
};
class TA : public Faculty, public Student {
public:
TA(int x) : Student(x), Faculty(x) {
cout << "TA::TA(int)" << endl;
}
};
int main() {
TA tx(20);
/*
output
Person::Preson(int)
Faculty::Faculty(int)
Person::Preson(int)
Student::Student(int)
TA::Student(int)
*/
/*
Notice above, two instances of base class object (Person) in a TA object
This is not desirable. How to solve this issue ?
We now see example of Virtual inheritance below
*/
return 0;
}
///////////////////Virtual Inheritance - Virtual Base Class/////////////////
/*
We virtually inherit Person in Faculty and Student.
This will make Faculty and Person to not construct instance of Person inside them.
And then only one instance of base class Person will be constructed which will be common for TA, Student, Person
So, question arises, how will this happen ? No one is calling Person for instance construction, then how will the object
be created for Person.
Ans : We will have to write a defuault constructor for base class Person so that when TA inherits Student and Faculty
, default constrcutor gets called one object of Person is created.
*/
class Person {
public:
Person(int x) {
cout << "Person::Preson(int)" << endl;
}
Person() {
cout << "Default constr -> Person::Person()" << endl;
}
};
class Faculty : virtual public Person { //virtual base class
public:
Faculty(int x) : Person(x) {
cout << "Faculty::Faculty(int)" << endl;
}
};
class Student : virtual public Person { //virtual base class
public:
Student(int x) : Person(x) {
cout << "Student::Student(int)" << endl;
}
};
class TA : public Faculty, public Student {
public:
TA(int x) : Student(x), Faculty(x) {
cout << "TA::TA(int)" << endl;
}
};
int main() {
TA tx(20);
/*
output
Default constr -> Person::Person()
Faculty::Faculty(int)
Student::Student(int)
TA::TA(int)
*/
/*
Note : See above that you can only call a default constructor of Base class.
What if we want to pass values to constructor ?
Se next example below (Just call Base class constructor from TA)
*/
return 0;
}
///////////////////////Last example///////////////////
class Person {
public:
Person(int x) {
cout << "Person::Preson(int)" << endl;
}
Person() {
cout << "Default constr -> Person::Person()" << endl;
}
};
class Faculty : virtual public Person { //virtual base class
public:
Faculty(int x) : Person(x) {
cout << "Faculty::Faculty(int)" << endl;
}
};
class Student : virtual public Person { //virtual base class
public:
Student(int x) : Person(x) {
cout << "Student::Student(int)" << endl;
}
};
class TA : public Faculty, public Student {
public:
TA(int x) : Student(x), Faculty(x), Person(x) { //call Base class constructor (Person(x)) from TA
cout << "TA::TA(int)" << endl;
}
};
int main() {
TA tx(20);
/*
output
Person::Person(int)
Faculty::Faculty(int)
Student::Student(int)
TA::TA(int)
*/
/*
Note : See above that Person::Person(int) gets called this time.
*/
return 0;
}
///////////////////////Ambiguity///////////////////
/*
In a diamond problem, if we have a pure virtual function in Base class, then
it has to be implemented by the derived classes.
But, once derived it becomes ambigous for the class which inherits the derived classes
that which virtual function it inherits.
So, to overcome that, we implement that pure virtual function in the class as well.
*/
class Person {
public:
Person(int x) {
cout << "Person::Preson(int)" << endl;
}
Person() {
cout << "Default constr -> Person::Person()" << endl;
}
virtual void teach() = 0; //since it's pure virtual, Faculty and Student will have to implement it
};
class Faculty : virtual public Person { //virtual base class
public:
Faculty(int x) : Person(x) {
cout << "Faculty::Faculty(int)" << endl;
}
virtual void teach() {
cout << "Faculty::teach()" << endl;
}
};
class Student : virtual public Person { //virtual base class
public:
Student(int x) : Person(x) {
cout << "Student::Student(int)" << endl;
}
virtual void teach() {
cout << "Student::teach()" << endl;
}
};
class TA : public Faculty, public Student {
public:
TA(int x) : Student(x), Faculty(x), Person(x) { //call Base class constructor (Person(x)) from TA
cout << "TA::TA(int)" << endl;
}
//I have to do this because it will be ambigous for compiler that which teach() function it inherits (Faculty, or Student)
virtual void teach() {
cout << "TA::teach()" << endl;
}
};
int main() {
TA tx(20);
return 0;
}