C++ Programming

Constructors and Destructors in Single and Multiple inheritance in C++

Constructors and Destructors in Multiple inheritance:

This is going to be a very detailed article on what are the Constructors and Destructors in C++, what is their purpose and how they are used in C++ programs. I will share with you some programs to help you understand how to use the Constructors and Destructors.

Constructors: In multiple inheritance, the constructors of the base classes and constructors of the derived class are automatically executed when an object of the derived class is created. The constructors of the base classes are executed first and then the constructor of the derived class is executed. The constructors of base classes are executed in the same order in which the base classes are specified in derived class(i.e. from left to right).

Similarly, the destructors are executed in reverse order, i.e. derived class constructor is executed first and then constructors of the base classes are executed (from right to left).

If the constructors of the base classes have no parameters, then the syntax to create relationship between derived class and base classes is very simple.


Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Example1:

Following program explains the constructors and destructors in multiple inheritance

#include <iostream>

using namespace std;




class base1

{

public:

  base1 (void)

  {

    cout << " constructor of class base1\n";







  }

   ~base1 ()

  {

      cout << " destructor of class base1\n";










  }




};




class base2

{

   

    public:

    base2(void)

    {

        cout << " constructor of class base2\n";




       

    }

    ~base2()

    {

       

        cout << " destructor of class base2\n";




    }

   

};

class derive1 : public base1, public base2

{

   

    public:

    derive1(void)

    {

     cout << " constructor of class derive1\n";




    }

    ~derive1()

    {

        cout << " destructor of class derive1\n";




       

    }

};




main ()

{

    derive1 x;

    cout << " Destructors are: "<<endl;


}

constructors and Destructors

In the above example code, the two classes base1 and base2 are defined. The class derive1 is publicly derived from two classes base1 and base2. It means that classes base1 and base2 are base classes while derive1 is a derived class. In the derived class “derive1”, the base1 is written first and then class base2 is written, the constructors of these classes are executed first in the same order as specified in derived class (i.e. left t right) and then the constructor of derive1 class is executed. Similarly, destructors of these classes are executed in reverse order such as:

  • Destructor of derive1 class is executed first.
  • Destructor of base2 is executed after destructor of derived1.
  • Destructor of base1 is executed after destructor of base2.



Constructors in multiple inheritance with arguments:

The constructor of the derived class calls the constructors of the base classes in the same order in which they are specified in the header of the derived class. The syntax define constructors in multiple inheritance with arguments is also similar to the definition of single inheritance with arguments. The constructors of the base classes are connected with the constructor of the derived class by using colon( : ) and separated by commas.

Example 2:

Following program explains the constructors with arguments in multiple inheritance:

#include <iostream>

using namespace std;




class base1

{

public:

base1(int x, int y)

{

    cout<<" Sum of values passed to base1: "<<x+y<<endl;

   

}

   

};

class base2

{

    public:

    base2(char str[])

    {

       

        cout<<" String passed to base2: "<<str<<endl;

       

    }

};

class derive1 : public base1, public base2

{

    public:

    derive1(int x, int y, char str[], double d) : base1(x,y),base2(str)

    {

       

        cout<<" Value passed to derive1: "<<d;

    }

   

   

};




main ()

{

  derive1 obj(6, 2, "Programming digest", 2.5);







}

 

constructors and Destructors

 

In this program the derived class derive1 contains four arguments, first two arguments for base1 class and third for base2 class. the fourth argument has its own argument.

Constructors and Destructors in Single Inheritance:

When an object of the derived class is created, a part of the base class is also included with that object. Actually, the base class is initiated to include the base class part with the object of the derived class and then the derived class part is included. So, when the derived class object is created, the constructor of the base class is automatically executed first and then the constructor of the derived class is executed.

The base class constructors are not inheritance by derived class. however, a derived class constructor always calls the constructor for its base class first to initialize base class data members for the objects of the derived class. if the derived class constructor is omitted, the derived class default constructor calls the base class constructor.

Destructors are called in the reverse order of constructor calls. So a derived class destructor is called its base class destructor.


Example 3:

Following program explains the concept of constructors and destructors in single inheritance:

#include <iostream>

using namespace std;

class A

{

    public:

    A(void)

    {

        cout<<" Constructor of the base class A\n ";

       

    }

    ~A()

    {

       

        cout<<" Destructor of the base class A\n ";

    }

   

};

class B : public A

{

   

    public:

    B(void)

    {

        cout<<" Constructor of the derive class B\n ";

       

    }

    ~B()

    {

        cout<<" Destructor of the derive class B\n ";

       

    }

   

};




main ()

{

  B obj;

}

 

constructors and Destructors

 

In this program, both constructors of the base class and derived class do not use arguments. If the base class constructor uses no argument, then the syntax to create the relationship between derived class base is very simple.

When the object obj of class B is created during program execution, then the constructor of the base class is executed first and then the constructor of the derived class is executed, similarly, when the object of class B is destroyed (just before the program termination), the destructor of the derived class B is executed first and then destructor of the base class A is executed.

Constructor in Single Inheritance With Arguments:

the derived class is responsible for calling the constructor of the base class. in case of constructor of the base class with arguments, the syntax to define constructor of the derived class is different. The base class constructor is connected with the derived class constructor by using the colon (: ) in the header of the derived class constructor. The parameters of the base class constructor are also given  as parameter list in the derived class constructor. In derived class constructor, the parameters of the base class come first than the derived class constructor. The reason is that all data of the base class constructor is initialized before the derived class constructor.


Example 4:

Following program explains the concept of constructors with arguments in single inheritance:

#include <iostream>




using namespace std;




class A

{

    private:

        int x,y;

    public:

    A(int m, int n)

    {

       

        x=m;

        y=n;

    }

    void print(void)

    {

        cout<<" value of x = "<<x<<endl;

        cout<<" value of y = "<<y<<endl;

       

    }

   

};

class B : public A

{

   

    private:

        float i, j;

    public:

    B(int m, int n, float a, float b) : A(m, n)

    {

        i = a;

        j= b;

       

    }

    void show(void)

    {

        cout<<" value of i = "<<i<<endl;

        cout<<" Value of j = "<<j<<endl;

       

    }

   

};

main ()

{

  B obj(5,7,29.4,56.3);

  obj.print();

  obj.show();







}

constructors and Destructors

 

In this program, the object obj of the derived class used four values. These values will be passed to the constructor of the derived class B, which used four parameters. The first two parameters ‘m’ and ‘n’ are used for base class constructor. The arguments ‘a’ and ‘b’ are used for derived class constructor.

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button