An abstract class in C++ is used to provide a common base for other classes. It contains at least one pure virtual function and helps achieve abstraction and runtime polymorphism.
In this chapter, you will learn about abstract classes in C++, their syntax, features, restrictions, and examples using pure virtual functions.
In C++, an abstract class is a class that contains at least one pure virtual function. A pure virtual function is declared using = 0.
An abstract class is mainly used as a base class for other classes. It provides common functions and rules that derived classes must follow.
We cannot create objects of an abstract class directly. However, we can create:
Derived classes must implement all pure virtual functions of the abstract class. If they do not implement them, the derived class also becomes an abstract class.
Abstract classes are useful when we want to define common behavior for multiple related classes.
It has the following syntax:
In this syntax,
In the following example, the Car class is an abstract class that contains a pure virtual function named startEngine().
Output:
Starting engine of BMW... Most cars use Petrol or Diesel. Starting engine of Audi... Most cars use Petrol or Diesel.
Explanation:
In this example, the Car class contains a pure virtual function startEngine() and a normal function fuelType(). The BMW and Audi classes inherit the Car class and provide their own implementation of the startEngine() function.
A base class pointer is used to call the overridden functions at runtime, which demonstrates runtime polymorphism.
The following are some important characteristics of abstract classes:
The following are some restrictions of abstract classes:
No, we cannot make the object of the abstract class because an abstract class cannot be instantiated. So, when we construct a pure virtual function, it reserves some space in the memory for it, but we do not put any address, and because of it, the compiler does not allow us to create an object for it. Thus, we can just make the abstract class, but we cannot instantiate it.
The ATM machine is another example of abstraction in everyday life. However, we all use the ATM to do tasks like cash withdrawals, money transfers, generating mini-statements, and so on, and we have no access to the ATM's internal data. Data protection techniques like data abstraction can prevent unauthorized access to data.
There are several differences between abstract classes and interfaces in C++. Some main differences are as follows:
| Interface | Abstract class |
|---|---|
| An interface can only inherit from another interface. | With the Extended keyword, an abstract class can enforce an interface and inherit from another class. |
| Use of the implements keyword is required in order to implement an interface. | Use the extends keyword to inherit from an abstract class. |
In the following example, the Shape class contains functions to calculate the area of a rectangle and a square using given dimensions.
Output:
The area of rectangle is: 50 The area of square with side 4 is: 16
Explanation:
In this example, the Shape class contains functions to set dimensions and calculate the area of a rectangle and a square. The width() and height() functions are used to set rectangle dimensions, while areaOfRectangle() and areaOfSquare() are used to calculate the areas.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India