Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Interfaces and Data Abstraction in C++ ( With Examples )

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

21 May 2024
Advanced
13.1K Views
8 min read
Learn with an interactive course and practical hands-on labs

Free C++ Course: Learn C++ In 21 Days

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
{
private:
 int A, B;
public:
 // 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);
 obj.display();
 return 0;
}

Output

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

Example


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

Output

ScholarHat
Summary

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
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this