C Programming

Pure Virtual Function in C++

A pure virtual function in the C++ programming language is a widely known concept in C++ inheritance. This virtual function can be used in the abstract class. It can only be stated in the abstract class, and it cannot be defined. The declaration value for the pure virtual function is 0. Thus, this article is implemented to determine how the pure virtual function can be stated in the base class and defined in the derived class.

Note: The operating system used in this tutorial is Ubuntu 20.04. You can use other Linux distros or Windows systems. The G++ compiler has been used for the implementation of examples.

Example 01

We are starting with a simple example to illustrate the overall working and structure of a pure virtual function in the C++ abstract class. You have to know that an abstract class doesn’t occupy any object, i.e., not instantiated. However, we can use pointers or references to access this class and its functions. So, create a new C++ file with the help of the “touch” query and open it with an editor, such as Nano editor.

$ touch pure.cc
$ nano pure.cc

Every C++ code starts with the input-output stream header included in it. The namespace standard must be declared after that. An abstract class has been initialized named “Parent”. It contains a single pure virtual function declaration called “show()”. This pure function has been initialized with 0. The derived class named “Child” has been initialized. This Child class has been accessing the Parent class while using it as an interface. The Child class contains the definition of a show() method. The show() method has been displaying some text with the “cout” clause. Here comes the main() method. All the work should start from here:

At the start of a main() method, the Parent class pointer “*p” has been created. As the Parent class is abstract, we cannot create its object. The Child class has been initiated with the object “c”. The pointer “*p” of Parent class has been accessing the address of Child class object “c”. Then the pointer “*p” is calling the pure virtual function “show()” by a reference. After saving the C++ pure virtual code, compile it with the G++ compiler package. No errors have been found so far. After running the file, we have got the following output:

$ g++ pure.cc
$ ./a.out

The result of this illustration can be analyzed in the attached image above.

Example 02

Let’s look at another example of using a pure virtual function in C++. We will be doing some mathematical calculations in the pure virtual method. So, start by opening the same file and modify it a little. Add the header package of the input-output stream and use the standard namespace in the code, as well. We have declared an abstract class named “Result” after the namespace. This class contains a protected integer type variable named “score” used to get marks from the user.

A simple public type method has been defined in the Abstract class named “getscore()”. The “cout” clause in this method requires the user to enter some score. The functions would get the marks from the user as input via the “cin” clause. Then, the pure virtual function “percentage()” is declared to 0 here. The derived class “Status” is defined to get the student percentage by accessing the Parent class as an interface. The Child class “Status” contains the definition of a pure virtual method “percentage()” in it:

The pure virtual function percentage() is calculating the percentage of student marks. This percentage is found with the help of a percentage formula using the marks of a student. The marks have been obtained by the variable “score” having marks entered by the user. The main() function contains the object creation of the Child class. This object is used to call the getscore() method to acquire marks from the user. The same object is used to call the percentage() function to calculate the percentage of marks entered by the user.

Let’s save the code first and exit the file by using “Ctrl+S” and “Ctrl+S”, respectively.

After coming back to the terminal, compile the code first with the g++ compiler package. After that, run the file with the “a.out” query. It will request you to enter your marks. We have added three different marks at every execution and got three different percentages every time.

$ g++ pure.cc
$ ./a.out

The result of this illustration can be analyzed in the attached image above.


Finally, we have used the examples of using a pure virtual function in the C++ abstract class. We have used abstract and derived classes to illustrate the concept of pure virtual function. Based on the information provided, we believe that it will be easy for you to work on pure virtual functions from now on. Thank you for reading this article and please check out the other articles provided by LinuxHint.com.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content