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 (variables) and behavior (functions) in a single container (object) 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.

Benefits of Encapsulation:

  • 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.

  • Provides abstraction between an object and its clients.

  • Protects an object from unwanted access by clients.

Example:
#include<iostream>

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

public:
void add()
{
cout<<"Enter any two numbers: ";
cin>>a>>b;
c=a+b;
cout<<"Sum: "<< c;
}
};
int main()
{
sum s;
s.add();
return 0
}           
OUTPUT:
Enter any two number: 
6
6
Sum: 12   

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

Next topic is Abstraction





 



Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved