Abstract Class in C++

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!
Learn via video course
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
By Prateek Narang
Free
5
Enrolled: 1000
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
Prateek Narang
Free
5
Enrolled: 1000
Start Learning

Overview

Abstract class in C++ refer to classes containing at least one pure virtual function, which cannot be instantiated. Such classes are mainly used for Upcasting, which means that its derived classes can use its interface.

Prerequisites

Introduction

When you are writing Object Oriented Code, one of your main objectives is to structure code so that it is easy to relate to real life. There are two important concepts you need to be familiar with. They are Inheritance and Abstraction.

Let’s look at these, one by one.

Inheritance

You must have seen some families where all the members of the same family have the same eye color. Ever wondered why? Well, it is because of inheritance. It is a biological concept that tells us that children get (inherit) some of the features (like eye color and hairstyle) from their parents.

Abstract Class in C++ - Inheritance

But what does this have to do with this article? Well, in Object-Oriented Programming, we use this concept extensively. We create Base Class(parent class) and Derived Class(Child class). Here also, the derived class inherits its base class's features (in this case, they are methods and properties). Learn more about Inheritance in C++.

Abstraction

Abstraction removes the implementation details and gives users a simple way to interact with things. With the help of abstraction, a programmer hides all the implementation details that the user does not need to see.

Let’s say you are driving a car. When you increase the speed, do you ever think about what is happening inside the engine? Or, when you shift gears, do you know how the gearbox works? A better question would be do you even need to know?

The same goes for many things we do daily. When we switch on the TV, we press a button from the remote trusting the remote to do its job, and we don’t care how it does what it does.

This is called abstraction. Learn more about Abstraction in C++.

Abstract Class in C++

An abstract class in C++ is a class that contains at least one pure virtual function, and these classes cannot be instantiated.

Abstract Classes came from the idea of abstraction. Before we dive into technicalities, let’s first consider a scenario where you might use this concept.

Abstract Class in C++ Example

Let’s say we are making a calculator which returns the perimeter of the shape we put in. Think of what kind of code you would write for such a calculator. You might start with some initial shapes and hardcode the perimeter by making separate functions inside the Shape class.

The class might look something like this –

This will work, but OOP suggests that we should try to stay close to real-world logic. So, we can make a class Shape as a parent class and then make separate classes like Square and Rectangle as child classes. This will make the code easier to maintain, and if you plan to add anything new in the future, then you can add that in the child class. To implement this feature, we need to use Abstract Classes. Abstract classes in C++ need at least one pure virtual function in a class. The classes that inherit the abstract class must define the pure virtual function; otherwise, the subclass will become an abstract class.

Restrictions on Abstract Classes

There are some restrictions on abstract classes in C++.

Abstract classes cannot be used for the following –

  • Variables or member data
  • Argument types
  • Function return types
  • Types of explicit conversions.

The constructors of the abstract class in c++ can call other member functions, but if they directly or indirectly call the pure virtual function, then the result is undefined.

But wait! What is a pure virtual function anyway?

To understand the pure virtual function, first, let’s understand virtual functions.

A virtual function in C++ is a member function declared within a base class and redefined by the derived class.

A pure virtual function (or abstract function) is a virtual function with no definition/logic. It is declared by assigning 0 at the time of declaration.

Let’s take a look at an example that will make things clear.

Output –

In the above code, you can see that the function perimeter() is a pure virtual function, the “virtual” keyword is used, and it is assigned a value of 0.

In the derived classes, Rectangle and Shape redefine the pure virtual function.

Characteristics of Abstract Class in C++

  1. Abstract Classes must have at least one pure virtual function.
  1. Abstract Classes cannot be instantiated, but pointers and references of Abstract Class types can be created. You cannot create an object of an abstract class. Here is an example of a pointer to an abstract class.

Output –

  1. Abstract Classes are mainly used for Upcasting, which means its derived classes can use its interface.
  2. Classes that inherit the Abstract Class must implement all pure virtual functions. If they do not, those classes will also be treated as abstract classes.

Why Can’t We Make an Abstract Class Object?

Abstract classes in C++ cannot be instantiated because they are “abstract”. It’s like someone is telling you to draw an animal without telling you which specific animal. You can only draw if the person asks you to draw a cat, dog, etc., but an animal is abstract.

The same is the case with abstract classes in C++, so we can’t create objects, but we can create a pointer of an abstract class.

Conclusion

Through this article, you now know:

  • What is Abstract Class in C++?
  • What is Inheritance?
  • What is Abstraction?
  • How to create and work with Abstract Class in C++?
  • What are virtual functions, and what is the difference between virtual and pure virtual functions?

That’s all for now, folks!

Thanks for reading.