C++ Interface with Examples

The C++ programming language offers various features and functionalities to the programmers. It also supports object-oriented programming which is a must when you are developing something. If you have learned C programming, then it will be easy for you to learn C++ programming. Because in C++, some concepts are the same as C programming. In this tutorial, we are going to discuss a concept of C++ named Interface.

What are Interfaces in C++

In C++, there is a way to describe the behaviour of a class without committing to a particular implementation of that class. This feature is offered by C++ objects and classes. Using abstract classes, you can implement the C++ interfaces.

Let me tell you that data abstraction and abstract classes are not the same. Data abstraction is all about keeping important details separate from associated data. You can say that interfaces and abstract classes convey the same idea.

Pure Virtual Functions

Abstract class is nothing but a class with a pure virtual function.

Virtual Function

In C++, it is a member of a function in a class that we declare in the base class and we also redefine it in a derived class.

Pure Virtual Function

You can declare a pure virtual function but you cannot implement it. It is a virtual function that exists but cannot be implemented. You can declare a pure virtual function by placing ‘0’ in its declaration.

virtual float fun() = 0;

C++ Abstract Class

In C++, you can make a class abstract by declaring one of its functions as a pure virtual function. As we said, you can declare a pure virtual function by placing ‘0’ in its declaration.

Below is an example of an abstract class that has one abstract method draw(). The implementation is done by the derived classes such as Rectangle and Circle.

#include <iostream>  
using namespace std;  
class Shapes   
{    
public:   
virtual void drawShape()=0; //pure virtual function!    
};    
class Rectangle:Shapes   
{    
public:  
void drawShape()    
{    
cout<<"Drawing a rectangle!"<<endl;    
}    
};    
class Circle:Shapes    
{    
public:  
void drawShape()    
{    
cout<<"Drawing a circle!"<<endl;    
}    
};    
int main() {  
Rectangle r;  
Circle c;
cout<<"TechVidvan Tutorial: C++ Interfaces!"<<endl;
r.drawShape();    
c.drawShape();   
return 0;  
}

Output:-

TechVidvan Tutorial: C++ Interfaces!
Drawing a rectangle!
Drawing a circle!

Importance of C++ Interface

Suppose, you created a class named OS with data members Windows, Linux and Mac with member functions such as size(), type() and feature().

Let’s say that the size of each operating system is fixed and cannot be altered. But different operating systems have different sizes. In one way, you can implement the class OS for the function size() by making this function abstract. In this way, we can make sure that the size of all operating systems is fixed.
Below is the code to solve the above problem.

#include <iostream>
using namespace std;
class OS
{
public:
virtual void size() = 0;
void type()
{
cout<<"It is a windows operating system!"<<endl;
}
};
class Windows: public OS
{
public:
void size()
{
cout<<"The size is 4.90gb!"<<endl;
}
};

int main()
{
cout<<"TechVidvan Tutorial: C++ Interfaces!"<<endl<<endl;
Windows data;
data.size();
data.type();
return 0;
}

Output:-

C++ Interfaces!

The size is 4.90gb!
It is a windows operating system!

Follow the rules before using C++ Interfaces

You should follow the rules before working with the interfaces in the C++ programming language.

1. You can only declare a pure virtual function but you cannot define it.

2. You can only assign 0 to the pure virtual function.

3. Also you cannot create an instance of a class.

4. You can create a pointer to the instance of the derived class with a reference of base abstract class.

For Example:-

OS *obj = new Windows();
obj->size();

Summary

In this tutorial, we discussed the interface in detail in the C++ programming language. We discussed what interfaces and abstract classes are in C++ and their importance. We also discussed pure virtual functions in C++.