Encapsulation in C++

Advertisements

Abstraction in C++ Polymorphism in C++

Real Life Example of Encapsulation in C++

Encapsulation is a process of wrapping of data and methods in a single unit. It is achieved in C++ language by class concept.

Combining of state and behavior in a single container is known as encapsulation. In C++ language encapsulation can be achieve using class keyword, state represents declaration of variables on attributes and behavior represents operations in terms of method.

encapsulation in c++

Syntax of Encapsulation in C++

Syntax of Encapsulation in C++

class class name
{
private:
datatype data;
public:
Member functions;
};

main()
{
classname objectname1,objectname2……………;
} 

Real Life Example of Encapsulation in C++

The common example of encapsulation is Capsule. In capsule all medicine are encapsulated in side capsule.

real life example of encapsulation in c++

Automatic Cola Vending Machine :Suppose you go to an automatic cola vending machine and request for a cola. The machine processes your request and gives the cola.

real life example of encapsulation in c++

Here automatic cola vending machine is a class. It contains both data i.e. Cola can and operations i.e. service mechanism and they are wrapped/integrated under a single unit Cola Vending Machine. This is called Encapsulation.

A Washing Machine and It's Power Button: What is the function that power button does? Switches the machine on (obviously). But did u ever imagined the inside mechanism. Doesn't matter unless it's functioning well. That's encapsulation. The object is wrapped and inner details are hidden. Only thing is that object can be called and used. User friendly!

You can't access private date outside the class.

Benefits of encapsulation

  • Provides abstraction between an object and its clients.
  • Protects an object from unwanted access by clients.
  • Example: A bank application forbids a client to change an Account's balance.

Example of Encapsulation in C++

#include<iostream.h>
#include<conio.h>

class sum
{
private: int a,b,c;

public:
void add()
{
clrscr();
cout<<"Enter any two numbers: ";
cin>>a>>b;
c=a+b;
cout<<"Sum: "<<c;
}
};
void main()
{
sum s;
s.add();
getch();
}

Output

Enter any two number: 
4
5
Sum: 9

In above example all data and function are bind inside class sum.

Role of Access Specifiers in Encapsulation

Access specifiers plays an important role in implementing encapsulation in C++. The process of implementing encapsulation can be sub-divided into two steps:

  • The data members should be labeled as private using the private access specifiers.
  • The member function which manipulates the data members should be labeled as public using the public access specifier.

How Encapsulation is achieved in a class

  • Make all the data members private.
  • 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.

Advantage of Encapsulation in C++

  • The main advantage of using of encapsulation is to secure the data from other methods, when we make a data private then these data only use within the class, but these data not accessible outside the class.
  • The major benefit of data encapsulation is the security of the data. It protects the data from unauthorized users that we inferred from the above stated real-real problem.
  • We can apply the concept of data encapsulation in the marketing and finance sector where there is a high demand for security and restricted access of data to various departments.
  • Encapsulation helps us in binding the data(instance variables) and the member functions(that work on the instance variables) of a class.
  • Encapsulation is also useful in hiding the data(instance variables) of a class from an illegal direct access.
  • Encapsulation also helps us to make a flexible code which is easy to change and maintain.

Dis-Advantage of Encapsulation in C++

You can't access private date outside the class.

Designing Strategy of Encapsulation in C++

Most of us have learnt to make class members private by default unless we really need to expose them. That's just good encapsulation. This is applied most frequently to data members, but it applies equally to all members, including virtual functions.


Abstraction in C++ Polymorphism in C++

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements