C++ Program to Create an Interface Last Updated : 05 Nov, 2024 Comments Improve Suggest changes 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. Comment More infoAdvertise with us Next Article C++ Program to Create an Interface A akhilvasabhaktula03 Follow Improve Article Tags : C++ Programs C++ cpp-virtual cpp-inheritance C++ Misc Programs CPP-OOPs +2 More Practice Tags : CPP 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 Setting up a C++ Competitive Programming Environment In this article, we will learn about how to setup all in one Competitive Programming Environment Operating System It is always recommended to use a Linux based OS. It is so because not only you will learn some better know-hows of the system but also will be able to get some pre-installed coding tool 5 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 How to Implement Interfaces Using Abstract Class in C++ C++ doesn't have built-in functionality of interfaces like other programming languages such as Java. However, interfaces can be implemented in C++ using the abstract classes and pure virtual functions. In this article, we will learn how to implement interfaces using abstract classes in C++.Implement 4 min read Creating a C++ reusable Header File and its Implementation Files Reusability is one of the most important concepts of Software Engineering. Reusability means developing code that can be reused either in the same program or in different programs. C++ allows reusability through inheritance, containership, polymorphism, and genericity. But, there is another way to d 4 min read How to Create a Class with Constructors and Destructors in C++? In C++, a class is a user-defined data type encapsulating data members and member functions. The constructors and destructors are special member functions of a class used for initializing objects and cleaning up resources respectively. In this article, we will discuss how to create a class with cons 2 min read How to Find the Type of an Object in C++? In C++, every variable and object has a type. The type of an object determines the set of values it can have and what operations can be performed on it. Itâs often useful to be able to determine the type of an object at runtime, especially when dealing with complex codebases. In this article, we wil 2 min read Like