Constructors in Derived Classes in C++


In C++, when a derived class is instantiated, the constructors of both the base and derived classes are executed. The base class constructor is called first, followed by the derived class constructor. This ensures that the base part of the object is properly initialized before the derived part.

How Constructors Work in Derived Classes

  • The base class constructor is automatically invoked when a derived class object is created.
  • If the base class constructor requires arguments, the derived class constructor must explicitly call it.
  • The derived class can pass arguments to the base class constructor using an initializer list.

Examples

1. Default Constructor in Base and Derived Classes

Example:

    #include <iostream>

    class Base {
    public:
        Base() {
            std::cout << "Base class constructor called." << std::endl;
        }
    };

    class Derived : public Base {
    public:
        Derived() {
            std::cout << "Derived class constructor called." << std::endl;
        }
    };

    int main() {
        Derived obj;
        return 0;
    }
        

Output:

    Base class constructor called.
    Derived class constructor called.
        

2. Parameterized Constructor in Base Class

If the base class has a parameterized constructor, the derived class must pass arguments to it explicitly.

Example:

    #include <iostream>

    class Base {
    public:
        Base(int x) {
            std::cout << "Base class constructor called with value: " << x << std::endl;
        }
    };

    class Derived : public Base {
    public:
        Derived(int x) : Base(x) {
            std::cout << "Derived class constructor called." << std::endl;
        }
    };

    int main() {
        Derived obj(10);
        return 0;
    }
        

Output:

    Base class constructor called with value: 10
    Derived class constructor called.
        

3. Parameterized Constructors in Both Base and Derived Classes

Both the base and derived classes can have parameterized constructors, and the derived class can pass arguments to the base class.

Example:

    #include <iostream>

    class Base {
    public:
        Base(int x) {
            std::cout << "Base class constructor called with value: " << x << std::endl;
        }
    };

    class Derived : public Base {
    public:
        Derived(int x, int y) : Base(x) {
            std::cout << "Derived class constructor called with value: " << y << std::endl;
        }
    };

    int main() {
        Derived obj(10, 20);
        return 0;
    }
        

Output:

    Base class constructor called with value: 10
    Derived class constructor called with value: 20
        

4. Default Arguments in Constructors

The derived class can also work with base class constructors that have default arguments.

Example:

    #include <iostream>

    class Base {
    public:
        Base(int x = 0) {
            std::cout << "Base class constructor called with value: " << x << std::endl;
        }
    };

    class Derived : public Base {
    public:
        Derived() : Base(5) {
            std::cout << "Derived class constructor called." << std::endl;
        }
    };

    int main() {
        Derived obj;
        return 0;
    }
        

Output:

    Base class constructor called with value: 5
    Derived class constructor called.
        

5. Constructor Execution Order

The base class constructor always executes before the derived class constructor, regardless of the order in the code.

Example:

    #include <iostream>

    class Base {
    public:
        Base() {
            std::cout << "Base class constructor called." << std::endl;
        }
    };

    class Derived : public Base {
    public:
        Derived() {
            std::cout << "Derived class constructor called." << std::endl;
        }
    };

    int main() {
        Derived obj;
        return 0;
    }
        

Output:

    Base class constructor called.
    Derived class constructor called.
        

Conclusion

Constructors in derived classes allow the initialization of both the base and derived parts of an object. By understanding how constructors work with inheritance, developers can design more robust and efficient object-oriented programs in C++.





Advertisement