-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathInheritance_Part_II.cpp
56 lines (48 loc) · 1.18 KB
/
Inheritance_Part_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
#include <iostream>
using namespace std;
/*
Inheritance - II
:Data Members
-Deriver class inherits all data members of Base class
-Derived class may add data members of its own
:Object Layout
-Derived class layout contains an instance of the Base class
-Further, Derived class layout will have data members of its own
-C++ does not guarantee the relative position of the Base class instance and Derived class members
*/
class B { //Base class
int data1B_;
public:
int data2B_;
// ...
};
class D : public B { //Derived Class
//Inherits B::data1B_;
//Inherits B::data2B_;
//(in same order as above)
int infoD_;
public:
// ...
};
int main() {
B b;
/*
Object b layout
_________
|_data1B_|
|_data2B_|
*/
D d;
/*
Object d layout (It cannot access data1B_ even though data1B_ is a part of it because it's private)
(It can access data2B)
________________
| _________ |
| |_data1B_| |
| |_data2B_| |
| __________ |
| |_infoD___| |
|________________|
*/
return 0;
}