C++

What is Inheritance Constructor in C++

In C++, inheritance is a key notion in object-oriented programming. A derived class can inherit the traits and behaviors of a base class thanks to the powerful inheritance capability in C++. A derived class immediately inherits all the base class’s members when it is formed. It is necessary for the derived class constructor to initialize the base class members as well. In C++, inheritance is a powerful feature that enables developers to reuse code, improve efficiency, and organize code into logical hierarchies.

Along with inheritance, constructors are also essential in C++. A constructor is a unique member function that allows you to initialize the object’s properties. An Inheritance constructor is a constructor used to initialize both the base class and derived class objects that the derived class has inherited. The inheritance constructor is responsible for initializing both the inherited base class members and the derived class members. To achieve this, the constructor invokes the constructor of the base class, ensuring that all members of the derived class are properly initialized, including those inherited from the base class.

By invoking the base class’s constructor and passing it the necessary parameters, the inheritance constructor initializes the members of the base class. This is accomplished using the ‘base class(args)’ initialization list in the constructor. Further instructions particular to the derived class are provided in the constructor body.

The syntax for inheriting a class in C++ is:

class DerivedClass : accessSpecifier BaseClass {
    // class members
};

The new class being formed in this case, “DerivedClass”, will inherit from “BaseClass”. The level of access to the inherited members is specified by the “accessSpecifier”. C++ utilizes three types of access specifiers, namely “public”, “private”, and “protected”. The word “public” indicates that the derived class has access to the base class’s public members. The derived class has no access to the members of the base class, according to the ‘private’ specifier. The derived class has access to the protected base class members that can be inherited by their child classes, according to the ‘protected’ specifier.

Example of Inheritance in C++

The following example show the implementation of Inheritance in C++:

#include <iostream>
using namespace std;

class Animal {
    public:
        void eat() {
        cout << "I can eat!" << endl;
    }
    void sleep() {
        cout << "I can sleep!" << endl;
    }
};
class Cat : public Animal {
    public:
        void meow() {
        cout << "I can meow!" << endl;
    }
};
int main() {
    Cat cat1;
    cat1.eat();
    cat1.sleep();
    cat1.meow();

    return 0;
}

The given code defines two classes, “Animal” and “Cat”, where “Cat” is derived from “Animal”. Both classes have some member functions, such as “eat”, “sleep”, and “meow”. The main function creates an object of the class “Cat” and calls the inherited functions from the class “Animal” as well as the specific function of the class “Cat”, which is “meow”. The program outputs the text to the console, indicating that the cat can eat, sleep, and meow.

Output

When a class is derived, all the base class’s member variables and member functions are transferred to the derived class. The inherited members’ accessibility in the derived class is determined by the access specifier. The derived class may also substitute new features for those that already exist. It can add new implementations for the base class’s methods and add new member functions and variables.

It is crucial to remember that the inheritance constructor must be used to explicitly call the base class’s constructor with the necessary parameters if the base class has a default constructor or if it is not available.

Conclusion

The strong feature of inheritance in C++ allows classes to reuse code by inheriting characteristics and actions from other ones. A useful feature of C++’s inheritance constructor allows derived classes to inherit the properties, methods, and data members of a base class. With the addition of an initialization list for base class constructors, it is defined using the same syntax as a standard constructor. C++ programmers can minimize code duplication and enhance code structure by utilizing the inheritance constructor. For creating complex class hierarchies in object-oriented programming, it is an important tool.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.