
Abstract Class, Pure Abstract Class, and Interface in C++ (OOP)
Introduction to Abstract Classes
Contents
- Introduction to Abstract Classes
- Key Definitions
- Code Example: Abstract Class with Pure Virtual Functions
- Abstract Class and Derived Classes
- Explanation of the Classes
- Main Function
- Explanation of the main Function
- Output
- Accessing and Understanding Abstract Classes
- Difference Between Abstract, Pure Abstract, and Interface Classes
- Conclusion
- Share this:
- Like this:
- Related
An abstract class in C++ is a class designed to be specifically used as a base class. It cannot be instantiated on its own and typically includes at least one pure virtual function.
A pure virtual function is a function declared within a class that has no implementation relative to the base class and must be implemented by all derived classes.
Abstract classes are crucial in object-oriented programming to enforce a contract for derived classes.
Key Definitions
- Abstract Class: A class with at least one pure virtual function. It serves as a blueprint for derived classes but cannot be instantiated directly.
- Pure Abstract Class: An abstract class where all member functions are pure virtual.
- Interface Class: A class that only contains pure virtual functions and no data members. It defines the “interface” that other classes must implement.
Code Example: Abstract Class with Pure Virtual Functions
Here’s the provided code that demonstrates the concept of abstract classes, including how they are used and enforced through inheritance:
Abstract Class and Derived Classes
#include <iostream>
#include <string>
using namespace std;
class shape { // Abstract base class
public:
virtual double calculatearea() = 0; // Pure virtual function
virtual string getcolor() = 0; // Pure virtual function
};
class circle : public shape { // Derived class
private:
string color;
double radius;
public:
circle(string c, double r) : color(c), radius(r) {} // Constructor
double calculatearea() override {
return 3.14 * radius * radius; // Circle area formula
}
string getcolor() override {
return color;
}
};
class rectangle : public shape { // Derived class
private:
string color;
double length, width;
public:
rectangle(string c, double l, double w) : color(c), length(l), width(w) {} // Constructor
double calculatearea() override {
return length * width; // Rectangle area formula
}
string getcolor() override {
return color;
}
};
Explanation of the Classes
- Abstract Base Class (
shape
):- Contains two pure virtual functions:
calculatearea()
: Requires derived classes to implement area calculation logic.getcolor()
: Requires derived classes to implement color retrieval logic.
- Acts as a blueprint for all shape types (e.g.,
circle
,rectangle
).
- Contains two pure virtual functions:
- Derived Class (
circle
):- Implements the
calculatearea()
function using the formula for a circle’s area (πr2\pi r^2πr2). - Implements the
getcolor()
function to return the color of the circle. - Contains private members:
color
(astring
) andradius
(adouble
).
- Implements the
- Derived Class (
rectangle
):- Implements the
calculatearea()
function using the formula for a rectangle’s area (length×width\text{length} \times \text{width}length×width). - Implements the
getcolor()
function to return the color of the rectangle. - Contains private members:
color
(astring
),length
, andwidth
(bothdouble
).
- Implements the
Main Function
int main() {
circle c1("Black", 26); // Create a circle object
rectangle R1("Blue", 23, 45); // Create a rectangle object
cout << "The area of the circle = " << c1.calculatearea() << endl;
cout << "The color of the circle = " << c1.getcolor() << endl;
cout << "The area of the rectangle = " << R1.calculatearea() << endl;
cout << "The color of the rectangle = " << R1.getcolor() << endl;
return 0;
}
Explanation of the main
Function
- Creating Objects:
- The program creates a
circle
objectc1
with color"Black"
and radius26
. - It also creates a
rectangle
objectR1
with color"Blue"
, length23
, and width45
.
- The program creates a
- Calling Methods:
- For
c1
, the program calls:calculatearea()
: Computes the area of the circle using πr2\pi r^2πr2.getcolor()
: Retrieves the color of the circle.
- For
R1
, the program calls:calculatearea()
: Computes the area of the rectangle using length×width\text{length} \times \text{width}length×width.getcolor()
: Retrieves the color of the rectangle.
- For
Output
The area of the circle = 2120.64
The color of the circle = Black
The area of the rectangle = 1035
The color of the rectangle = Blue
Accessing and Understanding Abstract Classes
- Abstract classes cannot be instantiated directly.
- Example:
shape s; // Invalid
- Example:
- They must be used through inheritance.
- Pure virtual functions enforce the derived classes to implement specific methods.
Difference Between Abstract, Pure Abstract, and Interface Classes
Type | Definition | Example |
---|---|---|
Abstract Class | Contains at least one pure virtual function but can have implemented methods. | shape in this example. |
Pure Abstract Class | All member functions are pure virtual, no concrete methods allowed. | shape (if it had no constructors or members). |
Interface Class | Special form of a pure abstract class with no data members. | Similar to Pure Abstract class. |
Conclusion
- Abstract classes serve as blueprints for creating derived classes, enforcing a set of rules or behaviors through pure virtual functions.
- This tutorial demonstrated how abstract classes allow us to define generalized behavior while leaving the specifics to derived classes.
- Key Takeaways:
- Use abstract classes to enforce a contract for derived classes.
- Use pure virtual functions for mandatory method implementation.
- Implement interface-like behavior in C++ using pure abstract classes.
This makes object-oriented programming more modular, reusable, and maintainable.