-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path48_Constructor_in_derived_classes.cpp
More file actions
83 lines (62 loc) · 1.74 KB
/
48_Constructor_in_derived_classes.cpp
File metadata and controls
83 lines (62 loc) · 1.74 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
// Constructors in derived classes i.e Inheritance...!!
#include<iostream>
using namespace std;
/*
Case1:
class B : public A{
// Order of execution of constructor --> first A() then B()
}
Case2:
class A : public B, public C{
// Order of execution of constructor --> first B() then C() and then A()
}
Case3:
class A : public B, virtual public C{
// Order of execution of constructor --> virtual C() then B() and then A()
}
*/
class Base1{
int data1;
public:
Base1(int i){
data1 = i;
cout << "Base1 class constructor called!" << endl;
}
void printDataBase1(void){
cout << "The value of data1 is: " << data1 <<endl;
}
};
class Base2{
int data2;
public:
Base2(int i){
data2 = i;
cout << "Base2 class constructor called!" << endl;
}
void printDataBase2(void){
cout << "The value of data2 is: " << data2 <<endl;
}
};
class Derived : public Base1, public Base2{ // Order will change if we write like :--> " public Base2, public Base1 "
int derived1, derived2;
public:
// Now we want to call the constructors of Both Base Classes
Derived(int a, int b, int c, int d) : Base1(a), Base2(b){
derived1 = c;
derived2 = d;
cout << "Derived class Constructor is called." << endl;
}
void printDataDerived(){
// Derived :: printData();
cout << "The Value of derived1 is: " << derived1 <<endl;
cout << "The Value of derived2 is: " << derived2 <<endl;
}
};
int main()
{
Derived obj(1, 2, 3, 4);
obj.printDataBase1();
obj.printDataBase2();
obj.printDataDerived();
return 0;
}