![]() |
VOOZH | about |
Interfaces are a feature of Java that allows us to define an abstract type that defines the behaviour of a class. In C++, there is no concept of interfaces, but we can create an interface-like structure using pure abstract classes. In this article, we will learn how to create interface-like structure in C++.
To create an interface, first we need to understand the rules that governs the behaviour of interface:
All of these rules can be followed by C++ pure abstract class that only contains the pure virtual functions. Following are the rules that governs the behaviour of pure abstract class:
As we can see, we can almost perfectly simulate the Java Interfaces with C++ Pure Abstract Classes.
GFG GeeksforGeeks
Explanation: The class I acts as an interface, declaring the pure virtual function getName() which must be implemented by any class that inherits from it. Classes B and C inherit from I and provide their own specific implementations of the getName() function, returning different strings.