C++

C++ Child Classes

There is a concept of inheritance in the programming language used to generate a parent-child relationship between different classes. When an inheritance takes place, there comes the child classes. The term, child class, means it is inheriting some properties of a parent class, i.e., base class. Here, you have to understand that the C++ classes are initialized with their object. While in an inheritance, only the child class objects will be created and not the parent class. The child class object will be used to execute or run the parent class along with its class.

Method 01: Simple/Single Inheritance

We will be starting from a Single Inheritance. It contains one single child class that will be derived from the single parent class. So, open the shell terminal using the “Ctrl+Alt+T”. Make sure to have the C++ compiler already configured on your Ubuntu 20.04 system. Use the “touch” keyword to create a new C++ file named “child.cc”. After creating the file, open it with an editor to edit it. We have been using the Nano editor to open it directly in the editor within the terminal. You can also use the Vim or Text editor. Both the commands for creating and opening a C++ file are listed and shown in the following screenshot:

We have added an input-output stream at its start. After that, another standard c++ header was added. After the header file, we have used the standard namespace to use input-output statements in the code. We have declared a parent class named “A” with a single integer type variable “ida” and can be accessed anywhere in the other classes as it is public. Another class named “B” has been declared as a child class as it is inheriting the class “A” using the “:” sign. Both the classes are public here. The child class “B” also contains a public data member of the integer type named “idb”. Then, the main method has been initialized. It contains the initialization of a child class object “b”.

Point to be noted that within the inheritance, there is no need to create an object of a parent class while it has a child class inheriting it. The child class object “b” has been used to initialize and display the public data members of the parent and child class, as per the image below. Save this code and quit the file via the “Ctrl+S” and “Ctrl+X”:

Compile the C++ code using the GCC compiler on the shell. The compilation was successful. After the execution, we have the result as parent class ID and child class ID:

Method 02: Multiple Inheritance

Let’s begin with the example of Multiple Inheritances. Open the code file “child.cc” again in the Nano editor. The header files and standard namespace has been left unchanged. We have declared two parent classes A and B, each containing the constructor in it, i.e., A() and B(). Both the constructors contain the standard cout statement used to be printed out and tell us which class we are currently in. The child class C has been declared, which inherits both parent classes A and B using the “:” sign. This class doesn’t contain any implementation. After the class initialization and declarations, we have used the main method. The main() function contains the initialization of child class C objects, i.e., “obj”. After creating this object, both the constructors of parents classes, i.e., A and B, will be executed automatically. This is because the constructor doesn’t need any function call, and it automatically executes when a class object has been created.

Thus, both the cout statements within the constructor functions will be printed out at the shell. Save the updated code and come back to the shell via the keystrokes Ctrl+S and Ctrl+X:

Compilation of this code doesn’t throw an exception; hence no errors have been found so far. The execution shows the output as expected. Both the print statements have been displayed on the shell, i.e., the cout statement of parent class A and B:

Method 03: Multilevel Inheritance

In this kind of inheritance, one child class would be derived and using the other class as a child class itself. So, we have opened the same code file within the Nano editor. The header files are left unchanged. We have declared three classes, i.e., A, B, and C. Class C is the child class of parent class B, and Class B is the child class of parent class A. Only class A is the one not inherited from any other class. All three classes contain constructors using the cout statement to display the relevant message on the shell as per their class. The object of the C class has been created, which will execute the constructor of its parent class B, and eventually execute the constructor of grandparent class A:

After the compilation and execution, we have executed all the cout statements within the constructors of three classes with a single object of child class C, such as obj:

Method 04: Hierarchical Inheritance

The inheritance can be called Hierarchical when more than one child’s classes are derived from a single class. So, we have updated the code very slightly. The child classes B and C are derived from the parent class A from the image code below. As there are two child classes, there are two objects as well. The object “objb” is of the child class B and runs the constructor of both parent class A and child class B. The object “objc” is the child class C, running both the constructors of parent class A and child class B. So, the parent constructor was executed two times here:

After running the updated file, we have the expected result of 4 lines:

Method 05: Hybrid/Multipath Inheritance

The Hybrid or Multipath Inheritance can be achieved by merging more than one type of inheritance in one situation. We join the Simple/Single Inheritance, Multiple Inheritances, and Multilevel Inheritance in one example code. From the 4 classes, class C is inheriting from class A showing the Simple/Single Inheritance. Class D is a derivative of both class B and C, and it has multiple inheritances. While class C is already inheriting from class A, thus “D->C->A” becomes the illustration of Multilevel Inheritance. Only a single object “d” has been created, i.e., child class D:

After the compilation and running of the file, the output is displayed below:

Conclusion:

Within this article, we have implemented C++ child classes while using different types of inheritance. We have started from scratch by showing a simple example of inheritance about how a child’s class can be created and used. Within all the different inheritance examples, we have demonstrated the child classes more efficiently to make them understand fully. We hope this article will be helpful for every naive user of C++. Please check out Linux Hint for more informative articles.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.