5.5) Abstract classes and Pure virtual functions
Table of Contents
Abstract Classes
An abstract class is a class that cannot be instantiated directly and is designed to be a blueprint for other classes. It serves as a foundation for creating derived classes that share a common interface and certain behaviors.
Abstract classes are used to define a set of methods that derived classes must implement, but they themselves may contain some default implementations or properties.
Key Points
- Abstract classes cannot be instantiated.
- Abstract classes may contain regular member variables and methods with implementations.
- Abstract classes typically include one or more pure virtual functions, which are functions without implementations, to be overridden by derived classes.
- An abstract class can have constructors and destructors, which are still called when a derived class object is created or destroyed.
Pure Virtual Functions
A pure virtual function is a virtual function declared in an abstract class without providing an implementation. This function acts as a placeholder, indicating that any derived class must provide its own implementation for this function. The presence of a pure virtual function makes the containing class abstract, meaning it cannot be instantiated on its own.
Key Points
- Pure virtual functions are declared using the
virtual
keyword and followed by= 0;
(no implementation). - Derived classes are required to override pure virtual functions and provide their own implementations.
- Pure virtual functions create a contract that derived classes must adhere to, ensuring a common interface.
- Once a class contains a pure virtual function, it becomes an abstract class.
Example
Here’s a simple example to illustrate abstract classes and pure virtual functions:
#include <iostream>
class Shape {
public:
virtual float area() = 0; // Pure virtual function
void display() {
std::cout << "This is a shape." << std::endl;
}
};
class Circle : public Shape {
public:
float area() override {
return 3.14f * radius * radius;
}
private:
float radius = 5.0f;
};
class Rectangle : public Shape {
public:
float area() override {
return length * width;
}
private:
float length = 4.0f;
float width = 3.0f;
};
int main() {
Circle circle;
Rectangle rectangle;
Shape* shapes[2];
shapes[0] = &circle;
shapes[1] = &rectangle;
for (int i = 0; i < 2; ++i) {
std::cout << "Area: " << shapes[i]->area() << std::endl;
shapes[i]->display();
}
return 0;
}
Output:
Area: 78.5
This is a shape.
Area: 12
This is a shape.
In this example, Shape
is an abstract class with a pure virtual function area()
. Both Circle
and Rectangle
inherit from Shape
and provide their own implementations for the area()
function.
Benefits:
- Abstract classes ensure a consistent interface for related classes.
- Pure virtual functions enforce that derived classes must implement certain behavior.
- Polymorphism is achieved using base class pointers, allowing flexibility in handling different derived classes.
Abstract classes and pure virtual functions are essential tools for creating well-structured, extensible, and polymorphic object-oriented designs in C++.