Data Hiding in C++ - Definition, Advantages and Examples

1. Introduction to Data Hiding in C++

Data Hiding is an important concept of Object-Oriented Programming (OOP). It refers to restricting direct access to the internal data members of a class to protect them from unauthorized or accidental modification. In C++, Data Hiding is commonly implemented using the private and protected access specifiers. Public member functions can be provided to access or modify private data in a controlled manner.

Data Hiding, Object-Oriented Programming (OOP) का एक महत्वपूर्ण concept है। इसका अर्थ class के internal data members तक direct access को restrict करना है, ताकि उन्हें unauthorized या accidental modification से सुरक्षित रखा जा सके। C++ में Data Hiding को सामान्यतः private और protected access specifiers की सहायता से implement किया जाता है। Private data को controlled तरीके से access या modify करने के लिए public member functions provide किए जा सकते हैं।

2. Need for Data Hiding in C++

Data Hiding is used to protect the internal data of a class and maintain control over how that data is accessed or modified.

Data Hiding का उपयोग class के internal data को सुरक्षित रखने और यह control करने के लिए किया जाता है कि उस data को कैसे access या modify किया जाए।

The main purposes of Data Hiding are:

  1. Data Protection: Prevents direct access to internal data members.

  2. Controlled Access: Allows data to be accessed or modified through suitable member functions.

  3. Data Validation: Helps ensure that only valid values are assigned to data members.

  4. Reduced Errors: Prevents accidental modification of important data.

  5. Maintainability: Internal data representation can be changed without requiring changes to code that uses the public interface.

Data Hiding के मुख्य उद्देश्य:

  1. Data Protection: Internal data members को direct access से सुरक्षित रखता है।

  2. Controlled Access: Suitable member functions के माध्यम से data को access या modify करने की सुविधा देता है।

  3. Data Validation: यह सुनिश्चित करने में मदद करता है कि data members को केवल valid values assign हों।

  4. Errors कम करना: Important data में accidental modification को रोकता है।

  5. Maintainability: Public interface का उपयोग करने वाले code को बदले बिना internal data representation बदला जा सकता है।

3. Implementing Data Hiding Using the private Access Specifier

In C++, the private access specifier is commonly used to implement Data Hiding. Private members cannot normally be accessed directly from outside the class. They can be accessed through the class's member functions and other permitted contexts.

C++ में Data Hiding implement करने के लिए सामान्यतः private access specifier का उपयोग किया जाता है।Private members को class के बाहर से normally directly access नहीं किया जा सकता। इन्हें class के member functions और अन्य permitted contexts के माध्यम से access किया जा सकता है।

Example: Data Hiding Using private

#include <iostream>
using namespace std;
class Student
{
private:
int rollNo;
public:
void setRollNo(int r)
{
rollNo = r;
}
void displayRollNo()
{
cout << "Roll Number: " << rollNo;
}
};
int main()
{
Student s1;
s1.setRollNo(101);
s1.displayRollNo();
return 0;
}

Output:

Roll Number: 101

Explanation:

  • rollNo is declared as a private data member.

  • It cannot be accessed directly from the main() function.

  • setRollNo() assigns a value to rollNo.

  • displayRollNo() displays the value of rollNo.

  • Both public member functions provide controlled access to the private data.

व्याख्या:

  • rollNo को private data member declare किया गया है।

  • इसे main() function से directly access नहीं किया जा सकता।

  • setRollNo() function, rollNo को value assign करता है।

  • displayRollNo() function, rollNo की value display करता है।

  • दोनों public member functions private data तक controlled access provide करते हैं।

4. Data Hiding Using Getter and Setter Functions

Getter and Setter functions are commonly used to implement controlled access to private data members.

  • A Setter function is used to assign or update a value.

  • A Getter function is used to retrieve a value.

Getter और Setter functions का उपयोग private data members तक controlled access provide करने के लिए किया जाता है।

  • Setter Function का उपयोग किसी data member को value assign या update करने के लिए किया जाता है।

  • Getter Function का उपयोग किसी data member की value प्राप्त करने के लिए किया जाता है।

Example: Getter and Setter in C++

#include <iostream>
using namespace std;
class BankAccount
{
private:
double balance = 0;
public:
void setBalance(double amount)
{
if (amount >= 0)
{
balance = amount;
}
else
{
cout << "Invalid Amount!" << endl;
}
}
double getBalance()
{
return balance;
}
};
int main()
{
BankAccount account;
account.setBalance(5000);
cout << "Account Balance: "
<< account.getBalance();
return 0;
}

Output:

