• Main Menu
  • Constructors in Derived Classes


    A constructor plays a vital role in initializing an object. An important note, while using constructors during inheritance, is that, as long as a base class constructor does not take any arguments, the derived class need not have a constructor function. However, if a base class contains a constructor with one or more arguments, then it is mandatory for the derived class to have a constructor and pass the arguments to the base class constructor. Remember, while applying inheritance, we usually create objects using derived class. Thus, it makes sense for the derived class to pass arguments to the base class constructor. When both the derived and base class contains constructors, the base constructor is executed first and then the constructor in the derived class is executed.

    In case of multiple inheritance, the base class is constructed in the same order in which they appear in the declaration of the derived class. Similarly, in a multilevel inheritance, the constructor will be executed in the order of inheritance.

    The derived class takes the responsibility of supplying the initial values to its base class. The constructor of the derived class receives the entire list of required values as its argument and passes them on to the base constructor in the order in which they are declared in the derived class. A base class constructor is called and executed before executing the statements in the body of the derived class.

    The header line of the derived-constructor function contains two parts separated by a colon (:). The first part provides the declaration of the arguments that are passed to the derived class constructor and the second part lists the function calls to the base class.

    Example:

    D(int a1, int a2, int b1, int b2, int d): A(a1, a2), B(b1, b2) 
    { 
        d = d1; 
    } 
    

    A(a1, a2) invokes the base constructor A() and B(b1, b2) invokes another base class constructor B(). The constructor D() supply the values i.e. a1, a2, b1, b2 (to A() and B()) and to one of its own variables d1.

    Hence, the assignment of the values will be as follows:

    When an object of D is created, D object-name(5, 12, 7, 8, 30);

    a1 <- 5 a2 <- 12 b1 <- 7 b2 <- 8 d1 <- 30

    The constructors for a virtual base class are invoked before any non-virtual base classes. If there are multiple virtual base classes, then they are invoked in the order in which they are declared.

    Example:

    // program to show how constructors are invoked in derived class
    #include <iostream.h>
    class alpha
    (
        private: 
            int x; 
        public: 
            alpha(int i)
            {
                x = i;
                cout << "\n alpha initialized \n";
            }
            void show_x()
            {
                cout << "\n x = "<<x;
            }
    );
    class beta
    (
        private: 
            float y; 
        public: 
            beta(float j)
            {
                y = j;
                cout << "\n beta initialized \n";
            }
            void show_y()
            {
                cout << "\n y = "<<y;
            }
    );
    class gamma : public beta, public alpha
    (
        private: 
            int n,m; 
        public: 
            gamma(int a, float b, int c, int d):: alpha(a), beta(b)
            {
                m = c;
                n = d;
                cout << "\n gamma initialized \n";
            }
            void show_mn()
            {
                cout << "\n m = "<<m;
                cout << "\n n = "<<n;
            }
    );
    
    void main()
    {
        gamma g(5, 7.65, 30, 100);
        cout << "\n";
        g.show_x();
        g.show_y();
        g.show_mn();
    }
    

    Output:

    beta initialized
    
    alpha initialized
    
    gamma initialized
    
    
    x = 5
    y = 7.65
    m = 30
    n = 100
    

    Got Something To Say:

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

    7 comments
    1. Harsh

      15 August, 2016 at 1:18 pm

      This code is not woking !

      Reply
      • Hattori

        22 October, 2017 at 1:05 pm

        public:
        gamma(int a, float b, int c, int d) :: alpha(a), beta(b)

        In the above line, replace the scope resolution operator (::) by a colon (:)

        Reply
    2. RACHANA PATIDAR

      3 October, 2015 at 11:43 am

      as long as a base class constructor does not take any arguments, the derived class need not have a constructor function. However, if a base class contains a constructor with one or more arguments, then it is mandatory for the derived class to have a constructor and pass the arguments to the base class constructor. Why?

      #include

      using namespace std;

      class Person {

      public:

      Person(int x) { cout << "Person::Person(int ) called" << endl; }

      Person() { cout << "Person::Person() called" << endl; }

      };

      class Faculty :public Person {

      };

      int main() {

      Faculty f;

      }

      compile it. no error

      Reply
    3. isha

      7 May, 2015 at 2:18 pm

      is it possible to write derived class constructor without calling base class constructor?

      Reply
      • Aliabbas

        31 August, 2015 at 9:17 am

        yes its possible to call base class without calling derived class because in last they have only call arguments

        Reply
    4. Paul

      25 May, 2012 at 3:52 pm

      Very helpful. Thank you.
      With gnu c++, private had to be tweaked to protected in alpha and beta.

      Reply
    5. dantreliya sanjay

      29 August, 2011 at 4:37 am

      class: public A
      {                                                    A() base constuctor
                                                          B() Derived consutructor       }

      class A:

       

       

      Reply
    C
    173 queries in 0.468 seconds.