Interfaces and Data Abstraction in C++ ( With Examples )

Interfaces and Data Abstraction in C++ ( With Examples )

31 Mar 2024
8 min read
Learn via Video Course & by Doing Hands-on Labs

C++ Programming For Beginners

Interfaces and Data Abstraction in C++: An Overview

After understanding OOP concepts like classes, objects, inheritance in C++, and polymorphism in C++, it's now time to move to Data Abstraction in C++. It is one of the most essential features of object-oriented programming in C++. For a better experience explore our C++ Programming Course.

What is Data Abstraction in C++?

Data Abstraction in C++ means providing only the essential details to the outside world and hiding the internal details, i.e., hiding the background details or implementation. Abstraction is a programming technique that depends on the separation of the interface and implementation details of the program.

Ways to implement Data Abstraction in C++

Interfaces and Data Abstraction in C++

Abstraction using classes

Abstraction in C++ programming language can be implemented in classes. The class helps the programmers to group the members of that data and function by using available access specifiers. A class is the main deciding factor of which data member will be visible to the world or which is not.

Abstraction using header files

Another type of abstraction in the C++ programming language is related to header files. For instance, let's consider the pow() method present in the math.h header file. If the developer needs to calculate the power of a number, then it is called the function pow() which is present in the math.

Read more: OOPs concept and object class in C++

Example to demonstrate data abstraction in C++ Compiler

// C++ Program to Demonstrate the working of Data Abstraction
#include <iostream>
using namespace std;
class implementAbstraction
 int A, B;
 // method to set values of
 // private members
 void set(int x, int y)
 A = x;
 B = y;
 void display()
 cout << "A = " << A << endl;
 cout << "B = " << B << endl;
int main()
 implementAbstraction obj;
 obj.set(20, 30);
 return 0;


A= 20
B= 30

Read More - C++ Interview Interview Questions and Answers

Advantages of Data Abstraction in C++

  1. Data abstraction in C++ programming language helps the developers as well as users to avoid writing any low-level code
  2. This particular feature helps to avoid code duplication thus it increases reusability.
  3. Data abstraction can independently change the internal implementation of the class without affecting the user.
  4. Data abstraction helps to boost the security of a program or application because only the important details are provided to the user.
  5. This particular feature reduces the redundancy as well as the complexity of the program, so it increases the readability of the code.

What is Interface in C++?

An interface describes the behavior or capabilities of a C++ class without committing to a particular implementation of that class. In C++, we don't get a built-in interface. interfaces. To create an interface, first, we need to create an abstract class which is having only pure virtual methods. In C++, Interfaces are also called pure abstract classes.

An abstract class is a class specially designed to be used as a base class. It must have at least one pure virtual function. It may have variables and normal functions. The derived classes of an abstract class must implement all the pure virtual functions of their base class or else they too become abstract.

Importance of Interfaces in C++

  • Any class that is derived from the pure abstract classes which are called Interface must be implemented by all of the methods of the base class.
  • The pointers in the Interface can be passed to classes and functions thereby the programmers as well as the user can call the functions of the derived class from that particular class.

Rules of Using Interfaces in C++

  • The users have to declare only pure virtual functions without giving their definition
  • For pure virtual functions, the users can only assign 0.
  • The user cannot create an instance of the class.
  • Users can only create a pointer to the instance of a particular derived class with a reference to a base class, or Abstract Classes


// C++ program to implement an Interface
#include <iostream>
#include <string>
using namespace std;
// Interface(Abstract class
// with pure virtual function)
class Scholar
 virtual string returnString() = 0;
class child : public Scholar
 string returnString()
 return "ScholarHat";
int main()
 child childObj;
 Scholar* ptr;
 ptr = &childObj;
 cout << ptr->returnString();
 return 0;



To conclude, interfaces and data abstraction are two important aspects of object-oriented programming in C++. By understanding and making use of these concepts, you can make your code more efficient and easier to manage. So, practice in C++ certification training – and don’t be afraid to experiment with these techniques in your own projects. Want to learn more about interfaces and data abstraction? Check out our other blog post on the subject.

Share Article
Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Self-paced Membership
  • 22+ Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A
  • 10+ Real-world Projects
  • Career Coaching
  • Email Support
Upto 66% OFF

To get full access to all courses

Accept cookies & close this