C++

Call Base Class Function in C++

There is a concept of inheritance in many programming languages like C++. It will provide you the opportunity to use the child classes in your program that are being derived from their respective base classes. A child class can access the data members of its specific base class, i.e., variables and methods. Within this guide, we will be discussing different ways to execute or call the base call function in C++. So, let’s get started with the launch of the terminal console of the Ubuntu 20.04 Linux operating system using the shortcut key “Ctrl+Alt+T” on its desktop. Let’s have a look at some of the simple examples to call a base method in C++ now.

Example 01:

The first example started with the generation of a c++ new file that will be used to create a code. The very known “touch” instruction is utilized here to create the “base.cc” file. This file must be opened within some editor that is already built-in in the Ubuntu 20.04 system, i.e., vim, nano, or text editor. We have chosen the “nano” editor to open it.

The code has been started from the inclusion of some needed header files of C++ like “iostream” and “bits/stdc++.h”. The file is opened in the empty GNU Nano editor of the Ubuntu 20.04 system. The C++ language uses the “Std” namespace to utilize “cout” and “cin” clauses for displaying and getting input. We have started a new class, “A” with a public method show(). This function contains a single cout statement to display that this is a parent class function executing right now. After this class, we have created a new class, “B” Inherited from the class “A”. This means class B is a child class of class A and can inherit its properties. Class “B” also contains a public type function named “display()”. This function utilizes a single “cout” statement here to display that this function has been executed within the child class of this program. Now, the child class has been ended here. We have started the main() method here to execute the classes.

Now, we will try to call the function “show” of parent class A without creating its object. We will be using the object of child class B for that purpose. So, within the main() function, we have created an object “obj” of child class B using the “dot” between the class name and the object name. This object “obj” has been utilized here with the “dot” sign to call the function “show()” of the parent class named “A”. We are not using this object to call the function of a child class B. If child class B has contained a constructor function, that constructor function will be executed right after creating an object. This is the concept of inheritance using an object of child class to call the function of the respective parent class. This is how we do it. Let’s save and quit the code with Ctrl+S and Ctrl+X shortcuts consecutively.

After saving the code, we are back in the terminal shell. Before the execution, code needs to be compiled on the shell with some c++ compiler. Ubuntu 20.04 is coming up with a “g++” compiler that can be installed with the “apt” package. So, we have used this “g++” compiler to compile the newly made file and then executed it with a “./a.out” Ubuntu 20.04 instruction. In return, the parent class function “show()” has been executed, and we got the display message.

Example 02:

Our first example was calling a function from a base class that contains a unique name overall the code. But, what will you do when both parent and child classes contain the same name function with the same parameters and return types? Let’s have a look at this method to call a base class function. After the header files and namespace, we have declared two classes, A and B. A is the parent class of B, and class B derives the properties of parent class A. Both classes A and B contain a function “same()” with the same name and same implementation separately. The implementation of both functions contains the cout statement showing whether the parent class method or child class method has been executed.

We have used the function call for parent class A method “same()” within the child class B using the “::” sign at the last line. This is to avoid the inconvenience that could be caused when an object will call the same name function. So, within the main() function, We have created an object “b” of child class “B”. This object “b” is used to call the function “same” of the child class B. When a user tries to call the same name function of the parent class with the same object, it will throw an exception. Due to the use of the “A::same” function call in the child class function, it will avoid the exception and no need to create a new object. Save your code and quit the editor.

The compilation and running of updated code lead us to the below output. You can see that both the same name functions of the child and parent class are executed with a single object.

Example 03:

Let’s see another method to call the base call same name function in C++. So, we have updated the same code. The header files, namespace, parent, and child classes are unchanged, i.e., not updated. The only update has been done within the “main()” method of this code. We have created two objects, b1 and b2, of child class “B”. The object b1 is calling the function “show()” of child class directly. While the object b2 is calling the parent class show() function using the “::” sign between its class name and function name. Let’s just run this code after saving it.

The result of this code execution worked successfully, and we can see that the base class function is also called using the child class object “b2”.

Example 04:

The last example is quite different from all the above examples. Overall code is unchanged while the main() function has been updated little. We have created an object “b” of child class B. This object “b” calls the child class function “same”. Then created a pointer “p” of base class “A” type that has been pointing towards the object “b” of child class B. This pointer is then used to call the same() function of the base class A. Let’s run this code and see the results.

After running this updated pointer code, we have seen that the base class function was executed using the pointer after executing the child class method. This is how we do it.

Conclusion:

This article is a super bonus for users who are looking for inheritance concepts. It demonstrated a calling base class function using the child class object or parent class object. On the other hand, we have also utilized the concept of pointers to call the base class function from the main function of a program.

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.