Back to C++
2026-03-245 min read

C++ Abstract Class and Pure Virtual Function

Learn C++ Abstract Class and Pure Virtual Function step by step with clear examples and exercises.

Why This Matters

In this full guide on C++ Abstract Classes and Pure Virtual Functions, we will delve into the essential concepts that are crucial for mastering object-oriented programming (OOP) in C++. By understanding these topics, you'll be well-equipped to develop robust, flexible code, excel in coding interviews, real-world projects, and debug common issues.

Why This Matters

In OOP, abstract classes and pure virtual functions play a pivotal role in creating polymorphic behavior, which enables objects of different types to be treated as objects of a common base class. Polymorphism is a powerful feature that enhances code reusability, flexibility, and maintainability. Understanding these concepts is indispensable for mastering C++ and developing robust, flexible code.

Prerequisites

Before diving into abstract classes and pure virtual functions, you should have a solid understanding of the following topics:

  • Basic C++ syntax (variables, constants, operators, loops, functions)
  • Object-oriented programming concepts (classes, objects, inheritance, polymorphism)
  • Virtual functions in C++

Core Concept

Abstract Classes

An abstract class is a class that cannot be instantiated and serves as a base for other classes. It contains at least one pure virtual function, which means the function declaration includes = 0 in its definition. Since an abstract class does not provide an implementation for the pure virtual functions, any derived class must provide an implementation for these functions.

#include <iostream>
using namespace std;

class Shape { // Abstract Class
public:
virtual void draw() = 0; // Pure Virtual Function

void move(int x, int y) {
cout << "Moving the shape to (" << x << ", " << y << ")" << endl;
}
};

Pure Virtual Functions

A pure virtual function is a function that does not have an implementation in the base class and must be provided by any derived class. When a class contains at least one pure virtual function, it becomes an abstract class and cannot be instantiated.

class Circle : public Shape { // Derived Class
public:
void draw() override {
cout << "Drawing a circle" << endl;
}
};

Abstract Member Functions

In addition to pure virtual functions, an abstract class can also have non-pure virtual functions. These functions provide a default implementation that derived classes may choose to inherit or override. However, having at least one pure virtual function is what makes a class abstract.

class Shape { // Abstract Class
public:
virtual void draw() = 0; // Pure Virtual Function

void move(int x, int y) {
cout << "Moving the shape to (" << x << ", " << y << ")" << endl;
}

virtual void resize() {
cout << "Resizing the shape" << endl;
}
};

Worked Example

Let's create an example using abstract classes and pure virtual functions to represent different shapes (Circle, Square, Rectangle) that can be drawn.

#include <iostream>
using namespace std;

class Shape { // Abstract Class
public:
virtual void draw() = 0;

void move(int x, int y) {
cout << "Moving the shape to (" << x << ", " << y << ")" << endl;
}
};

class Circle : public Shape { // Derived Class
public:
void draw() override {
cout << "Drawing a circle" << endl;
}
};

class Square : public Shape { // Derived Class
public:
void draw() override {
cout << "Drawing a square" << endl;
}
};

int main() {
Circle c;
c.draw();
c.move(5, 7);

Square s;
s.draw();
s.move(10, 12);

return 0;
}

Common Mistakes

  1. Forgetting to declare a function as pure virtual in the abstract class: If a function is not declared as pure virtual, the base class can be instantiated, which defeats the purpose of an abstract class.
  1. Not providing an implementation for a pure virtual function in derived classes: Any derived class must provide an implementation for all pure virtual functions inherited from the abstract base class.
  1. Misusing abstract classes: Abstract classes should only be used when there is a need to define a common interface for a group of related classes, and at least one pure virtual function is required.

Common Mistakes (Continued)

  1. Incorrectly overriding non-virtual functions in derived classes: When overriding a non-virtual function in a derived class, the base class implementation will be hidden, so it's essential to understand the consequences of hiding base class functionality.
  1. Not understanding the difference between abstract and concrete classes: Concrete classes are regular classes that can be instantiated, while abstract classes cannot. Abstract classes provide a common interface for derived classes, whereas concrete classes implement this interface.

Practice Questions

  1. Create an abstract class Animal with a pure virtual function sound(). Derive two classes Dog and Cat, each providing their own implementation of the sound() function.
  1. Modify the previous example to include a Rectangle derived class, and create a program that can draw shapes using polymorphism.
  1. Create an abstract class Vehicle with pure virtual functions accelerate() and brake(). Derive two classes Car and Motorcycle, each providing their own implementation of the accelerate() and brake() functions.

FAQ

  1. What happens if I try to instantiate an abstract class? Attempting to instantiate an abstract class will result in a compile-time error because it does not provide an implementation for at least one pure virtual function.
  1. Can I have non-pure virtual functions in an abstract class? Yes, you can have non-pure virtual functions in an abstract class. However, having at least one pure virtual function is what makes a class abstract.
  1. What's the purpose of using abstract classes and pure virtual functions? The primary goal is to create a common interface for a group of related classes, ensuring that all derived classes implement certain functions in a consistent manner, thus achieving polymorphism. This allows objects of different types to be treated as objects of a common base class, enhancing code reusability, flexibility, and maintainability.
  1. Can I call a pure virtual function directly on an abstract class object? No, since the implementation is provided by derived classes, you cannot call a pure virtual function directly on an abstract class object. Instead, you should create an instance of a derived class and call the function on that object.
  1. Is it possible to have multiple inheritance with abstract classes in C++? Yes, multiple inheritance is allowed with abstract classes in C++. However, care must be taken to avoid diamond inheritance, where a single derived class inherits from two or more base classes with the same name for a member function or variable. This can lead to ambiguities and compile-time errors.
C++ Abstract Class and Pure Virtual Function | C++ | XQA Learn