Concept Of Data hiding & Encapsulation In C++
Defining data members and member functions together within a class is refffered as encapsulation. Without encapsulation the object oriented programming cannot be implement.
In object oriented programming most of the item data members is defined as private and member function will be defined as public. The public members of the class can be accessed from inside of the class as well as from the outside of the class. The private members of the class can be accessed from inside of the class but cannot be accessed from outside of the class. Thus private members remain hidden from outside of the class. Since most of the time the data members are kept private it remains hidden from outside of the class and thus the data hiding is implemented. Since data members are private the class user cannot access them directly but ca access them through predefined functions. Thus the class user can access the data the way class developer likes it.
- A function set radius that accepts value of the radius as parameter.
- A function get radius that will get the value of radius from user.
- A function to calculate and return area of the function.
- A function to calculate and return circumference of the circle.
- Write a suitable main function
In the above program data member radius and four member function set radius, get radius, area and circum are defined together and thus the encapsulation is obtained
The private members of the class can be accessed from inside of the class can be accessed from inside of the class but cannot be accessed from outside of the class. In the previous example the private member radius is accessed within the class and it cannot be access from the main function.
→ Public members:
Objects and allocated memory.
Calling the member fn.
Like it on Facebook, +1 on Google, Tweet it or share this article on other bookmarking websites.