C++ Encapsulation
Encapsulation is process to combination of data and function into single unit. this are features of object oriented programming(oops). Class mechanism are providing this facility by using of bundling data member and member function. this process is also know as data abstraction and data hiding.
Why Encapsulation Necessary
When we create a class, then our goal is only defined well structure and accessibility. So we are divide of class function and data in separate specifiers category. Public private and protected are decide accessibility of class function and data. There are important because private and protected data member and function are not directly access outside of class. This mechanism are provide protection. and we are decide how to utilize those (private & protected) data and function using public member function. this process are provide control of accessibility. So this reason programmer are complete control in code execution.
Encapsulation Real Life Example
Assume that you are a rich man. and you are develop the new IT infrastructure in our country. so you are organize a function and invite most IT sector people. so this is highly confidential meeting so here provide very tight security. only those people are join this meeting there are have invitation card. otherwise security in-charge can not allowed to come other people. or we can say only authorized people are allowed to join function. In this case security is layer those are protected of function organization. And outside people are not information about what is happening inside meeting room.
Data Encapsulation Example
C++ are implement encapsulation using class and access specifiers. see example
#include<iostream>
using namespace std;
class Innovation{
private:
//hide by outside of class
int gun,boom;
public:
//data members
Innovation(int,int);
void display();
};
Innovation :: Innovation(int gun,int boom){
//assign initial values
this->boom=boom;
this->gun=gun;
}
void Innovation::display(){
/*
Display private data member inside
a class member function.
*/
cout<<" Number of Gun's :"<<gun<<endl;
cout<<" Number of boom's :"<<boom<<endl;
}
int main(){
//Create object
Innovation test(50,60);
//access public function
test.display();
}
Output
Number of Gun's :50
Number of boom's :60
Note that Innovation class are two private data this is access by only class function. not allowed to access outside of class.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment