Data Hiding in C++
Introduction
In object-oriented programming, data hiding is an important concept that allows us to protect the internal data of a class from being accessed or modified directly by external code. This helps in maintaining the integrity and encapsulation of the class.
In this code example, we will explore the concept of data hiding in C++. We will create a class called "Base" that demonstrates how to hide data members and provide controlled access to them using member functions.
Key Concepts
- Data Hiding: Data hiding is the practice of encapsulating data within a class and providing controlled access to it through member functions. This prevents direct access to the data from external code, ensuring data integrity and encapsulation.
- Private Access Specifier: In C++, the private access specifier is used to declare members that are only accessible within the class. Private members cannot be accessed or modified directly by external code.
- Public Member Functions: Public member functions are used to provide controlled access to private data members. These functions can be called from external code to perform operations on the hidden data.
Code Structure
- The code starts with the inclusion of the iostream library, which allows us to use input/output operations. Then, a class called "Base" is defined, which contains a private data member num and two public member functions: getData() and showData().
- The getData() function prompts the user to enter an integer value and stores it in the private data member num. The showData() function displays the value of num on the console.
- In the main() function, an object of the Base class is created. The getData() function is called to input a value, and then the showData() function is called to display the value.
Code Examples
#include<iostream>
using namespace std;
class Base
{
int num;
public:
void getData();
void showData();
};
void Base::getData()
{
cout<<"Enter Any Integer Vlaue:"<<endl;
cin>>num;
}
void Base::showData()
{
cout<<"The Value Is:"<<num<<endl;
}
int main()
{
Base obj;
obj.getData();
obj.showData();
}
Explanation
- The code demonstrates the concept of data hiding by encapsulating the num data member within the Base class. The num member is declared as private, which means it cannot be accessed directly from outside the class.
- The getData() function is used to input a value for num from the user. It prompts the user to enter an integer value and stores it in the private num member.
- The showData() function is used to display the value of num on the console. It simply prints the value of num using the cout statement.
- In the main() function, an object of the Base class is created using the Base obj; statement. Then, the getData() function is called to input a value for num. Finally, the showData() function is called to display the value of num on the console.
Conclusion
Data hiding is an essential concept in object-oriented programming that allows us to protect the internal data of a class from direct access by external code. By encapsulating data within a class and providing controlled access through member functions, we can ensure data integrity and maintain the encapsulation of the class. In this code example, we demonstrated how to hide data members in C++ using the private access specifier and provided controlled access to them through public member functions.
Learn More...
- Efficient Array Sorting in C++: Static Overloaded Functions for Floats and Integers
- Programm to demonstrate constructor with default arguments
- Understanding Function Call by Value in C++
- Exploring Virtual Functions with Default Arguments in C++
- Multiple Inheritance in C++
- Binary + Operator Overloading In CPP
- Exploring Class-to-Basic Type Conversion with a Product Example in C++
- Creating a Diamond Pattern in CPP Program
- Write a CPP program to perform arithmetic operations using inline function.
- Write a program in C++ to create a structure named complex having data member real and imag. Create member function add_complex which takes structure as an argument and return structure. Using function add two complex numbers.
- Write program to generate following pattern