What is meant by Abstraction in C++?

Abstraction is a process of providing only the essential details to the outside world and hiding the internal details, i.e., representing only the essential details in the program. It can be achieved in C++ using header files or wrapping program logic in classes.

It should be employed when you want to:-

  • Protect details of the class from user-level errors.
  • not need to write the low-level code.
  • avoid code duplication.

CODE:

#include <iostream>    
using namespace std;    
 class Sum    
{    
private: int x, y, z; 
public:    
void add()    
{    
cout<<"Enter two numbers: ";    
cin>>x>>y;    
z= x+y;    
cout<<"Sum of two number is: "<<z<<endl;    
}    
};    
int main()    
{    
Sum sm;    
sm.add();    
return 0;    
}