-
Notifications
You must be signed in to change notification settings - Fork 0
/
Constructors_in_derived_class.cpp
62 lines (59 loc) · 1.39 KB
/
Constructors_in_derived_class.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
#include <iostream>
using namespace std;
class Base1
{
private:
int data1;
public:
Base1(int input)
{
data1 = input;
cout << "Inside constructor of Base-1" << endl;
}
void getData1()
{
cout << "Base-1 data is: " << data1 << endl;
}
};
class Base2
{
private:
int data2;
public:
Base2(int input)
{
data2 = input;
cout << "Inside constructor of Base-2" << endl;
}
void getData2()
{
cout << "Base-2 data is: " << data2 << endl;
}
};
class Derived : public Base1, public Base2 // if this order changes then order of execution changes
{
private:
int data3, data4;
public:
Derived(int a, int b, int c, int d) : Base1(a), Base2(b)
{
data3 = c;
data4 = d;
cout << "Inside constructor of Derived" << endl;
}
void getData()
{
cout << "Derived data3 is " << data3 << endl;
cout << "Derived data4 is " << data4 << endl;
}
};
/*Always first Base classes will be executed and then the derived class. In Base classes also,
first virtual base classes will be executed and then non-virtual base classes will be executed and that non-vitual
base class will be executed before which has been declared before.*/
int main()
{
Derived d(1, 2, 3, 4);
d.getData1();
d.getData2();
d.getData();
}