How do you implement abstraction and encapsulated in c++
Abstraction
The abstraction principle only shows people the information they need. It hides the implementation intricacies of programs, reducing the program's complexity.
Example of Abstraction:
#include <iostream>
using namespace std;
class Abstraction {
private:
int a, b, c; //The private members
public:
void mult(int x, int y)
{
a = x;
b = y;
c = a * b;
cout<<"Multiplication of the two number is : "<<c<<endl;
}
};
int main()
{
Abstraction s;
s.mult(5, 4);
return 0;
}
Encapsulation:
Encapsulation is a technique for hiding data in a single entity or unit while also protecting it from the outside world.
Example of Abstraction:
#include <iostream>
using namespace std;
class Encapsulation {
private:
int x; // Declared as private:
public:
// Initializing the value of x
void setX(int a)
{
x = a;
}
//Getting the value of x
int getX()
{
return x;
}
};
int main()
{
Encapsulation en1;
en1.setX(10);
cout<<en1.getX();
return 0;
}
Comments
Leave a comment