If a derived class object is created, which constructor is called first?

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


If a derived class object is created, which constructor is called first

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.

  1. 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).
  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).
  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

  1. Which symbol is used to create multiple inheritances?
  2. If a derived class object is created, which constructor is called first?
  3. Which of the following is not a type of constructor?
  4. a ____ is a member function that is automatically called when a class object is __.
  5. What is the output of the following code in C++?
  6. How many times will the following code print?
  7. Which of the following is a procedural language?
  8. Which of the following is not a programming language?
  9. Which of the following is not an example of high-level language?
  10. While declaring pointer variables which operator do we use?
  11. To which does the function pointer point to?
  12. Which of these best describes an array?
  13. 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?

Leave a Reply

Contents Copyrights Reserved By T4Tutorials