-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructorsInDerivedClasses.cpp
More file actions
88 lines (71 loc) · 1.36 KB
/
ConstructorsInDerivedClasses.cpp
File metadata and controls
88 lines (71 loc) · 1.36 KB
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
#include <iostream>
using namespace std;
// Note: It's detailed explaination is given in notebook.
/*
class A: public B
{
// order of execution of constructor -> first B() then A()
}
class A: public C, public B
{
// order of execution of constructor -> first C() then B() then A()
}
class A: public C, virtual public B
{
// order of execution of constructor -> first B() then C() then A()
}
*/
class Base1
{
protected:
int var1;
public:
Base1(int one)
{
var1 = one;
cout << "Base1 constructor called " << endl;
}
void display_base1()
{
cout << "Base1: " << var1 << endl;
}
};
class Base2
{
protected:
int var2;
public:
Base2(int two)
{
var2 = two;
cout << "Base2 constructor called " << endl;
}
void display_base2()
{
cout << "Base1: " << var2 << endl;
}
};
class Derived : public Base1, public Base2
{
int der1, der2;
public:
Derived(int a, int b, int c, int d) : Base1(a), Base2(b)
{
der1 = c;
der2 = d;
cout << "Derived constructor called " << endl;
}
void display_derived()
{
cout << "Der1: " << der1 << endl;
cout << "Der2: " << der2 << endl;
}
};
int main()
{
Derived obj(5, 10, 15, 20);
obj.display_base1();
obj.display_base2();
obj.display_derived();
return 0;
}