What is a C++ abstract class?

An abstract class is like a basic plan for other classes. It makes sure that all related classes follow the same rules but can still work in their own way. For example, think of different animals. All animals make a sound, but the sound is different for each one. An abstract class called Animal can make sure every animal has a makeSound() function, but each animal, like a dog or a cat, can decide what sound it makes.

What is an abstract class?

An abstract class in C++ cannot be instantiatedInstantiated means creating a specific instance or object of a class. and typically contains at least one pure virtual function. Its purpose is to provide a common interface for derived classes.

Now that we understand the purpose of an abstract class, let’s explore the key features that make it unique.

Key characteristics of abstract class

  • Contains at least one pure virtual functionDeclared in a class using virtual and = 0.

  • Provides a base for derived classes to inherit and implement missing functionality.

  • Helps enforce a consistent interface across multiple derived classes.

Syntax of abstract class

Here’s a basic syntax for an abstract class in C++:

class AbstractClass {
public:
virtual void pureVirtualFunction() = 0; // Pure virtual function
};

What is a pure virtual function?

A pure virtual function is a function declared within a class and marked with virtual and = 0. It has no implementation in the base class and must be implemented in all derived classes.

Why do we need pure virtual function?

A pure virtual function is a way to ensure that all related classes must have a specific function, but each can do it in its unique way. For example, in a zoo, every animal can make a sound, but the sound differs: a dog barks, a cat meows, and a bird chirps. We create a rule in the base class (like "Animal") that says, "Every animal must know how to make a sound." This rule is the pure virtual function. It ensures that any new animal added, like an elephant or a lion, must have its own way of making a sound, keeping everything organized and consistent.

Example of abstract class with pure virtual function

Below is a practical example of using an abstract class:

#include <iostream>
#include <vector>
// Abstract class
class Animal {
public:
virtual void speak() = 0; // Pure virtual function
virtual ~Animal() {} // Virtual destructor
};
// Derived class: Dog
class Dog : public Animal {
public:
void speak() override {
std::cout << "The dog says: Woof!" << std::endl;
}
};
// Derived class: Cat
class Cat : public Animal {
public:
void speak() override {
std::cout << "The cat says: Meow!" << std::endl;
}
};
int main() {
std::vector<Animal*> animals; // Vector of Animal pointers
animals.push_back(new Dog()); // Adding a Dog object
animals.push_back(new Cat()); // Adding a Cat object
for (Animal* animal : animals) {
animal->speak(); // Polymorphic call to speak
}
// Clean up memory
for (Animal* animal : animals) {
delete animal;
}
return 0;
}

Code explanation:

The code shows how an abstract class Animal is used to define a common interface for all derived classes (Dog and Cat). Each derived class implements the speak() function to produce behavior specific to the animal type.

Try this: Create a new derived class, such as Bird, and implement the speak() function to print "The bird says: Chirp!". Add a Bird object to the animals vector in the main() function and observe the output.

Let's discuss the advantages and disadvantages of abstract class:

Pros and cons of abstract class

Pros of Abstract Class

Cons of Abstract Class

It forces derived classes to implement specific functions, ensuring consistency across different classes.

Abstract classes introduce an extra layer of complexity, especially in larger projects with many classes.

Common code can be written in an abstract class, reducing code duplication in derived classes.

Since derived classes are forced to implement abstract functions, they may be restricted in terms of flexibility.

Abstract classes provide a single place to make changes that affect multiple derived classes, improving maintainability.

If classes don’t have a common base behavior, an abstract class might not be the best solution.

Key takeaways

  • Abstract classes provide a way to define a common interface for derived classes while allowing each class to implement its specific behavior.

  • They ensure consistency and enforce contracts that all derived classes must follow.

  • However, abstract classes cannot be instantiated directly and may introduce complexity and design overhead.

  • Polymorphism and code reuse are significant advantages when using abstract classes.

Start your programming journey with our Learn C++ Course. This course offers step-by-step guidance, hands-on examples, and practical exercises to help you build a strong foundation in C++. Looking to go from beginner to expert? Explore the Become a C++ Programmer Path. This skill path covers everything from the basics of C++ to advanced programming concepts, with the skills needed to excel in real-world projects.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is an abstract class in C++?

An abstract class in C++ is a class that cannot be instantiated directly. It contains at least one pure virtual function, which must be overridden by any derived class. It serves as a blueprint for derived classes, ensuring they implement certain methods.


Why do we need pure virtual functions in abstract classes?

Pure virtual functions enforce that derived classes implement specific functions. This ensures consistency and a common structure across different derived classes, making the code easier to maintain and extend.


Can we create objects of an abstract class?

No, we cannot create objects of an abstract class because it contains pure virtual functions. Abstract classes are designed to be inherited by other classes that provide implementations for these functions.


What is the difference between an abstract class and an interface?

An abstract class can have both pure virtual functions and regular methods with implementations, whereas an interface (in languages like Java or C#) typically contains only method declarations with no implementation. In C++, an abstract class is similar to an interface when it only contains pure virtual functions.


When should I use an abstract class?

Use an abstract class when you want to define a common interface for a group of related classes, but you also want to provide shared functionality in the base class. It’s useful when you want to enforce that derived classes implement specific methods, ensuring consistency across them.


What happens if you forget to define the pure virtual function in a derived class?

If you forget to define a pure virtual function in a derived class, the derived class also becomes abstract, meaning you cannot create objects of it until all pure virtual functions are implemented.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved