In C++, the concept of an interface is not a built-in feature like in some other languages (e.g., Java or C#), but it can be achieved using abstract classes. An interface in C++ is typically represented as a class that has only pure virtual functions and no data members or implemented functions. This ensures that the derived classes are forced to implement the methods defined in the interface.
While an abstract class can have both pure virtual functions and regular member functions, an interface is typically a class that only contains pure virtual functions and no data members or concrete (implemented) functions.
Pure Virtual Functions: An interface in C++ is a class that contains only pure virtual functions. These are functions declared with = 0 at the end of the function declaration, and they have no implementation in the interface itself.
No Data Members: An interface class should not contain any data members (variables), as it is meant to specify behavior (i.e., method signatures), not state.
No Concrete Methods: Unlike abstract classes, interfaces typically do not have any concrete methods with implemented code. All functions in an interface are pure virtual functions.
Inheritance: Any class that wants to implement the interface must inherit from it and provide concrete implementations of the pure virtual functions.
Multiple Inheritance: In C++, a class can inherit from multiple interfaces, which allows a class to implement more than one set of behaviors (interfaces).
class IInterface {
public:
// Pure virtual function (Interface method)
virtual void method1() = 0;
virtual void method2() = 0;
// Destructor (optional, if needed)
virtual ~IInterface() = default;
};
A class that implements an interface must provide concrete implementations for all the pure virtual functions declared in the interface. If the class fails to do so, it will also be considered an abstract class and cannot be instantiated.
class MyClass : public IInterface {
public:
// Implementing method1 from IInterface
void method1() override {
cout << "method1 implementation" << endl;
}
// Implementing method2 from IInterface
void method2() override {
cout << "method2 implementation" << endl;
}
};
Let's walk through an example where we define an interface Drawable and then create two classes (Circle and Rectangle) that implement the Drawable interface.
#include <iostream>
using namespace std;
// Interface (Abstract class with pure virtual functions)
class Drawable {
public:
// Pure virtual function (Interface method)
virtual void draw() = 0;
virtual double area() = 0;
// Virtual destructor (optional)
virtual ~Drawable() = default;
};
// Class Circle implementing Drawable interface
class Circle : public Drawable {
private:
double radius;
public:
Circle(double r) : radius(r) {}
void draw() override {
cout << "Drawing a Circle" << endl;
}
double area() override {
return 3.14159 * radius * radius; // Area of circle
}
};
// Class Rectangle implementing Drawable interface
class Rectangle : public Drawable {
private:
double width, height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
void draw() override {
cout << "Drawing a Rectangle" << endl;
}
double area() override {
return width * height; // Area of rectangle
}
};
int main() {
// Creating objects of derived classes
Drawable* shape1 = new Circle(5); // Pointer of type Drawable pointing to Circle
Drawable* shape2 = new Rectangle(4, 6); // Pointer of type Drawable pointing to Rectangle
shape1->draw(); // Calls Circle's draw method
cout << "Circle Area: " << shape1->area() << endl;
shape2->draw(); // Calls Rectangle's draw method
cout << "Rectangle Area: " << shape2->area() << endl;
// Cleanup
delete shape1;
delete shape2;
return 0;
}
Drawable Interface: The Drawable class defines the interface with two pure virtual functions: draw() and area(). These methods must be implemented by any class that inherits from Drawable.
Circle and Rectangle Classes: Both Circle and Rectangle classes inherit from Drawable and implement the draw() and area() methods. These classes provide their own concrete implementations for the methods defined in the interface.
Main Function: The main function creates pointers to the Drawable interface type, which are then assigned to objects of type Circle and Rectangle. This demonstrates polymorphism, as the correct method is called based on the actual object type (either Circle or Rectangle), not the pointer type (Drawable).
Multiple Inheritance:
Polymorphism:
Separation of Interface and Implementation:
Improved Code Reusability and Extensibility:
Better Design:
No Default Behavior:
Cannot Have Data Members:
Complexity with Multiple Inheritance:
Open this section to load past papers