Interfaces in C++ (Abstract Class) – Explore How Pure Virtual Function Works

Free C++ course with real-time projects Start Now!!

Interfaces in C++ are nothing but a way to describe the behavior of a class without any commitment to any specific implementation of that class. It is the supplement feature offered by C++ classes and objects. Here, we will help you move a step forward to explore the depth of object-oriented programing using classes and objects. It is important to note that there’s no difference between Interfaces and abstract classes in C++.

So let’s not waste time and start our journey for C++ Interfaces.

Interfaces in C++

In programming terminology, a class with a pure virtual function can be termed as an abstract class in C++.

We can implement Interfaces in C++ with the help of abstract classes. Interfaces are closely associated with classes and objects. Therefore, it is safe is to say that the term “Interfaces” and “Abstract Classes” more or less convey the same idea.

Key takeaway: It is important to note that abstract classes and data abstraction in C++ are two different concepts as data abstraction simply separates the essential data from its implementation techniques.

Pure Virtual Functions in C++

As already discussed, an abstract class in C++ is nothing but a class with a pure virtual function. Now, let’s understand what are virtual and pure virtual function.

A virtual function in C++ is nothing but a member function in a class that we declare within the base class and redefine in a derived class.

A pure virtual function in C++ is nothing but a virtual function that we know exists but cannot be implemented. It is simply declared, not implemented.

Its declaration is as follows:

/* 0 does not indicate that we initialize the function to a null or zero value */

virtual void function() = 0;

Key takeaway: It is important to note that a pure virtual function can only be declared, not defined.

Importance of Interfaces in C++

After developing an understanding of pure virtual functions, let us acknowledge its significance by considering a simple problem at hand. Suppose you have created a class named Apple with data members iPhoneX, iPad_Pro, and iPod with member functions price(), ringtone() and alarm().

Let us consider that the price of each product is fixed and cannot be altered. We know that Apple products have different prices like iPhoneX costs 65,500 and an iPad_Pro costs 48,000.

Implementation of the class Apple for the function price() can be done only in one correct way, that is, by making this function pure abstract so that, we need not give implementation in the class Apple but all the classes that inherit Apple class must give implementation to this function. In this way, we can ensure that all Apple products have a fixed and unique price.

2 Ways to Implement Friend Function in C++ (Including Practical Example)

Here is a program in C++ that would help you solve this above-stated problem:

using namespace std;

class Apple
// Pure Virtual Function declaration
virtual void price() = 0;

// Member functions
void ringtone() 
cout<<"The ringtone is: Reflection"<<endl;
class iPhoneX: public Apple
void price() 
cout<<"The price is: 65,500"<<endl;

int main()

cout<<"Welcome to DataFlair tutorials"<<endl<<endl;

iPhoneX i;
return 0;

From the above program, it is pretty clear how we can implement abstract classes in C++ with the help of pure virtual functions.


Output of Pure Virtual Functions in C++

Rules Associated with Interfaces in C++

There are certain rules that we need to keep in mind while working with interfaces/ abstract classes in C++.

  • A pure virtual function can only be declared, it cannot be defined.
  • You cannot assign any value other than 0 to the pure virtual function.
  • It is not possible to create an instance of a class. For instance, with reference to the above program, you cannot create an object of Apple type. It would result in a compilation error.
  • In case the derived class cannot implement the pure virtual function of the base class, then the derived class acts as an abstract class.
  • It is possible to create a pointer with a reference of base abstract class points to the instance of the derived class.

For instance, with reference to the above program, you can achieve this task by writing the following statements:

Apple *object = new iPhoneX();
object -> price();

Quiz on Interfaces in C++


Congratulations, you have finally completed interfaces in C++. Hope, now you are clear with the difference between interfaces and abstract classes in C++. In addition, we saw how to implement pure virtual functions with the help of classes through a real-world example. Finally, we discussed some of the rules associated with abstract classes leaving no room for any doubts.

If you have any queries, do let us know in the comment section below.

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

follow dataflair on YouTube

1 Response

  1. gerardoruiz says:

    Good illustration of ‘C++ interfaces’ or ‘Abstract Classes’. Needs to improve redaction. But in general good Article.
    Just remove:
    ‘2 Ways to Implement Friend Function in C++ (Including Practical Example)’
    I don’t know what is doing there. If tried to make an hyperlink. It doesn’t have it, and confuses with the Article in action.

Leave a Reply

Your email address will not be published. Required fields are marked *