Abstract Classes and Interfaces in C++
In Object-Oriented Programming (OOP), both abstract classes and interfaces are used to define a contract for derived classes, but they have different purposes and characteristics. Let's dive into each one and their usage in C++.
An abstract class is a class that cannot be instantiated on its own and is meant to be a base class for other classes. It typically contains one or more pure virtual functions (i.e., functions that have no implementation in the abstract class itself) which must be overridden in derived classes. The purpose of an abstract class is to define common behavior that can be shared among its derived classes while leaving certain functionality to be implemented by those derived classes.
Cannot be instantiated:
Pure Virtual Functions:
virtual void functionName() = 0;Can have concrete functions:
class AbstractClass {
public:
// Pure virtual function (no implementation)
virtual void pureVirtualFunction() = 0;
// Regular (concrete) function with implementation
void regularFunction() {
cout << "This is a concrete function." << endl;
}
};
#include <iostream>
using namespace std;
// Abstract class
class Shape {
public:
// Pure virtual function
virtual void draw() = 0; // Derived classes must implement this
// Concrete function
void display() {
cout << "Displaying the shape." << endl;
}
};
// Derived class 1
class Circle : public Shape {
public:
// Implementation of the pure virtual function
void draw() override {
cout << "Drawing a Circle." << endl;
}
};
// Derived class 2
class Square : public Shape {
public:
// Implementation of the pure virtual function
void draw() override {
cout << "Drawing a Square." << endl;
}
};
int main() {
// Shape shape; // Error: Cannot instantiate abstract class
Shape* shape1 = new Circle();
Shape* shape2 = new Square();
shape1->draw(); // Output: Drawing a Circle.
shape2->draw(); // Output: Drawing a Square.
shape1->display(); // Output: Displaying the shape.
delete shape1;
delete shape2;
return 0;
}
Shape is an abstract class because it contains the pure virtual function draw().Circle and Square are derived classes that override the draw() function.display() function in Shape is a regular function with implementation, and it can be used by all derived classes.In C++, interfaces are usually implemented using abstract classes that contain only pure virtual functions and no data members or concrete methods. Technically, there is no specific interface keyword in C++, as there is in other languages like Java. However, an interface in C++ is typically just an abstract class that only contains pure virtual functions, and its sole purpose is to define a contract that must be followed by derived classes.
Purely abstract:
No data members:
No concrete methods:
Multiple inheritance:
class InterfaceName {
public:
virtual void functionName() = 0; // Pure virtual function
virtual void anotherFunction() = 0; // Another pure virtual function
};
#include <iostream>
using namespace std;
// Interface (abstract class with only pure virtual functions)
class Drawable {
public:
virtual void draw() = 0; // Pure virtual function
};
// Interface (abstract class with only pure virtual functions)
class Resizeable {
public:
virtual void resize() = 0; // Pure virtual function
};
// A class implementing both Drawable and Resizeable interfaces
class Rectangle : public Drawable, public Resizeable {
public:
void draw() override {
cout << "Drawing a Rectangle." << endl;
}
void resize() override {
cout << "Resizing the Rectangle." << endl;
}
};
int main() {
Rectangle rect;
rect.draw(); // Output: Drawing a Rectangle.
rect.resize(); // Output: Resizing the Rectangle.
return 0;
}
Drawable and Resizeable are interfaces (abstract classes containing only pure virtual functions).Rectangle implements both the Drawable and Resizeable interfaces by providing concrete implementations of the draw() and resize() methods.| Feature | Abstract Class | Interface |
|---|---|---|
| Purpose | Used for shared functionality and behavior among derived classes. | Defines a contract that classes must implement. |
| Methods | Can have both pure virtual and concrete (implemented) methods. | Can only have pure virtual functions (no implementations). |
| Data Members | Can have data members and member functions. | Typically does not have data members. |
| Multiple Inheritance | C++ supports multiple inheritance, so you can inherit from multiple abstract classes. | C++ allows implementing multiple interfaces (abstract classes with only pure virtual functions). |
| Use Case | Used when you need to share common functionality across multiple classes, but allow for customization in derived classes. | Used when you need to enforce that certain classes must provide specific behavior (method implementation). |
Abstract Class:
Interface (Abstract Class with Pure Virtual Functions):
Both abstract classes and interfaces help in achieving abstraction in OOP, ensuring that certain behaviors are defined and implemented in a specific way by derived classes, while leaving other details to be handled later.
Open this section to load past papers