13
DecInterfaces and Data Abstraction in C++ ( With Examples )
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++
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++
- Data abstraction in C++ programming language helps the developers as well as users to avoid writing any low-level code
- This particular feature helps to avoid code duplication thus it increases reusability.
- Data abstraction can independently change the internal implementation of the class without affecting the user.
- Data abstraction helps to boost the security of a program or application because only the important details are provided to the user.
- 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.