-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path43.) Tut 46 (Constructors in Derived Class).cpp
36 lines (21 loc) · 1.33 KB
/
43.) Tut 46 (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
/*
Constructors in Derived Class in C++
We can use constructors in derived classes in C++
If the base class constructor does not have any arguments, there is no need for any constructor in the derived class
But if there are one or more arguments in the base class constructor, derived class need to pass argument to the base class constructor
If both base and derived classes have constructors, base class constructor is executed first
Special Syntax
C++ supports a special syntax for passing arguments to multiple base classes
The constructor of the derived class receives all the arguments at once and then will pass the call to the respective base classes
The body is called after the constructors is finished executing
/////////////////////////////////////////////////////////////
Derived-Constructor (arg1, arg2, arg3….): Base 1-Constructor (arg1,arg2), Base 2-Constructor(arg3,arg4)
{
….
} Base 1-Constructor (arg1,arg2)
//////////////////////////////////////////////////////////////
Special Case of Virtual Base Class
The constructors for virtual base classes are invoked before a non-virtual base class
If there are multiple virtual base classes, they are invoked in the order declared
Any non-virtual base class are then constructed before the derived class constructor is executed
/*