Constructors in Inheritance C++
In this fast-paced world like today we have so many new concepts and technologies evolving every other second that we have to keep up with each one of them being absolutely correct and efficient. Developers while keeping up and developing these technologies have to also make sure that we are constantly evolving from the previous versions and not moving back to the same old philosophy. Let’s consider a scenario of launching a new smartphone with many additional features, but retaining some basic features unlike calling and receiving phone calls which were already present in previous versions of the smartphone. It’s the best example of Inheritance concept in C++. In simple terms, Inheritance simply means taking or extracting the already created or evolved properties and using them to build something new.
In this tutorial, we will be discussing about constructors in the inheritance concept and gain some insights about them.
Basic Understanding
Before moving ahead and gaining some knowledge about these topics through case studies or code examples we have to first understand these concepts thoroughly. A constructor is a special member function that has the name as of the class it is created in. It usually creates objects of its own and then initializes them by providing them some values. It is automatically invoked when we create a class and it contains all the actions to get the work going on.
Practices
In this section of the tutorial, we are going to see some of the practices involved while working with the constructors in the inheritance in C++ language: -
1. Initialization Order
We have to make sure that whenever we start to write our code, we have to begin with the main class first and then move to the special classes and if we don’t follow this order then this might result in run time errors which no one wants to handle and debug.
2. Proper Constructor Overloading
If in case the main classes use many different ways to get ready, we have to make sure that all the special classes know about these methods so we can eventually help them in choosing.
3.Avoid Redundant Calls
Don’t make sure to indulge in everything else and start to set up everything by your own mean, if the main class is getting ready on its own then let it be and don’t over indulge.
4. Virtual Destructors
If the main class has virtual functions for their own set of actions and clean ups then they should also have their own destructors. This will help us in cleaning up the code when the objects or methods are no longer needed, they will be automatically destroyed.
Examples
#include // Base class with a default constructor and a parameterized constructor class Base { public: Base() { std::cout << "This is the base class constructor "; } Base(int m) { std::cout << "This is the base class constructor that contains some arguments: " << m << " "; } }; // Derived class inheriting from Base with a copy constructor class Derived : public Base { public: Derived() { std::cout << "This is the derived class constructor "; } // Parameterized constructor of Derived, explicitly calling the parameterized constructor of Base Derived(int t) : Base(t) { std::cout << "This is the derived class constructor that contains some arguments: " << t << " "; } // Copy constructor of Derived, explicitly calling the copy constructor of Base Derived(const Derived& other) : Base(other) { std::cout << "Creating a copy constructor of the derived class "; } }; int main() { std::cout << "We are creating an object of the base class: "; Base baseObj; // Calls Base class default constructor std::cout << " We are creating an object of the derived class: "; Derived derivedObj; // Calls Base class default constructor, then Derived class default constructor Derived derivedObjWithParam(19); // Calls Base class parameterized constructor, then Derived class parameterized constructor std::cout << " We are copying the object of the derived class: "; Derived copiedDerivedObj = derivedObj; // Calls Base class copy constructor, then Derived class copy constructor return 0; }
Output: