No C++ interface keyword then how to create interface in C++?

(Last Updated On: March 27, 2017)

Yes, there is no C++ interface keyword available. To create interface in C++ program, we use pure virtual functions into a class. Generally, we prefix class name with letter “I” as naming convention e.g. “class IMyClass”, to depict Interfaces in c++.

Note that in an interface, we keep only pure virtual functions and we never provide implementation of functions in it. Derived/Extended classes are responsible to implement these pure virtual functions, or we can say that only derived classes have implementation of c++ interfaces.

C++ Interface class example:
 

[What is pure virtual functions in C++?]

How to create interface and use it in C++ program?

Lets see how to implement and create c++ interface. In below c++ interface inheritance example, we have an interface called ILogin  that contains 2 pure virtual functions name() and password(). Also, there are two extended classes that is EmailLogin and MembershipLogin classes. These two derived classes will inherit and implement interface class pure virtual functions.

Note that how C++ interface has been used in client i.e. main() method.

 

Now, lets see how to use interface class in c++ program. Here is the main() function that will use the interface class and functions in a program.