Encapsulation in C++
With C++ we achieve the functionality to wrap data and functions inside as a single form and keep them safe from outside world and achieve data hiding from and make the code more secure from unauthorised access.
With C++ we achieve the functionality to wrap data and functions inside as a single form and keep them safe from outside world and achieve data hiding from and make the code more secure from unauthorised access.
#include<iostream>
using namespace std;
class myClass
{
private:
// This data hidden from outside world
int salary;
public:
// function to set salary value
void setSalary(int s)
{
salary = s;
}
// function to return value of
// variable x
int getSalary()
{
return salary;
}
};
// main function
int main()
{
myClass myObj;
myObj.setSalary(80000);
cout << "The salary is: " << myObj.getSalary();
return 0;
}
Observations –
To implement this –