Question: If a derived class object is created, which constructor is called first?
a. Derived class constructor
b. Base class constructor
c. Totally Depends on that how we call the object
d. Both a and B
MCQs Answer:
Base class constructor
Now, let’s see the diagram. The numbers 1, 2, 3, and 4 in the diagram represents a sequence of execution.
The diagram has 3 parts.
- The Left most diagram represents single inheritance
- When the object of the child class creates, then firstly constructor of the base class will execute(represents with 1), and then the child class constructor will execute (represents with 2).
- The central diagram represents Multiple inheritance
- When the object of the child class creates, then firstly constructor of the base classes will execute(represents with 1 and 2 in parent classes), and then the child class constructor will execute (represents with 3).
- The rightmost diagram represents Multilevel inheritance
- When the object of the child class creates, then firstly constructor of the topmost base classes will execute(represents with 1 in top parent class), and then the constructor of the middle base classes will execute(represents with 2 in middle parent class), and then the child class constructor will execute (represents with 3).
C++ Program to represents that If a derived class object is created, then which constructor is called first?
//eg of constructor and destructor in single inheritance
#include<iostream>
using namespace std;
class Parent
{
public:
Parent()
{
cout<<"The Parent class constructor will execute first."<<endl;
} ~Parent()
{
cout<<"The Parent class destructor will execute second."<<endl;
}
};
class Child:public Parent
{
public:
Child()
{
cout<<"The Child class constructor will execute second."<<endl;
} ~Child()
{
cout<<"The Child class destructor will execute first."<<endl;
}
};
int main()
{
Child d;
return 0;
}
Output
The Parent class constructor will execute first.
The Child class constructor will execute second.
The Child class destructor will execute first.
The Parent class destructor will execute second.
Note: in the case of destructors, the destructor of the child class will execute first and then the destructor of the parent class will execute.
1000+ Programming C Plus Plus MCQs
Highly Recommended C++ Important MCQs with Explanation
- Which symbol is used to create multiple inheritances?
- If a derived class object is created, which constructor is called first?
- Which of the following is not a type of constructor?
- a ____ is a member function that is automatically called when a class object is __.
- What is the output of the following code in C++?
- How many times will the following code print?
- Which of the following is a procedural language?
- Which of the following is not a programming language?
- Which of the following is not an example of high-level language?
- While declaring pointer variables which operator do we use?
- To which does the function pointer point to?
- Which of these best describes an array?
- Which of the following is a Python Tuple?
Other important MCQs
Data Structure
Which of the following is not the type of queue?
Database
If one attribute is a determinant of the second, which in turn is a determinant of the third, then the relation cannot be
Python
Which of the following is a Python Tuple?
