Encapsulation in C++ | Benefits of encapsulation | Example | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Tuesday, July 28, 2020

Encapsulation in C++ | Benefits of encapsulation | Example | My CS Tutorial

Data encapsulation in c++ | Benefits or advantages of encapsulation in c++ | C++ encapsulation | example of encapsulation in c++ | My CS Tutorial

Data encapsulation in C++ | My CS Tutorial

Encapsulation in C++


Encapsulation is a fundamental concept of object oriented programming. It is a way of combining the data and function into a single unit is called class and this mechanism is called encapsulation. This property provides the feature of information (Data) Hiding.

All C++ programs are composed of the following two fundamental elements −


Program statements (code) − This is the part of a program that performs actions and they are called functions.

Program data − The data is the information of the program which gets affected by the program functions.

Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.

How Encapsulation is achieved in a class


To do this:
1) Make all the data members private.
2) Create public setter and getter functions for each data member in such a way that the set function set the value of data member and get function get the value of data member.


There are various benefits of encapsulated classes.


Encapsulated classes reduce complexity.
Help protect our data. A client cannot change an Account's balance if we encapsulate it.
Encapsulated classes are easier to change. We can change the privacy of the data according to the requirement without changing the whole program by using access modifiers (public, private, protected). For example, if a data member is declared private and we wish to make it directly accessible from anywhere outside the class, we just need to replace the specifier private by public.


Let's see an example of Encapsulation.


#include <iostream>

using namespace std;

class Rectangle
{
int length;
int breadth;
public:
void setDimension(int l, int b)
{
length = l;
breadth = b;
}
int getArea()
{
return length * breadth;
}
};

int main()
{
Rectangle rt;
rt.setDimension(7, 4);
cout << rt.getArea() << endl;
return 0;
}


Data encapsulation in c++ | Benefits or advantages of encapsulation in c++ | C++ encapsulation | example of encapsulation in c++ | My CS Tutorial

_______________________________________


Please share this post and blog link with your friends.For more programs use this blog.

If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment