C++ Program to Create an Interface Last Updated : 05 Nov, 2024 Suggest changes Share Like Article Like Report 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++.Implementation of Interface in C++To create an interface, first we need to understand the rules that governs the behaviour of interface:Interface only contains the declaration of the methods not definition.Interface methods must be declared public.Any class can implement/inherit any interface.The implementing class must provide the definition of the methods.Instance of interface cannot be created.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:All methods of pure abstract class are only declared but not defined (pure virtual methods).Its methods can be defined either public, protected or private.Any class can inherit pure abstract class.The implementing class must provide the definition of the methods otherwise it will become abstract class.Instance of pure virtual class cannot be created.As we can see, we can almost perfectly simulate the Java Interfaces with C++ Pure Abstract Classes.Example C++ #include <bits/stdc++.h> using namespace std; // Interface equivalent pure abstract class class I { public: virtual string getName() = 0; }; // Class B which inherits I class B : public I { public: string getName() { return "GFG"; } }; // Class C which inherits I class C : public I { public: string getName() { return "GeeksforGeeks"; } }; int main() { B obj1; C obj2; I *ptr; // Assigning the address of obj1 to ptr ptr = &obj1; cout << ptr->getName() << endl; // Assigning the address of obj2 to ptr ptr = &obj2; cout << ptr->getName(); return 0; } OutputGFG 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. Advertise with us Next Article C++ Program to Create an Interface A akhilvasabhaktula03 Follow Similar Reads How to Create a Static Library in C++? C++ allows users to add their own libraries to reduce the amount of code we have to write in our program. A static library in C++ is a library that is linked to the program at compile-time. In this article, we will learn how to create a static library in C++ from scratch. Create Static Library in C+ 3 min read How to Create a Pointer to a Function in C++? In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use 2 min read How Do I Create a Library in C++? Libraries are reusable code packages that can be imported into our program to use the code defined in them. In C++, libraries can be either static or dynamic. A static library is a library that is linked to the program at compile-time whereas dynamic libraries in C++ are linked at runtime but they h 4 min read Can I Mix C and C++ Code in the Same Project? C and C++ both are very popular programming languages. There can be many situations when you might have to export some functionalities that are written in C programming language into your C++ code. Now the biggest question arises while making any project in C and C++ is that- "Can we Mix C and C++ C 3 min read Structure of C++ Program The C++ program is written using a specific template structure. The structure of the program written in C++ language is as follows: Documentation Section:This section comes first and is used to document the logic of the program that the programmer going to code.It can be also used to write for purpo 5 min read Article Tags : C++ Programs C++ cpp-virtual cpp-inheritance C++ Misc Programs CPP-OOPs +2 More Practice Tags : CPP Like