C++

Abstract Base Class in C++

The usage of classes in C++ is the core of object-oriented and modular programming. Depending upon the functionality, these classes can be divided into different types. One such type is the abstract base class in C++. The sole purpose of writing this article is to discuss the significance of the abstract base classes in C++ and also walk you through their usage in Ubuntu 20.04.

Purpose of Using the Abstract Base Class in C++:

An abstract base class in C++ is created by making at least one of its functions pure virtual. A pure virtual function in C++ is that whose prototype is equalized to “0” within the base class, and its implementation has to be provided within every derived class (failing to do so results in a compile-time error). The reason behind using the abstract base classes in C++ is to provide a general template that all the derived classes can use. The implementation of the pure virtual function or functions can be modified in every derived class according to the required functionality. Moreover, the object of an abstract base class should never be created since it leads to a compilation error.

Summary of the Important Properties of the Abstract Base Class in C++:

In the following points, we will try to summarize all the important features of an abstract base class in C++:

  • At least, one of the functions of an abstract base class must be purely virtual. However, depending upon your needs, you can also have more than one pure virtual function.
  • The implementation of the pure virtual function[s] must be provided in every derived class.

Using the Abstract Base Class in C++ in Ubuntu 20.04:

For explaining the usage of the abstract base class in C++ in Ubuntu 20.04, we have implemented an example in C++. We have added relevant comments to each section of our code that makes it quite self-explanatory; however, we will still be explaining each section of this code to you separately so that you can get a fair idea of how the abstract base classes and their corresponding derived classes work in C++ in Ubuntu 20.04. We want to calculate the household and commercial electricity bills depending upon the units consumed in this example. The abstract base class for this particular example is as follows:

We have created an abstract base class named “ElectricityBill”. We have made some members public within this abstract base class by using the “public” keyword. First, we have the pure virtual function with the declaration “virtual int getBill()=0”. It means that we will have to provide separate implementations of this function in each of our derived classes. This function aims to return the total household and commercial electricity bills depending upon the units consumed. Then, we have the setter function named “setUnits(int u)” for setting the respective units. After that, we have a “protected” member “int units”. The reason behind making this member protected is that it can be accessed easily within our derived classes as well but not within any other class.

Then, we have our first derived class named “HouseholdBill” and we have publicly inherited it from the “ElectricityBill” class so that all of its public members can also stay public in our derived class. In this derived class, we have only provided the implementation of the “getBill()” function. For this particular implementation, we have assumed the price of electricity per unit as 10 USD. Then, we have simply returned the bill through this function after calculating it.

Then, we have our second derived class named “CommercialBill” and we have publicly inherited it from the “ElectricityBill” class for the same reason that we have described above. In this derived class, we have only provided the implementation of the “getBill()” function. For this particular implementation, we have assumed the price of electricity per unit as 20 USD. Then, we have simply returned the bill through this function after calculating it.

Finally, we have our “main()” driver function. In this function, we have first created the objects of our two derived classes with the names “HB” and “CB” respectively. After that, we have called the setter function with the “HB” object and provided “100” units to this function. Then, we have called the “getBill()” function with the very same object to print the total household electricity bill on the terminal. In the same manner, we have called the setter function with the “CB” object and provided “79” units to this function. Finally, we have called the “getBill()” function with the very same object to print the total commercial electricity bill on the terminal.

After saving this example code, we have compiled it with the following command:

$ g++ AbstractBase.cpp –o AbstractBase

Afterward, we have executed this code with the command given below:

$ ./AbstractBase

As a result of executing this example code, the total household and commercial electricity bills were printed accurately on the terminal, as shown in the following image:

Now, we will discuss some scenarios with you that can lead to compilation errors while dealing with the abstract base classes. In the first scenario, we have kept the above code as it is except for a very small change. We have only tried to create an object of the abstract base class within our “main()” function with the statement “ElectricityBill EB”. This is shown in the image below:

After doing this, when we tried to compile the same code, we were presented with an error message, as shown in the following image. The reason behind the production of this error is that we have tried to instantiate an object of the abstract base class, which is strictly not allowed since these classes are only meant for providing a template for the derived classes, i.e., their objects cannot be created in any situation.

Again, in the second scenario, we have kept the above code as it is except for a very small change. We have only commented on the implementation of the pure virtual function within our first derived class. This is shown in the image below:

After doing this, when we tried to compile the same code, we were presented with an error message, as shown in the following image. The sole reason behind the production of this error is that we have skipped the implementation of the pure virtual function in one of the derived classes. That is why this mistake led to a compilation error.

Conclusion:

The main goal of this guide was to throw light on the significance of the abstract base classes in C++ in Ubuntu 20.04. For that, we first talked about the purpose of the abstract base classes in C++, followed by a summary of their important properties. Then, to make this concept clearer, we discussed a thorough C++ example depicting the usage of the abstract base classes. Moreover, we also shared with you some scenarios that can lead to compilation errors while working with the abstract base classes in C++. After going through this guide, you will develop a basic understanding of the usage of the abstract base classes in C++ in Ubuntu 20.04.

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.