Account Balance: 5000

Explanation:

  • balance is a private data member initialized to zero.

  • setBalance() checks that the amount is not negative before updating the balance.

  • getBalance() returns the current balance.

  • The private data member is accessed through public member functions.

व्याख्या:

  • balance एक private data member है, जिसे zero से initialize किया गया है।

  • setBalance() balance update करने से पहले check करता है कि amount negative न हो।

  • getBalance() current balance return करता है।

  • Private data member को public member functions के माध्यम से access किया गया है।

5. Data Hiding Using the protected Access Specifier

The protected access specifier allows class members to be accessed within the class and its derived classes. However, these members cannot normally be accessed directly through an object from outside the class hierarchy.

The protected access specifier is useful when derived classes need access to data members of their base class.

protected access specifier class members को उसी class और उसकी derived classes में access करने की अनुमति देता है। लेकिन class hierarchy के बाहर से object की सहायता से इन members को normally directly access नहीं किया जा सकता।

जब derived classes को अपनी base class के data members तक access की आवश्यकता होती है, तब protected access specifier उपयोगी होता है।

Example: Data Hiding Using protected

#include <iostream>
using namespace std;
class Employee
{
protected:
int employeeId;
};
class Manager : public Employee
{
public:
void setEmployeeId(int id)
{
employeeId = id;
}
void displayEmployeeId()
{
cout << "Employee ID: " << employeeId;
}
};
int main()
{
Manager m1;
m1.setEmployeeId(501);
m1.displayEmployeeId();
return 0;
}

Output:

Employee ID: 501

Explanation:

  • Employee is the base class.

  • employeeId is a protected data member.

  • Manager is derived from Employee.

  • The derived class can access employeeId through its member functions.

  • The object m1 cannot directly access employeeId from main().

व्याख्या:

  • Employee एक base class है।

  • employeeId एक protected data member है।

  • Manager, Employee से derived class है।

  • Derived class अपने member functions के माध्यम से employeeId को access कर सकती है।

  • main() function से m1 object द्वारा employeeId को directly access नहीं किया जा सकता।

6. Advantages of Data Hiding in C++

Data Hiding provides several benefits in object-oriented program development.

Object-Oriented Programs develop करते समय Data Hiding के कई फायदे होते हैं।

  1. Improved Data Protection: Restricts direct access to internal data members.

    Data Protection: Internal data members तक direct access को restrict करता है।

  2. Controlled Modification: Data can be modified through member functions that apply suitable validation.

    Controlled Modification: Data को ऐसे member functions के माध्यम से modify किया जा सकता है जो suitable validation apply करते हैं।

  3. Better Reliability: Reduces the possibility of accidental changes to important data.

    Better Reliability: Important data में accidental changes की संभावना को कम करता है।

  4. Encapsulation: Supports encapsulation by combining data and the functions that operate on it within a class.

    Encapsulation: Data और उस पर कार्य करने वाले functions को एक class में combine करके Encapsulation को support करता है।

  5. Easy Maintenance: Changes to internal data representation can be managed without changing the public interface.

    Easy Maintenance: Public interface को बदले बिना internal data representation में changes किए जा सकते हैं।

7. Difference Between Data Hiding and Encapsulation

Data Hiding and Encapsulation are closely related concepts, but they are not exactly the same.

Data Hiding और Encapsulation एक-दूसरे से संबंधित concepts हैं, लेकिन दोनों बिल्कुल समान नहीं हैं।

BasisData HidingEncapsulation
MeaningRestricts direct access to internal dataCombines data and related functions into a single unit
Main purposeProtects internal dataOrganizes and controls access to data and functionality
FocusData accessibilityData and methods together
ImplementationCommonly uses private and protectedAchieved using classes and access control
RelationshipA part of encapsulationA broader OOP concept

Conclusion

Data Hiding is an important feature of Object-Oriented Programming in C++. It restricts direct access to internal data members and provides controlled access through public member functions. The private and protected access specifiers are commonly used for Data Hiding. Getter and Setter functions can also be used to validate and control access to private data.

C++ में Data Hiding Object-Oriented Programming का एक महत्वपूर्ण feature है। यह internal data members तक direct access को restrict करता है और public member functions के माध्यम से controlled access provide करता है। Data Hiding के लिए सामान्यतः private और protected access specifiers का उपयोग किया जाता है। Private data तक access को validate और control करने के लिए Getter और Setter functions का भी उपयोग किया जा सकता है।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

C++ Programming Syllabus Complete - From Basics to Advanced C++, including OOP and Data Structures and Algorithms (DSA)

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)