In object-oriented programming (OOP), encapsulation and data hiding aid in the management and security of program data.
Encapsulation In C++: Bundling Data and Functions
Code and the data it manipulates are protected from misuse and outside interference by encapsulation. It entails incorporating functions and data into a C++ class.
Class as a Unit: A class enables you to encapsulate data and construct functions on a custom type, restricting access to only the methods that require it. Members of a class, such as functions or data, are specified when the class is defined.
Abstract Data Type: An abstract data type is defined by encapsulation and data abstraction (the division between interface and implementation). Programmers that use this kind of class must understand the type’s functions but not its internal workings. This is accomplished by using a concealed implementation (private members) and a clear interface (public members).
Data Hiding: Protecting Implementation Details
One of OOP’s main features is data hiding, which means that data is hidden inside a class so that functions outside of it cannot accidentally access it. Building safe programs that are impenetrable by code in other areas of the program requires adherence to this notion.
Benefits: Data hiding prevents user programs from mistakenly changing encapsulated objects. As long as the public interface remains constant, class implementation can change without user-level code. Program writing, debugging, and maintenance improve.
Code Example: Illustrating Encapsulation and Data Hiding
Based on a person class, the C++ code sample that follows shows how data hiding limits access to internal data and how encapsulation mixes data and methods.
#include <iostream> // For input/output operations like std::cout and std::cin
#include <string> // For using std::string to store names
// Define a class named 'person'
class person {
// Private members: 'name' and 'age'. By default, if no access specifier
// is given, members declared within a class are private.
// This enforces data hiding, meaning they cannot be accessed directly
// from outside the class.
std::string name;
int age;
public:
// Public methods: 'getdata()' and 'display()'. These functions form the
// public interface of the 'person' class.
// They are the only intended way to interact with the private 'name' and 'age' data.
void getdata(void); // Method to get data from the user
void display(void); // Method to display the data
};
// Definition of the 'getdata()' member function, outside the class declaration.
// It uses the scope resolution operator '::' to indicate it belongs to 'person' class.
// Member functions have direct access to the private data members of their class.
void person::getdata(void) {
std::cout << "Enter name: ";
std::cin >> name; // Directly accesses private data member 'name'
std::cout << "Enter age: ";
std::cin >> age; // Directly accesses private data member 'age'
}
// Definition of the 'display()' member function.
// It also directly accesses the private data members 'name' and 'age'.
void person::display() {
std::cout << "\nName: " << name;
std::cout << "\nAge: " << age << std::endl;
}
int main() {
// Creating an object 'p' of the 'person' class. An object is an instance of a class.
person p;
// Accessing public member functions using the dot operator ('.').
// This is the controlled way to interact with the object's data.
p.getdata(); // Calls the public method 'getdata()' for object 'p'
p.display(); // Calls the public method 'display()' for object 'p'
// Attempting to directly access a private data member would result in a compile-time error.
// For example, uncommenting the line below:
// p.age = 30; // Error: 'int person::age' is private within this context.
// This demonstrates the effectiveness of data hiding.
return 0;
}
Output
Enter name: Govindhtech
Enter age: 2
Name: Govindhtech
Age: 2
The variables name and age in this example are wrapped within the person class and are susceptible to data hiding because they are implicitly designated private. Name or age cannot be directly accessed or changed by external code like main(). In order to ensure that access and manipulation take place in a regulated way specified by the class, main() must instead interact with the object’s data using the public member functions getdata() and show().
You can also read What Are The Accessing Members In C++ With Code Example