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.
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
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 function Declared 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 classclass Animal {public:virtual void speak() = 0; // Pure virtual functionvirtual ~Animal() {} // Virtual destructor};// Derived class: Dogclass Dog : public Animal {public:void speak() override {std::cout << "The dog says: Woof!" << std::endl;}};// Derived class: Catclass Cat : public Animal {public:void speak() override {std::cout << "The cat says: Meow!" << std::endl;}};int main() {std::vector<Animal*> animals; // Vector of Animal pointersanimals.push_back(new Dog()); // Adding a Dog objectanimals.push_back(new Cat()); // Adding a Cat objectfor (Animal* animal : animals) {animal->speak(); // Polymorphic call to speak}// Clean up memoryfor (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++?
Why do we need pure virtual functions in abstract classes?
Can we create objects of an abstract class?
What is the difference between an abstract class and an interface?
When should I use an abstract class?
What happens if you forget to define the pure virtual function in a derived class?
Free Resources