Abstract Class, Pure Abstract Class, and Interface in C++ (OOP)

Abstract Class, Pure Abstract Class, and Interface in C++ (OOP)

Introduction to Abstract Classes

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

  1. Abstract Class: A class with at least one pure virtual function. It serves as a blueprint for derived classes but cannot be instantiated directly.
  2. Pure Abstract Class: An abstract class where all member functions are pure virtual.
  3. 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

  1. 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).
  2. 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 (a string) and radius (a double).
  3. 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 (a string), length, and width (both double).

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

  1. Creating Objects:
    • The program creates a circle object c1 with color "Black" and radius 26.
    • It also creates a rectangle object R1 with color "Blue", length 23, and width 45.
  2. 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.

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
  • 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

TypeDefinitionExample
Abstract ClassContains at least one pure virtual function but can have implemented methods.shape in this example.
Pure Abstract ClassAll member functions are pure virtual, no concrete methods allowed.shape (if it had no constructors or members).
Interface ClassSpecial 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.

Leave a Reply

Your email address will not be published. Required fields are marked *