C++ Abstract Class and Pure Virtual Function
In C++, an abstract class is a class that cannot be instantiated directly. It serves as a base for other classes and is typically used to define a common interface for derived classes. The abstract class may contain pure virtual functions, which are functions declared in the base class but are not implemented. Derived classes are required to override these pure virtual functions to provide their own specific implementations.
Abstract classes and pure virtual functions are crucial concepts in object-oriented programming (OOP), helping to define a contract for derived classes and enabling polymorphism.
An abstract class in C++ is a class that cannot be instantiated on its own. It is designed to be inherited by other classes that provide specific implementations of the abstract class's pure virtual functions.
class AbstractClass {
public:
virtual void someFunction() = 0; // Pure virtual function
};
= 0
after the function declaration makes it a pure virtual function.A pure virtual function is a function that is declared in the base class but does not have any implementation. It forces derived classes to provide an implementation for that function. A class containing at least one pure virtual function is called an abstract class.
class Base {
public:
virtual void functionName() = 0; // Pure virtual function
};
= 0
syntax after the function declaration marks it as a pure virtual function.Let’s look at a basic example to understand how an abstract class and pure virtual functions work in C++.
#include <iostream>
using namespace std;
// Abstract class with pure virtual function
class Shape {
public:
// Pure virtual function
virtual void draw() = 0; // No implementation, forcing derived classes to provide one
// A regular function
void display() {
cout << "This is a shape." << endl;
}
};
// Derived class implementing the pure virtual function
class Circle : public Shape {
public:
void draw() override { // Overriding the pure virtual function
cout << "Drawing a circle!" << endl;
}
};
// Another derived class implementing the pure virtual function
class Square : public Shape {
public:
void draw() override { // Overriding the pure virtual function
cout << "Drawing a square!" << endl;
}
};
int main() {
// Shape shape; // Error: cannot instantiate an abstract class
Shape* shape1 = new Circle(); // Create a Circle object using Shape pointer
Shape* shape2 = new Square(); // Create a Square object using Shape pointer
shape1->draw(); // Output: Drawing a circle!
shape2->draw(); // Output: Drawing a square!
delete shape1;
delete shape2;
return 0;
}
Explanation:
Shape
class is an abstract class because it contains a pure virtual function draw()
. This forces any derived class to implement the draw()
function.Circle
and Square
classes override the draw()
function, providing their own specific implementations.Shape
directly, but you can create pointers to Shape
and assign them to objects of derived classes (Circle
and Square
).draw()
function is called based on the actual object type, demonstrating polymorphism.Enforcing a Common Interface:
Code Reusability:
Polymorphism:
Separation of Interface and Implementation:
In C++, a derived class can inherit from multiple abstract classes. If any of the base classes contains pure virtual functions, the derived class must implement those functions.
#include <iostream>
using namespace std;
// First abstract class
class Printable {
public:
virtual void print() = 0; // Pure virtual function
};
// Second abstract class
class Scannable {
public:
virtual void scan() = 0; // Pure virtual function
};
// Derived class inheriting from both abstract classes
class MultiFunctionPrinter : public Printable, public Scannable {
public:
void print() override {
cout << "Printing document." << endl;
}
void scan() override {
cout << "Scanning document." << endl;
}
};
int main() {
MultiFunctionPrinter mfp;
mfp.print(); // Output: Printing document.
mfp.scan(); // Output: Scanning document.
return 0;
}
Explanation:
Printable
and Scannable
are two abstract classes, each with a pure virtual function (print()
and scan()
respectively).MultiFunctionPrinter
inherits from both abstract classes and provides implementations for both print()
and scan()
.