Data Encapsulation in C++
All C++ programs have the following two fundamental elements:
- Program Statements (Code): These are the parts of the program that perform actions and are called functions.
- Program Data: Data is the information of the program that is affected by the program's functions.
Encapsulation is a concept in object-oriented programming that binds data and the functions that manipulate the data together, preventing interference and misuse from the outside, thereby ensuring security. This concept leads to another important OOP concept, data hiding.
Data Encapsulation is a mechanism that bundles data and the functions that operate on the data, while Data Abstraction is a mechanism that exposes only the interfaces to the user and hides the implementation details.
C++ supports encapsulation and data hiding through the creation of classes (public, protected, private). We know that classes contain private members (private), protected members (protected), and public members (public). By default, all items defined in a class are private. For example:
class Box
{
public:
double getVolume(void)
{
return length * breadth * height;
}
private:
double length; // Length
double breadth; // Breadth
double height; // Height
};
Variables length, breadth, and height are private. This means they can only be accessed by other members of the Box class and not by any other part of the program. This is one way to achieve encapsulation.
To make members of a class public (i.e., accessible by other parts of the program), they must be declared with the public keyword. Any variable or function defined after the public identifier can be accessed by all other functions in the program.
Defining a class as a friend of another class exposes implementation details and reduces encapsulation. The ideal approach is to hide the implementation details of each class as much as possible.
Example of Data Encapsulation
Any class in a C++ program that has public and private members can serve as an example of data encapsulation and data abstraction. Consider the following example:
#include <iostream>
using namespace std;
class Adder{
public:
// Constructor
Adder(int i = 0)
{
total = i;
}
// Interface to the outside world
void addNum(int number)
{
total += number;
}
// Interface to the outside world
int getTotal()
{
return total;
};
private:
// Hidden data from the outside world
int total;
};
int main( )
{
Adder a;
a.addNum(10);
a.addNum(20);
a.addNum(30);
cout << "Total " << a.getTotal() <<endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Total 60
The class above adds numbers and returns the sum. The public members addNum and getTotal are the interfaces to the outside world, which users need to know to use the class. The private member total is hidden from the outside world, which the user does not need to know but is essential for the class to function properly.
Design Strategy
Typically, we set class member states to private unless we really need to expose them, to ensure good encapsulation.
This applies to data members, but it also applies to all members, including virtual functions.