I am not experienced in c++ . I read in text book that Encapsulation means data-hiding. I have seen some of discussions but I am not clear
For Example :
If I want to show the name and age of person but I want to hide the salary then I think I have to use encapsulation.
Does it demonstrate the example of encapsulation ?
In program how to use the encapsulation ?
For Example :
C:
#include <iostream>
using namespace std;
class person
{
public:
string name;
int age;
};
int main()
{
person a, b, c, d;
a.name = "Rock";
b.name = "Ceena";
c.name = "Stroke";
d.name = "Lacner";
a.age = 30;
b.age = 20;
c.age = 33;
d.age = 22;
cout << a.name << ": " << a.age << endl;
cout << b.name << ": " << b.age << endl;
cout << c.name << ": " << c.age << endl;
cout << d.name << ": " << d.age << endl;
return 0;
}
If I want to show the name and age of person but I want to hide the salary then I think I have to use encapsulation.
Does it demonstrate the example of encapsulation ?
C:
class person
{
public:
string name;
int age;
private:
float salary;
};
Last edited: