Abstract Class in C++

Last Updated : 9 May 2026

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.

What is an Abstract Class in C++?

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:

  • Pointers of abstract class type
  • References of abstract class type

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.

Syntax

It has the following syntax:

In this syntax,

  • class_name represents the abstract class name.
  • virtual is used to declare a virtual function.
  • = 0 makes the function a pure virtual function.

Example of Abstract Class

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.

Characteristics of Abstract Class

The following are some important characteristics of abstract classes:

  • An abstract class contains at least one pure virtual function.
  • We cannot create objects of an abstract class.
  • Abstract classes can contain normal functions and data members.
  • Abstract classes can have constructors and destructors.
  • Derived classes must implement all pure virtual functions.
  • Abstract classes are mainly used as base classes.
  • Pointers and references of abstract class type can be created.
  • Abstract classes help achieve abstraction and runtime polymorphism.

Restrictions of Abstract Class

The following are some restrictions of abstract classes:

  • An abstract class must contain at least one pure virtual function.
  • Objects of an abstract class cannot be created directly.
  • Derived classes must override all pure virtual functions.
  • If a derived class does not override all pure virtual functions, it also becomes abstract.

Can we make an object of an abstract class?

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.

Importance of Abstraction in Daily Life

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.

Abstract Class Vs. Interface

There are several differences between abstract classes and interfaces in C++. Some main differences are as follows:

InterfaceAbstract 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.

Another Example of 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.


Next TopicC++ Namespaces