

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Encapsulation in C++
Encapsulation brings together the data and the methods that manipulate the data into a single component and protects them from outside interference. In essence, encapsulation involves bundling the data as well as the functions that use the data. Data encapsulation in lead to the very important concept of data hiding.
Encapsulation in C++ is implemented using classes that are user defined data types. These classes contain data types as well as methods that are bound together.
A program that represents encapsulation in C++ using classes is given as follows.
Example
#include <iostream> using namespace std; class EncapsulationDemo { private: int length, breath, height; public: void setValues(int l, int b,int h) { length = l; breath = b; height = h; } void calcVolume() { cout<<"Length = " << length << endl; cout<<"Breath = " << breath << endl; cout<<"Height = " << height << endl; cout<<"Volume = " << length*breath*height << endl; } }; int main() { EncapsulationDemo obj; obj.setValues(5, 3, 2); obj.calcVolume(); return 0; }
Output
Length = 5 Breath = 3 Height = 2 Volume = 30
In the above program, the variables and methods are wrapped in a single unit i.e the class Encapsulation. So, this program demonstrates the concept of encapsulation.
The length, breadth and height in class Encapsulation are private variables. There are public functions that initialize these variables and also calculate the volume by multiplying length, breadth and height. The code snippet for this is as follows.
class Encapsulation { private: int length, breadth, height; public: void setValues(int l, int b,int h) { length = l; breadth = b; height = h; } void calcVolume() { cout<<"Length = " << length << endl; cout<<"Breadth = " << breadth << endl; cout<<"Height = " << height << endl; cout<<"Volume = " << length*breadth*height << endl; } };
In the function main(), first an object of type Encapsulation is defined. Then the function setValues() is called with the values 5, 3 and 2. Finally, these values and the volume are displayed using the function calcVolume(). The code snippet for this is as follows.
Encapsulation obj; obj.setValues(5, 3, 2); obj.calcVolume();
- Related Questions & Answers
- Encapsulation in Java
- Encapsulation in C#
- Explain Encapsulation in PHP.
- What is encapsulation in Java?
- Abstraction vs Encapsulation in Java
- Advantages of encapsulation in Java
- What is encapsulation in C#?
- How is encapsulation implemented in C#?
- How Does Encapsulation Work in Ruby?
- JavaScript Encapsulation using Anonymous Functions
- Difference Between Abstraction and Encapsulation
- Difference Between Data Hiding and Encapsulation
- What is Generic Routing Encapsulation (GRE)?
- Explain difference between Abstraction and Encapsulation in PHP.
- What is the difference between abstraction and encapsulation in Java?