C++ Encapsulation


In C++ encapsulation is the process of binds together the data and functions in a single unit and it safe from outside interference and misuse.


Encapsulation is a technical name for data hiding.


Example: Example: Capsule, it is wrapped with different medicines.

c++ encapsulation

Data encapsulation is the process of binding the data, and the functions that use them and data abstraction is the process of showing only the interfaces and hiding the implementation details from the user.

 

The primary goal of encapsulation is the insulation of a particular object class internal details and show the interfaces.

 

If data is declared Private then it accesses within that package or class only.


Example:

#include <iostream>  
using namespace std;

class Rectangle{
	private:
    	// hidden data from outside world
    	int length,breadth; 
        float area;
	public:
    	void get_data()
        {
        	cout << "Enter the length and breadth value: " ;
            cin >> length >> breadth;
            area= length * breadth;
        }
        void show_data()
        {
        	cout << "Area of the Rectangle: " << area;
        }
}; 
int main(){  
    Rectangle r1;
	r1.get_data();
    r1.show_data();
    return 0;   
}



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.