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.

C++

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...

Next Post Previous Post
No Comment
Add Comment
comment url