Interfaces - Rybd04/2143-OOP GitHub Wiki
Typically represented using an abstract class that only contains pure virtual functions. It defines a set of behaviors (functions) that any derived class must implement, without providing any default implementation.
#include <iostream>
using namespace std;
// Interface (pure abstract class)
class Printable {
public:
virtual void print() = 0; // Pure virtual function
virtual void info() = 0; // Pure virtual function
};
// Concrete class implementing the interface
class Document : public Printable {
public:
void print() override {
cout << "Printing Document..." << endl;
}
void info() override {
cout << "This is a document." << endl;
}
};
// Another class implementing the same interface
class Image : public Printable {
public:
void print() override {
cout << "Printing Image..." << endl;
}
void info() override {
cout << "This is an image." << endl;
}
};
int main() {
Document doc;
Image img;
doc.print(); // Printing Document...
doc.info(); // This is a document.
img.print(); // Printing Image...
img.info(); // This is an image.
return 0;
}
https://github.com/user-attachments/assets/3962e374-ba34-4792-b895-02b063b5e9dd https://www.geeksforgeeks.org/cpp-program-to-create-an-interface/