Advertisement



< Prev
Next >

C++ Encapsulation





C++ being an Object Oriented Programming(OOPS) language provides your code - security, flexibility and its easy maintainability through encapsulation. Encapsulation is to wrap the data member and the functions of a class within a single unit.




Why encapsulation?



To understand the need of encapsulation, let's first see a class that is an example of a bad encapsulation and the drawbacks associated with it.




An example of a bad encapsulation.


We are creating a class with a public data member called weight, that should never be set to a negative value.

#include<iostream>

using namespace std;

class A
{
public:		// public - visibility label
int weight; // weight with public visibility label makes it accessible by anyone 
};


int main()
{
	A ob;
	ob.weight = -100; //weight set to a negative value, legal but wrong.
	cout<<"weight is : " << ob.weight;
}


Output


weight is : -100

In this code, we have made a class A with its public data member, weight. In the main method, we have created an object of this class and through it we have directly accessed its public data member, weight, to set it to a negative value i.e. ob.weight=-100, which will later cause a problem as weight is never a negative value.




Drawbacks of a bad encapsulation.


Giving a public access to data member of your class makes it unsafe from an unauthorized access, hence, anyone can directly access it directly through class's object and set its value even beyond its permissible range, which may cause an issue.

Now in order to rectify this issue, if you modify the visibility label of the data member, weight, to private, you will end up breaking the code of everyone who are already inheriting your class A and are directly accessing its data member, weight. Thus such code is not only less secure but also less flexible to change.

Hence, you should only create your classes by following proper encapsulation. Let's see how the good encapsulation comes to the rescue.


Advertisement









An example of good encapsulation


#include<iostream>

using namespace std;

class A
{

private:
int  weight;	//declaring weight with private visibility label


public:
int getWeight()  //public getter method to get weight.
{
return weight;
}

void setWeight(int wt)  //public setter method to set weight.
{
if(wt<0)
	cout<<"Please enter weight greater than zero";

else
	weight=wt;
}
};



int main()
{
A ob;
ob.setWeight(100);
}

In this code, we have restricted the access to weight data member by declaring it private, hence, anyone wishing to set the value of weight data member, will have to call setWeight() function. In setWeight() function, we have an if-else condition that makes sure that the data member, weight, should not be set to a negative value.

Following such proper encapsulation also provides flexibility to your code, allowing you to make changes to you code, without breaking the code of others who were accessing your code, for example- tomorrow, you may decide to put in a different if-else condition, like - weight should be set between 50 and 100 kg/pounds. Modifying your code in such way will not affect anyone accessing your class.




Benefits of encapsulation


Through such proper Encapsulation, now your code is more flexible to the change, easy to maintain and you've also secured and protected your data(data members) from the direct access by giving it a private access.




Difference between encapsulation and abstraction


While encapsulation helps in hiding the data(instance variables) of a class from an illegal access, abstraction provides essential general features without providing implementation details, by using an abstract class.


Example of Abstraction

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement