Encapsulation and Data Hiding in Object-Oriented C++ Notes in Hindi & English : एनकैप्सुलेशन और डेटा हाइडिंग: गेटर्स और सेटर्स की भूमिका और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Encapsulation and Data Hiding in Object-Oriented C++ (एनकैप्सुलेशन और डेटा हाइडिंग: गेटर्स और सेटर्स की भूमिका) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: Encapsulation bundles data members and the methods operating on them inside a single class capsule, while data hiding restricts direct external access using private or protected visibility. Controlled public getter (const accessor) and setter (validating mutator) methods safeguard object invariants and decouple internal storage details from client code.

अध्याय सार: एनकैप्सुलेशन डेटा मेंबर्स और उन पर काम करने वाले मेथड्स को एक ही क्लास इकाई में समेटता है, जबकि डेटा हाइडिंग private या protected द्वारा बाहरी सीधे एक्सेस को रोकती है। नियंत्रित पब्लिक गेटर (const एक्सेसर) और सेटर (सत्यापन करने वाला म्यूटेटर) मेथड्स डेटा नियमों की रक्षा करते हैं और बाहरी कोड को आंतरिक बदलावों से बचाते हैं।

Encapsulation and Data Hiding in Object-Oriented C++: Detailed Notes & Architecture (एनकैप्सुलेशन और डेटा हाइडिंग: गेटर्स और सेटर्स की भूमिका)

1. Encapsulation vs Data Hiding: Distinguishing the Concepts (एनकैप्सुलेशन बनाम डेटा हाइडिंग: दोनों में सूक्ष्म अंतर)

[EN] Although frequently mentioned together, encapsulation and data hiding solve distinct architectural goals in OOP. Encapsulation is the structural bundling of state (attributes) and behavior (member functions) into a cohesive class unit—even a class with all public members technically encapsulates them in one container. Data hiding goes further by marking internal fields private (or protected) so outside functions cannot directly read or overwrite raw state without going through approved methods. Combined together, encapsulation + data hiding establish a strong abstraction barrier that keeps objects in valid, self-consistent states throughout their lifetime.

[HI] यद्यपि अक्सर दोनों को एक साथ पढ़ा जाता है, एनकैप्सुलेशन और डेटा हाइडिंग के उद्देश्य थोड़े अलग हैं। एनकैप्सुलेशन का अर्थ डेटा (गुण) और उस पर काम करने वाले मेथड्स (व्यवहार) को एक क्लास कैप्सूल में बांधना है। डेटा हाइडिंग इससे आगे बढ़कर आंतरिक डेटा को private या protected बनाकर बाहरी कोड द्वारा सीधे बदलाव या अनधिकृत रीडिंग को रोकती है। दोनों मिलकर एक मजबूत सुरक्षा कवच बनाते हैं जिससे ऑब्जेक्ट हमेशा वैध और सुसंगत अवस्था में बना रहता है।

2. Getters (Accessors) and Setters (Mutators) (गेटर्स (Accessors) और सेटर्स (Mutators) की भूमिका)

[EN] To interact safely with hidden private fields, classes expose public accessor methods (getters) and mutator methods (setters). A getter such as double getBalance() const { return balance; } retrieves state without altering the object; marking it const guarantees read-only safety and allows calling it on const objects and const references. A setter such as void setAge(int a) enforces business invariants before updating the field—for instance, rejecting negative ages or out-of-range percentages instead of letting invalid data corrupt the object. By omitting a setter (providing only a getter), a designer creates a read-only attribute; omitting a getter creates a write-only attribute such as setPassword().

[HI] छिपे हुए private डेटा से सुरक्षित संपर्क के लिए क्लास पब्लिक गेटर (एक्सेसर) और सेटर (म्यूटेटर) मेथड्स उपलब्ध कराती है। गेटर जैसे double getBalance() const बिना डेटा बदले मान लौटाता है; इसके अंत में const लगाने से यह const ऑब्जेक्ट्स पर भी कॉल किया जा सकता है। सेटर जैसे void setAge(int a) मान अपडेट करने से पहले वैधता जांच (वैलिडेशन) करता है—जैसे ऋणात्मक उम्र या बैलेंस को अस्वीकार करना। केवल गेटर (बिना सेटर) देकर रीड-ओनली प्रॉपर्टी और केवल सेटर (बिना गेटर) देकर राइट-ओनली प्रॉपर्टी (जैसे पासवर्ड सेट करना) बनाई जा सकती है।

3. Invariants, Representation Independence, and Maintainability (इनवेरिएंट्स, रिप्रेजेंटेशन इंडिपेंडेंस और रखरखाव)

[EN] A class invariant is a condition that must remain true throughout an object's life (e.g., Date month is always 1..12, or Stack top index never exceeds capacity). If balance were public, any external line like acc.balance = -5000; could violate bank invariants silently across hundreds of call sites. With encapsulation, all modifications funnel through deposit() and withdraw(), enabling logging, thread synchronization (mutex locking), or validation in one centralized place. Furthermore, representation independence allows changing internal storage (such as storing temperature internally in Kelvin instead of Celsius, or switching a vector to a ring buffer) without breaking external callers of getCelsius().

[HI] क्लास इनवेरिएंट वह शर्त है जो ऑब्जेक्ट के पूरे जीवनकाल में सत्य रहनी चाहिए (जैसे माह 1 से 12 के बीच हो, या बैंक बैलेंस ऋणात्मक न हो)। यदि डेटा public हो, तो बाहरी कोड acc.balance = -5000; लिखकर सीधे इस नियम को तोड़ सकता है। एनकैप्सुलेशन होने पर सारा बदलाव deposit() या withdraw() से होकर गुजरता है, जिससे एक ही स्थान पर वैलिडेशन, लॉगिंग या थ्रेड लॉकिंग जोड़ी जा सकती है। इसके अतिरिक्त, रिप्रेजेंटेशन इंडिपेंडेंस के कारण आंतरिक भंडारण (जैसे सेल्सियस के बजाय केल्विन में स्टोर करना) बदलने पर भी बाहरी getCelsius() कॉल करने वाला कोड नहीं टूटता।

4. Common Anti-Patterns That Leak Data Hiding (डेटा हाइडिंग तोड़ने वाली सामान्य गलतियां (Anti-Patterns))

[EN] Merely making every field private and blindly generating trivial getters and setters for every variable yields weak 'anemic' encapsulation without real behavioral abstraction. A severe C++ encapsulation bug is returning a non-const pointer or non-const reference to a private data member, such as int* getSecretPtr() { return &secret; } or vector<int>& getItems(). Doing so hands external callers direct write access to private memory, letting them mutate or invalidate invariants behind the class's back. Instead, return small primitives by value, return large read-only objects by const reference (const std::string& getName() const), and prefer domain actions like account.withdraw(amt) over raw setBalance().

[HI] हर प्राइवेट वेरिएबल के लिए बिना सोचे-समझे सीधे गेटर और सेटर बना देना कमजोर एनकैप्सुलेशन कहलाता है; बेहतर है कि क्लास अर्थपूर्ण ऑपरेशन (जैसे withdraw) प्रदान करे। C++ में एक बड़ी गलती private मेंबर का नॉन-कॉन्स्ट पॉइंटर या रेफरेंस लौटाना है, जैसे int* getSecretPtr() { return &secret; }। ऐसा करने से बाहरी कोड पॉइंटर के माध्यम से सीधे प्राइवेट मेमोरी को बदल सकता है और डेटा हाइडिंग पूरी तरह निष्फल हो जाती है। इसलिए छोटे मान बाय-वैल्यू और बड़े ऑब्जेक्ट्स const रेफरेंस (const std::string& getName() const) द्वारा लौटाने चाहिए।

Syntax, Formula & Technical Example (Encapsulation and Data Hiding in Object-Oriented C++)

#include <iostream>
class BankAccount {
    double balance; // hidden state
public:
    explicit BankAccount(double b) : balance(b >= 0 ? b : 0) {}
    void deposit(double amt) { if (amt > 0) balance += amt; }
    bool withdraw(double amt) {
        if (amt <= 0 || amt > balance) return false;
        balance -= amt; return true;
    }
    double getBalance() const { return balance; }
};

Encapsulation and Data Hiding in Object-Oriented C++ Quick Reference & Comparison Table

MechanismRole in EncapsulationBest Practice / Exam Rule
EncapsulationBundles data and methods inside a classAchieved syntactically by class/struct grouping
Data HidingBlocks direct external access via privatePrevents external invariant corruption
Getter (Accessor)Reads private state safely (getX() const)Mark const; avoid returning non-const &/*
Setter (Mutator)Updates private state with validationEnforces bounds/rules before modifying fields

Encapsulation and Data Hiding in Object-Oriented C++ Top 20 Bilingual MCQs with Answers

Q1. How does 'Encapsulation' differ technically from 'Data Hiding' in OOP?

(OOP में 'एनकैप्सुलेशन' (Encapsulation) और 'डेटा हाइडिंग' (Data Hiding) में तकनीकी अंतर क्या है?)

(A) Encapsulation applies only to functions and never to variables

(B) Encapsulation deletes unused variables while data hiding encrypts files

(C) Data hiding uses public while encapsulation uses goto

(D) Encapsulation bundles data and methods into one unit, whereas data hiding restricts direct access using private/protected

Correct Answer: (D)

[EN] Encapsulation is the wrapping/bundling of state and behavior; data hiding is specifically restricting visibility to prevent unauthorized direct access.

[HI] एनकैप्सुलेशन डेटा और मेथड्स को एक इकाई में बांधना है, जबकि डेटा हाइडिंग private/protected से सीधे बाहरी एक्सेस को प्रतिबंधित करना है।

Q2. Which keyword should follow the parameter list of a getter method like int getAge() const?

(int getAge() const जैसे गेटर मेथड की पैरामीटर लिस्ट के बाद कौन-सा कीवर्ड लगाना सर्वोत्तम अभ्यास है?)

(A) mutable

(B) friend

(C) const

(D) static

Correct Answer: (C)

[EN] Trailing const promises the compiler that the getter does not modify *this, allowing it to be called on const objects.

[HI] अंत में const लगाने से यह सुनिश्चित होता है कि गेटर ऑब्जेक्ट को बदलेगा नहीं और इसे const ऑब्जेक्ट्स पर भी कॉल किया जा सकेगा।

Q3. What is the primary purpose of a setter (mutator) method compared to making a variable public?

(किसी वेरिएबल को सीधे public बनाने की तुलना में सेटर (म्यूटेटर) मेथड का मुख्य उद्देश्य क्या है?)

(A) To allow any arbitrary negative or corrupt value

(B) To validate incoming values and enforce class invariants before changing internal state

(C) To reduce the file size of the source code

(D) To bypass constructor execution

Correct Answer: (B)

[EN] Setters act as gatekeepers that reject invalid arguments (like negative age or salary) before mutating private members.

[HI] सेटर एक द्वारपाल की तरह काम करता है जो डेटा बदलने से पहले मानों की वैधता (जैसे उम्र >= 0) जांचता है।

Q4. How do you design a read-only property for a private field id inside a C++ class?

(किसी C++ क्लास के अंदर private फील्ड id को रीड-ओनली (Read-only) प्रॉपर्टी कैसे बनाया जाता है?)

(A) Declare id as public const int id without a constructor

(B) Provide a public getter getId() const without providing any public setter

(C) Provide only a setter without a getter

(D) Make the getter private

Correct Answer: (B)

[EN] Exposing only a public const getter allows external code to read id while preventing any external modification after construction.

[HI] केवल पब्लिक const गेटर देकर और कोई सेटर न बनाकर बाहरी कोड को केवल पढ़ने की अनुमति दी जाती है, बदलने की नहीं।

Q5. Which mistake in a getter method completely breaks C++ data hiding?

(किसी गेटर मेथड में कौन-सी गलती C++ की डेटा हाइडिंग को पूरी तरह तोड़ देती है?)

(A) Returning a const std::string& reference

(B) Marking the getter method as const

(C) Returning a non-const pointer or non-const reference to a private data member

(D) Returning a primitive int by value

Correct Answer: (C)

[EN] Returning int* or int& lets external callers write *(obj.getPtr()) = -999; and corrupt private state directly.

[HI] नॉन-कॉन्स्ट पॉइंटर या रेफरेंस लौटाने पर बाहरी कोड सीधे निजी मेमोरी में अमान्य मान लिख सकता है, जिससे डेटा हाइडिंग टूट जाती है।

Q6. Why does encapsulation make refactoring internal data representation easier?

(एनकैप्सुलेशन आंतरिक डेटा संरचना (रिप्रेजेंटेशन) को बाद में बदलना आसान क्यों बनाता है?)

(A) Because private variables automatically convert between int and string

(B) Because callers never invoke public methods

(C) Because compilers rewrite external main() code automatically

(D) External code depends only on public method signatures, not on how private fields are stored

Correct Answer: (D)

[EN] As long as public methods preserve their contract, private storage can change (e.g., array to vector or Celsius to Kelvin) without modifying callers.

[HI] जब तक पब्लिक मेथड्स का इंटरफेस समान रहता है, क्लास के अंदर डेटा स्टोर करने का तरीका बदलने पर भी बाहरी कोड में बदलाव नहीं करना पड़ता।

Q7. What happens if you mark a data member with the mutable keyword (mutable int cacheHits;) inside an encapsulated class?

(एनकैप्सुलेटेड क्लास में किसी डेटा मेंबर के आगे mutable कीवर्ड (mutable int cacheHits;) लगाने से क्या होता है?)

(A) It makes the private member publicly accessible in main()

(B) It can be modified even inside const member functions (such as caching inside a const getter)

(C) It prevents constructors from initializing the member

(D) It turns the member into a compile-time constant

Correct Answer: (B)

[EN] mutable enables logical constness: internal caching or mutexes can update inside const getters without exposing logical state mutation.

[HI] mutable लगाने से लॉजिकल कॉन्स्टनेस बनी रहती है और const गेटर के अंदर भी कैश काउंटर या म्युटेक्स अपडेट किया जा सकता है।

Q8. Suppose const Student s(101); is passed to a function. Which getter declaration can be legally invoked on s?

(मान लीजिए const Student s(101); किसी फंक्शन में पास किया गया है। s पर कौन-सी गेटर घोषणा वैध रूप से कॉल की जा सकती है?)

(A) int getRoll();

(B) void setRoll(int r);

(C) int getRoll() const;

(D) static int getRoll() const;

Correct Answer: (C)

[EN] On a const object, C++ allows calling ONLY member functions marked const (static methods cannot be marked const either).

[HI] const ऑब्जेक्ट पर केवल वही नॉन-स्टैटिक मेंबर फंक्शन्स कॉल किए जा सकते हैं जिनके अंत में const लगा हो।

Q9. Which C++ idiom hides ALL private data members and helper headers behind an opaque pointer in the public header file?

(पब्लिक हेडर फाइल से सभी private मेंबर्स की जानकारी तक छुपाने के लिए C++ की कौन-सी तकनीक ओपेक पॉइंटर (opaque pointer) का उपयोग करती है?)

(A) Multiple inheritance diamond

(B) Global extern arrays

(C) Macro token pasting

(D) Pimpl (Pointer to Implementation) idiom

Correct Answer: (D)

[EN] The Pimpl idiom stores private fields in a forward-declared Impl struct pointed to by a private pointer, providing strong binary & compile-time encapsulation.

[HI] Pimpl (Pointer to Implementation) तकनीक निजी डेटा को अलग Impl स्ट्रक्चर के पॉइंटर में रखती है, जिससे हेडर से आंतरिक लेआउट भी छिप जाता है।

Q10. If a class implements only setPassword(const string& p) without getPassword(), what kind of encapsulated property is password?

(यदि कोई क्लास केवल setPassword(const string& p) देती है और getPassword() नहीं बनाती, तो पासवर्ड किस प्रकार की प्रॉपर्टी है?)

(A) Global property

(B) Write-only property

(C) Read-only property

(D) Unencapsulated property

Correct Answer: (B)

[EN] Having a setter without a getter permits updating/verifying state without exposing the stored value externally (write-only).

[HI] बिना गेटर के केवल सेटर देने पर बाहरी कोड मान सेट कर सकता है पर उसे पढ़ नहीं सकता, जिसे राइट-ओनली प्रॉपर्टी कहते हैं।

Q11. Why is account.withdraw(500) preferred over account.setBalance(account.getBalance() - 500) in well-encapsulated OOP design?

(अच्छे OOP डिज़ाइन में account.setBalance(account.getBalance() - 500) के बजाय account.withdraw(500) को प्राथमिकता क्यों दी जाती है?)

(A) setBalance() cannot accept double parameters

(B) withdraw() encapsulates atomic validation, overdraft checks, and auditing inside the class instead of scattering logic to callers

(C) withdraw() uses fewer characters in ASCII

(D) withdraw() makes balance public automatically

Correct Answer: (B)

[EN] Rich domain methods keep invariants, thread safety, and business rules centralized inside the class rather than exposing raw state manipulation.

[HI] withdraw() जैसे डोमेन मेथड्स ओवरड्राफ्ट जांच, लॉगिंग और नियमों को कॉलर्स पर छोड़ने के बजाय क्लास के भीतर सुरक्षित रखते हैं।

Q12. Which access specifier hides internal data from outside callers while still allowing subclasses to read/modify it directly?

(कौन-सा एक्सेस स्पेसिफायर बाहरी कॉलर्स से डेटा छुपाता है लेकिन सबक्लासेस को उसे सीधे पढ़ने/बदलने की अनुमति देता है?)

(A) public

(B) inline

(C) protected

(D) private

Correct Answer: (C)

[EN] protected hides members from external functions like main() while permitting direct access inside derived classes (note: private gives even stricter hiding).

[HI] protected बाहरी कॉलर्स से डेटा छुपाता है पर डिराइव्ड क्लासेस को सीधे एक्सेस देता है।

Q13. What happens if a setter void setMarks(int m) receives -15 when validation requires 0 <= m <= 100?

(यदि वैलिडेशन नियम 0 <= m <= 100 होने पर सेटर void setMarks(int m) को -15 मान प्राप्त हो, तो अच्छी एनकैप्सुलेटेड क्लास क्या करती है?)

(A) Deletes the Student object immediately

(B) Rejects the update (leaves marks unchanged, returns false, or throws std::invalid_argument)

(C) Silently wraps -15 to 241 in unsigned arithmetic

(D) Converts marks into a public variable

Correct Answer: (B)

[EN] A validating setter refuses invalid input by ignoring it, returning an error status, or throwing an exception so the invariant holds.

[HI] वैधता जांचने वाला सेटर अमान्य मान को अस्वीकार करके पुराना वैध मान सुरक्षित रखता है या त्रुटि/एक्सेप्शन लौटाता है।

Q14. What risk occurs if a class stores a raw pointer char *name and its constructor simply copies callerPtr without allocating its own copy?

(यदि कोई क्लास char *name रखे और कंस्ट्रक्टर या सेटर बिना नई कॉपी बनाए सीधे कॉलर का पॉइंटर स्टोर कर ले, तो क्या खतरा होता है?)

(A) sizeof(char*) doubles automatically

(B) External code retaining callerPtr can mutate or free the buffer later, breaking encapsulation

(C) The string becomes immutable in memory

(D) The compiler automatically converts char* to int

Correct Answer: (B)

[EN] Storing an external mutable pointer without defensive copying lets the caller alter or delete internal state externally.

[HI] बाहरी पॉइंटर की डिफेंसिव कॉपी न बनाने पर कॉलर बाद में उस बफर को बदल या फ्री करके क्लास के आंतरिक डेटा को बिगाड़ सकता है।

Q15. Which C++ access control mechanism represents the tightest level of data hiding?

(C++ में डेटा हाइडिंग का सबसे सख्त स्तर किस एक्सेस स्पेसिफायर द्वारा प्राप्त होता है?)

(A) public

(B) protected

(C) struct default

(D) private

Correct Answer: (D)

[EN] private blocks both external callers AND derived classes, making it the strictest access control level.

[HI] private बाहरी कोड और डिराइव्ड क्लासेस दोनों के सीधे एक्सेस को रोकता है, इसलिए यह सबसे सख्त स्तर है।

Q16. Which of the following statements about encapsulation and data hiding in C++ is NOT true?

(C++ में एनकैप्सुलेशन और डेटा हाइडिंग के बारे में निम्नलिखित में से कौन-सा कथन सत्य नहीं है?)

(A) Setters allow central validation and logging before modifying internal state

(B) Encapsulation groups related variables and member functions into a single class

(C) Getters marked const can be safely called on const object references

(D) Making all data members public with getters and setters is the definition of strict data hiding

Correct Answer: (D)

[EN] Public data members provide NO data hiding at all, because external code can bypass getters and setters completely.

[HI] सभी डेटा मेंबर्स को public बना देने से कोई डेटा हाइडिंग नहीं होती, क्योंकि बाहरी कोड गेटर्स/सेटर्स को अनदेखा करके सीधे डेटा बदल सकता है।

Q17. Within Encapsulation and Data Hiding in Object-Oriented C++, which mechanism directly maps to: "Bundles data and methods inside a class"?

(Encapsulation and Data Hiding in Object-Oriented C++ में कौन सा Mechanism सीधे "Bundles data and methods inside a class" से संबंधित है?)

(A) Setter (Mutator)

(B) Data Hiding

(C) Encapsulation

(D) Getter (Accessor)

Correct Answer: (C)

[EN] In Encapsulation and Data Hiding in Object-Oriented C++, 'Encapsulation' handles 'Bundles data and methods inside a class' (Achieved syntactically by class/struct grouping).

[HI] Encapsulation and Data Hiding in Object-Oriented C++ में 'Encapsulation' का कार्य/विवरण 'Bundles data and methods inside a class' (Achieved syntactically by class/struct grouping) है।

Q18. Which option accurately describes 'Data Hiding' in Encapsulation and Data Hiding in Object-Oriented C++?

(Encapsulation and Data Hiding in Object-Oriented C++ के संदर्भ में 'Data Hiding' की सही विशेषता कौन सी है?)

(A) Bundles data and methods inside a class

(B) Blocks direct external access via private

(C) Updates private state with validation

(D) Reads private state safely (getX() const)

Correct Answer: (B)

[EN] 'Data Hiding' provides 'Blocks direct external access via private' — key point: Prevents external invariant corruption.

[HI] 'Data Hiding' का मुख्य विवरण 'Blocks direct external access via private' है (Prevents external invariant corruption)।

Q19. In Encapsulation and Data Hiding in Object-Oriented C++, identify the correct parameter for the specification: "Reads private state safely (getX() const)".

(Encapsulation and Data Hiding in Object-Oriented C++ में "Reads private state safely (getX() const)" विवरण किस विकल्प के लिए सही है?)

(A) Encapsulation

(B) Setter (Mutator)

(C) Getter (Accessor)

(D) Data Hiding

Correct Answer: (C)

[EN] 'Getter (Accessor)' corresponds to 'Reads private state safely (getX() const)' (Mark const; avoid returning non-const &/*).

[HI] 'Getter (Accessor)' का संबंध 'Reads private state safely (getX() const)' (Mark const; avoid returning non-const &/*) से है।

Q20. Considering Encapsulation and Data Hiding in Object-Oriented C++, what key behavior defines 'Setter (Mutator)'?

(Encapsulation and Data Hiding in Object-Oriented C++ में 'Setter (Mutator)' की मुख्य भूमिका या मान क्या है?)

(A) Blocks direct external access via private

(B) Bundles data and methods inside a class

(C) Updates private state with validation

(D) Reads private state safely (getX() const)

Correct Answer: (C)

[EN] 'Setter (Mutator)' represents 'Updates private state with validation' (Enforces bounds/rules before modifying fields).

[HI] 'Setter (Mutator)' का अर्थ/मान 'Updates private state with validation' है (Enforces bounds/rules before modifying fields)।

Encapsulation and Data Hiding in Object-Oriented C++ — Exam FAQs

Q1: If a class has 5 private fields, should I automatically write 5 getters and 5 setters?

यदि किसी क्लास में 5 private फील्ड्स हैं, तो क्या मुझे स्वतः सभी के 5 गेटर और 5 सेटर बना देने चाहिए?

[EN] No; expose only the behaviors and read-only queries callers actually need. Blindly adding setters for every internal field defeats encapsulation.

[HI] नहीं; केवल वही मेथड्स और रीड-ओनली क्वेरीज़ दें जिनकी कॉलर को सचमुच जरूरत है। हर फील्ड के लिए सेटर बनाना एनकैप्सुलेशन को कमजोर करता है।

Q2: Why is returning const std::string& from a getter better than returning std::string by value?

गेटर से बाय-वैल्यू std::string लौटाने की तुलना में const std::string& लौटाना क्यों बेहतर माना जाता है?

[EN] Returning const std::string& avoids copying the string buffer in memory while still preventing the caller from modifying the private string.

[HI] const std::string& लौटाने से मेमोरी में स्ट्रिंग कॉपी करने का खर्च बचता है और const होने के कारण कॉलर प्राइवेट स्ट्रिंग को बदल भी नहीं सकता।

Q3: How does encapsulation help in unit testing and debugging?

एनकैप्सुलेशन यूनिट टेस्टिंग और डीबगिंग में किस प्रकार मदद करता है?

[EN] Because all mutations pass through setters or public methods, you can place a single breakpoint or assertion inside the setter to catch every illegal modification immediately.

[HI] चूंकि सारा बदलाव केवल सेटर्स या पब्लिक मेथड्स से होता है, इसलिए सेटर में एक ही ब्रेकपॉइंट या असर्शन लगाकर हर गलत बदलाव को तुरंत पकड़ा जा सकता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)