An abstract class in C++ is a class that is designed to serve as a base or blueprint for other classes but cannot be instantiated itself. It contains one or more pure virtual functions, which are virtual functions without any implementation in the abstract class. An abstract class provides an interface for its derived classes to inherit from and implement the pure virtual functions according to their specific needs.
Here's an example of an abstract class in C++:
#include <iostream>
// Abstract class
class Shape {
public:
// Pure virtual function
virtual void draw() = 0;
// Non-virtual function
void print() {
std::cout << "Printing from the Shape class." << std::endl;
}
};
// Derived class 1
class Circle : public Shape {
public:
void draw() override {
std::cout << "Drawing a circle." << std::endl;
}
};
// Derived class 2
class Rectangle : public Shape {
public:
void draw() override {
std::cout << "Drawing a rectangle." << std::endl;
}
};
int main() {
// Shape shape; // Error: Cannot instantiate an abstract class
Circle circle;
circle.draw(); // Output: Drawing a circle.
circle.print(); // Output: Printing from the Shape class.
Rectangle rectangle;
rectangle.draw(); // Output: Drawing a rectangle.
rectangle.print(); // Output: Printing from the Shape class.
return 0;
}In this example, the Shape class is an abstract class with a pure virtual function draw(). It cannot be instantiated because of the presence of the pure virtual function.
The Circle and Rectangle classes are derived from the Shape class and provide their own implementation of the draw() function. They must override the pure virtual function from the base class to become concrete classes.
In the main() function, we create objects of the Circle and Rectangle classes. We can call the draw() function on these objects, which will execute the appropriate implementation based on their respective types.
Abstract classes are useful when we want to define a common interface for a group of related classes but do not want to allow instances of the abstract class itself. It provides a way to enforce a specific set of functions that derived classes must implement while allowing common functionality to be defined in the abstract class.
Overall, the advantages of using abstract classes, such as providing a common interface, promoting code reusability, and enabling polymorphism, often outweigh the disadvantages. However, it's important to carefully design and use abstract classes based on the specific requirements and structure of your program.
Note that a class becomes abstract when it contains at least one pure virtual function, even if it also has other non-pure virtual functions or member variables.