Order of Constructor Call in Inheritance
In inheritance we derive a class from the base class. Once inheritance takes place then we can create objects of the derived class. It is worth mentioning that objects of the derived class can access all the members of derived as well as base class.
While creating the object of the derived class, data members of both derived and base class need to be initialized. For this initialization of data members we use constructors.
Default Constructors of Base and Derived Classes
As the object of derived class use the data members of both derived and base class, therefore constructors of both derived and base class will be called while creating the object of derived class to initialize the corresponding data members.
By default, the default constructor of base class is called unless specified explicitly.
The following example shows that if the object of derived class is created using the default constructor of derived class then it calls the default constructor of base class as well.
We can see that while creating object of class Student at line 27, no parameters are passed. It means that the default constructor of Student class will be called which in turn will call the default constructor of base class Person. As a result the following output is produced.
Parameterized Constructors of Base and Derived Classes
If we want to mention the values to be assigned to the members of the object at the time of object creation then we will need to use the parameterized constructor of derived class which in turn calls the default constructor of base class but may call the specified parameterized constructor of base class as well if mentioned.
The following code shows that how to mention that which constructor of base class will be called by the constructor of derived class at the time of object creation.
We can see at line 29 above that when the parameterized constructor of Student class is called, it in turn calls the parameterized constructor of Person class sending the values of name and age to the base class. The output of the above code is as below.
More Related Articles For You
- What is C++
- C++ and Object Oriented Programming OOPS concepts
- Syntax and Structure of C++ program
- Data Types in C++
- C++ Variables
- Types of operators in C++
- Decision making in C++
- C++ Loop Types
- Storage Classes in C++
- Functions in C++
- Classes and Objects in C++
- Access controls in C++ Classes
- Defining Class and Object in C++
- Accessing Public and Private Data Members in C++
- Member Functions in Classes
- Types of Member Functions in C++
- Inline Functions in C++
- Namespaces in C++
- Function Overloading in Classes in C++
- Constructors and Destructors in C++
- Static Keywords in C++
- Const Keyword in C++ Programming
- References in C++
- Copy Constructor in C++
- Pointer to Members in C++ Classes
- Introduction to Inheritance
- Types of Inheritance
- Upcasting in C++
- Function Overriding in C++
- Virtual Functions in C++
- Abstract Class and Pure Virtual Functions in C++
- Virtual Destructors in C++
- Operator Overloading in C++