Access Specifiers: private, public, protected in C++ Notes in Hindi & English : एक्सेस स्पेसिफायर्स: private (क्लास के अंदर), public और protected और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

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

Overview: C++ controls member visibility through three access specifiers—private, public, and protected—governing whether external code, class methods, or derived subclasses can access a member. Additionally, inheritance visibility modes (public, protected, private) determine how base-class permissions transform inside derived classes.

अध्याय सार: C++ में तीन एक्सेस स्पेसिफायर्स—private, public और protected—यह नियंत्रित करते हैं कि किसी क्लास के मेंबर्स को बाहरी कोड, उसी क्लास के मेथड्स या डिराइव्ड क्लासेस एक्सेस कर सकती हैं या नहीं। इसके अतिरिक्त, इनहेरिटेंस मोड (public, protected, private) यह तय करता है कि बेस क्लास के मेंबर्स की विजिबिलिटी चाइल्ड क्लास में किस रूप में प्राप्त होगी।

Access Specifiers: private, public, protected in C++: Detailed Notes & Architecture (एक्सेस स्पेसिफायर्स: private (क्लास के अंदर), public और protected)

1. The Three Access Specifiers: private, public, protected (तीन एक्सेस स्पेसिफायर्स: private, public और protected)

[EN] Access specifiers followed by a colon (:) partition a class body into visibility sections and can appear multiple times in any order. private members are accessible exclusively by member functions of the same class and declared friends; neither main() nor derived classes can touch them directly. public members form the external API of the class and are accessible from anywhere an object is visible. protected members sit in between: inaccessible to external callers (like main()) through an object, yet directly accessible inside member functions of derived classes.

[HI] कोलन (:) के साथ लिखे जाने वाले एक्सेस स्पेसिफायर्स क्लास बॉडी को विजिबिलिटी क्षेत्रों में बांटते हैं और किसी भी क्रम में कई बार आ सकते हैं। private मेंबर्स केवल उसी क्लास के मेंबर फंक्शन्स और घोषित friends द्वारा ही एक्सेस किए जा सकते हैं; न तो main() और न ही डिराइव्ड क्लास इन्हें सीधे छू सकती है। public मेंबर्स क्लास का बाहरी इंटरफेस बनाते हैं और प्रोग्राम में कहीं से भी ऑब्जेक्ट द्वारा एक्सेस किए जा सकते हैं। protected मेंबर्स मुख्य कोड (जैसे main()) से छिपे रहते हैं लेकिन डिराइव्ड (चाइल्ड) क्लासेस के अंदर सीधे एक्सेस हो जाते हैं।

2. Inheritance Access Modes and Visibility Matrix (इनहेरिटेंस एक्सेस मोड्स और विजिबिलिटी मैट्रिक्स)

[EN] When deriving a subclass via class Derived : mode Base, the inheritance mode acts as a ceiling restricting inherited public and protected members of Base. With public inheritance (representing an "is-a" relationship), Base's public stays public and Base's protected stays protected in Derived. With protected inheritance, both public and protected members of Base become protected in Derived; with private inheritance (the default when using the class keyword without a mode), both become private in Derived. Crucially, Base's private members are NEVER directly accessible inside Derived regardless of inheritance mode, though they still exist in Derived's memory layout and can be accessed via Base's public/protected methods.

[HI] जब class Derived : mode Base लिखा जाता है, तो इनहेरिटेंस मोड बेस क्लास के public और protected मेंबर्स की अधिकतम विजिबिलिटी को सीमित करता है। public इनहेरिटेंस ('is-a' संबंध) में बेस के public मेंबर्स Derived में public और protected मेंबर्स protected ही रहते हैं। protected इनहेरिटेंस में बेस के public और protected दोनों Derived में protected बन जाते हैं, जबकि private इनहेरिटेंस (class का डिफ़ॉल्ट) में दोनों private हो जाते हैं। ध्यान रहे कि किसी भी मोड में बेस क्लास के private मेंबर्स Derived में सीधे एक्सेस नहीं होते, यद्यपि वे ऑब्जेक्ट की मेमोरी में मौजूद रहते हैं।

3. Friend Functions and Friend Classes as Controlled Exceptions (फ्रेंड फंक्शन और फ्रेंड क्लास: नियंत्रित अपवाद)

[EN] Sometimes an external helper function (such as operator<< for printing) or a tightly coupled helper class needs direct access to private/protected internals without making those fields public to the entire world. Declaring friend void inspect(const Box&); or friend class Debugger; inside Box grants that named external entity full access to Box's private and protected members. Friendship in C++ has three strict rules: it is NOT mutual (if A declares B as friend, B does not automatically make A a friend), NOT transitive (friend of a friend gets no access), and NOT inherited by subclasses. Because a friend function is not a member of the class, it has no this pointer and must receive an object explicitly as a parameter.

[HI] कभी-कभी किसी बाहरी फंक्शन (जैसे आउटपुट के लिए operator<<) या सहायक क्लास को private/protected डेटा तक सीधी पहुँच की आवश्यकता होती है बिना उसे सबके लिए public बनाए। क्लास के अंदर friend void inspect(const Box&); या friend class Debugger; घोषित करने से उस बाहरी इकाई को private और protected मेंबर्स की पूर्ण पहुँच मिल जाती है। C++ में फ्रेंडशिप के तीन सख्त नियम हैं: यह पारस्परिक (mutual) नहीं होती, संक्रामक (transitive) नहीं होती, और डिराइव्ड क्लासेस को विरासत में (inherited) नहीं मिलती। चूंकि फ्रेंड फंक्शन क्लास का मेंबर नहीं होता, इसलिए उसमें this पॉइंटर नहीं होता और उसे ऑब्जेक्ट पैरामीटर के रूप में देना पड़ता है।

4. Private Constructors, Access Checks, and Compile-Time Enforcement (प्राइवेट कंस्ट्रक्टर और कंपाइल-टाइम एक्सेस नियंत्रण)

[EN] Access checking in C++ is enforced strictly at compile time rather than runtime, incurring zero CPU overhead when a program executes. Remember that access specifiers also apply to methods, nested types, and constructors—not just data variables. Making a constructor private prevents external code from instantiating objects arbitrarily, a technique widely used in the Singleton pattern (returning a single instance via a static getInstance() method) or Factory patterns. Furthermore, access control operates on the static type at the call site: if a Derived class overrides a public virtual function as private, calling it through a Base* pointer still compiles because Base's declaration is public.

[HI] C++ में एक्सेस विजिबिलिटी की जांच पूरी तरह कंपाइल समय पर होती है, जिससे प्रोग्राम चलते समय कोई अतिरिक्त CPU ओवरहेड नहीं लगता। ध्यान रखें कि एक्सेस स्पेसिफायर्स केवल डेटा वेरिएबल्स पर ही नहीं, बल्कि मेथड्स, नेस्टेड टाइप्स और कंस्ट्रक्टर्स पर भी लागू होते हैं। कंस्ट्रक्टर को private बनाकर बाहरी कोड को सीधे ऑब्जेक्ट बनाने से रोका जा सकता है, जिसका उपयोग Singleton डिज़ाइन पैटर्न (static getInstance() द्वारा) में होता है। चूंकि एक्सेस जांच पॉइंटर के स्टैटिक टाइप पर होती है, यदि Base में कोई वर्चुअल फंक्शन public है तो Derived द्वारा उसे private करने पर भी Base* के जरिए कॉल कंपाइल हो जाता है।

Syntax, Formula & Technical Example (Access Specifiers: private, public, protected in C++)

#include <iostream>
class Base {
private:   int sec = 10;
protected: int prot = 20;
public:    int pub = 30;
};
class Child : public Base {
public:
    void show() { std::cout << prot + pub << '\n'; } // sec is inaccessible
};
int main() {
    Child c;
    c.show();
    std::cout << c.pub << '\n';
}

Access Specifiers: private, public, protected in C++ Quick Reference & Comparison Table

Specifier / ModeAccess Scope & RuleDerived Class & External Note
private memberAccessible ONLY inside same class & friendsInaccessible to both derived class and main()
protected memberAccessible in class, friends & derived classesHidden from external callers like main()
public memberAccessible anywhere the object is visibleAccessible in derived class and via object in main()
private inheritanceDefault mode for class Derived : BaseTurns Base public & protected into Derived private

Access Specifiers: private, public, protected in C++ Top 20 Bilingual MCQs with Answers

Q1. Which access specifier allows a member to be accessed inside derived classes while keeping it hidden from unrelated external functions like main()?

(कौन-सा एक्सेस स्पेसिफायर किसी मेंबर को डिराइव्ड क्लासेस में एक्सेस करने की अनुमति देता है, जबकि main() जैसे बाहरी कोड से छिपाए रखता है?)

(A) private

(B) static

(C) protected

(D) public

Correct Answer: (C)

[EN] protected members are accessible inside the defining class, friends, and derived classes, but cannot be accessed directly via objects in main().

[HI] protected मेंबर्स उसी क्लास, फ्रेंड्स और डिराइव्ड क्लासेस में सीधे एक्सेस होते हैं, पर main() में ऑब्जेक्ट से एक्सेस नहीं होते।

Q2. If class Derived : Base {}; is written without specifying public, protected, or private before Base, which inheritance mode does C++ apply?

(यदि class Derived : Base {}; में Base से पहले कोई मोड न लिखा जाए, तो C++ कौन-सा इनहेरिटेंस मोड लागू करता है?)

(A) protected inheritance

(B) friend inheritance

(C) private inheritance

(D) public inheritance

Correct Answer: (C)

[EN] Just as class members default to private, omitting the inheritance specifier on a class defaults to private inheritance (whereas struct defaults to public).

[HI] class कीवर्ड में बिना मोड लिखे इनहेरिट करने पर डिफ़ॉल्ट रूप से private इनहेरिटेंस लागू होता है (जबकि struct में public)।

Q3. Under public inheritance (class Child : public Parent), how do Parent's protected members behave inside Child?

(पब्लिक इनहेरिटेंस (class Child : public Parent) के तहत Parent के protected मेंबर्स Child में किस विजिबिलिटी के साथ आते हैं?)

(A) They become private in Child

(B) They remain protected in Child

(C) They are deleted and absent from memory

(D) They become public in Child

Correct Answer: (B)

[EN] Public inheritance preserves Base's public as public and Base's protected as protected inside Derived.

[HI] पब्लिक इनहेरिटेंस में बेस क्लास के public मेंबर्स public और protected मेंबर्स protected ही बने रहते हैं।

Q4. Under protected inheritance (class Child : protected Parent), what happens to Parent's public members inside Child?

(प्रोटेक्टेड इनहेरिटेंस (class Child : protected Parent) में Parent के public मेंबर्स Child के अंदर क्या बन जाते हैं?)

(A) private

(B) protected

(C) friend

(D) public

Correct Answer: (B)

[EN] Protected inheritance lowers Base's public members to protected inside Child (while Base's protected stay protected).

[HI] प्रोटेक्टेड इनहेरिटेंस में बेस के public मेंबर्स Child में protected बन जाते हैं।

Q5. Which members of a Base class can NEVER be directly accessed by name inside a Derived class regardless of whether inheritance is public, protected, or private?

(इनहेरिटेंस चाहे public, protected या private हो, बेस क्लास के कौन-से मेंबर्स डिराइव्ड क्लास में नाम से सीधे एक्सेस नहीं किए जा सकते?)

(A) public members

(B) virtual functions declared public

(C) protected members

(D) private members

Correct Answer: (D)

[EN] private members of Base are strictly encapsulated within Base and cannot be directly accessed in Derived (unless Base explicitly names Derived as a friend).

[HI] बेस क्लास के private मेंबर्स किसी भी इनहेरिटेंस मोड में डिराइव्ड क्लास द्वारा सीधे एक्सेस नहीं किए जा सकते।

Q6. What happens if class A declares class B as a friend (friend class B;)?

(यदि class A अपने अंदर friend class B; घोषित करे, तो क्या होता है?)

(A) Both A and B automatically access each other's private members

(B) All members of A become public in main()

(C) B's member functions can access A's private and protected members, but A does NOT gain access to B's private members

(D) Derived classes of B also become friends of A

Correct Answer: (C)

[EN] Friendship in C++ is one-way (not symmetric/mutual) and not inherited.

[HI] C++ में फ्रेंडशिप एकतरफा (non-mutual) और नॉन-इनहेरिटेड होती है; B को A का एक्सेस मिलता है, पर A को B का नहीं।

Q7. At which phase of program execution are C++ access specifiers (private/protected/public) checked and enforced?

(C++ में एक्सेस स्पेसिफायर्स (private/protected/public) की जांच और लागूकरण किस चरण में होता है?)

(A) Link time only

(B) Compile time

(C) Runtime via garbage collector

(D) Operating system kernel trap

Correct Answer: (B)

[EN] C++ verifies all access permissions statically at compile time with zero runtime cost.

[HI] C++ सभी एक्सेस नियमों की जांच पूरी तरह कंपाइल टाइम पर करता है, जिससे रनटाइम पर कोई ओवरहेड नहीं होता।

Q8. How many times can the public: or private: label appear inside a single C++ class definition?

(एक ही C++ क्लास परिभाषा के अंदर public: या private: लेबल कितनी बार आ सकते हैं?)

(A) Maximum twice per class

(B) Only once each, with public always first

(C) Any number of times in any order

(D) Only once each, with private always first

Correct Answer: (C)

[EN] C++ allows access specifier labels to appear repeatedly and in any order within a class body.

[HI] C++ क्लास बॉडी के भीतर एक्सेस स्पेसिफायर लेबल किसी भी क्रम में कितनी भी बार आ सकते हैं।

Q9. In struct Derived : Base {}; (where both Base and Derived use the struct keyword), what is the default inheritance mode?

(struct Derived : Base {}; (जहाँ Base और Derived दोनों struct हैं) में डिफ़ॉल्ट इनहेरिटेंस मोड क्या होता है?)

(A) private inheritance

(B) public inheritance

(C) No inheritance is allowed for structs

(D) protected inheritance

Correct Answer: (B)

[EN] For struct, both default member visibility and default base-class inheritance are public.

[HI] struct के लिए डिफ़ॉल्ट मेंबर विजिबिलिटी और डिफ़ॉल्ट इनहेरिटेंस मोड दोनों public होते हैं।

Q10. Why might a designer declare a class's constructor in the private section?

(कोई प्रोग्रामर किसी क्लास के कंस्ट्रक्टर को private सेक्शन में क्यों घोषित कर सकता है?)

(A) To restrict direct object creation outside the class, such as implementing a Singleton or Factory pattern

(B) To allow every external function to create objects freely

(C) To speed up arithmetic operations

(D) Because constructors are forbidden in public

Correct Answer: (A)

[EN] A private constructor prevents arbitrary external instantiation while allowing static member functions or friends to control object creation.

[HI] प्राइवेट कंस्ट्रक्टर बाहरी कोड को सीधे ऑब्जेक्ट बनाने से रोकता है, जिससे Singleton या Factory पैटर्न द्वारा नियंत्रित निर्माण संभव होता है।

Q11. If class X has a private member int secret; and a friend function void peek(X obj), how does peek access secret?

(यदि class X में private मेंबर int secret; और फ्रेंड फंक्शन void peek(X obj) है, तो peek फंक्शन secret को कैसे एक्सेस करेगा?)

(A) secret directly without an object

(B) X::secret

(C) this->secret

(D) obj.secret

Correct Answer: (D)

[EN] Because a friend function is not a member of X, it lacks a this pointer and accesses non-static members via an object (obj.secret).

[HI] फ्रेंड फंक्शन क्लास का मेंबर नहीं होता इसलिए उसमें this पॉइंटर नहीं होता; वह ऑब्जेक्ट के माध्यम से obj.secret लिखकर एक्सेस करता है।

Q12. If A is a friend of B, and B is a friend of C, can A access C's private members automatically?

(यदि A, B का फ्रेंड है और B, C का फ्रेंड है, तो क्या A स्वतः C के private मेंबर्स को एक्सेस कर सकता है?)

(A) Yes; friendship chains transitively

(B) No; friendship in C++ is not transitive

(C) Yes, if public inheritance is used

(D) Only inside main()

Correct Answer: (B)

[EN] Friendship in C++ is explicitly non-transitive: A must be declared a friend directly inside C to access C's private data.

[HI] नहीं; C++ में फ्रेंडशिप संक्रामक (transitive) नहीं होती, इसलिए C के अंदर भी A को सीधे friend घोषित करना होगा।

Q13. In struct S : class B {}; (where class B has public int x;), what is the visibility of x inside S?

(यदि class B में public int x; है और struct S : B {}; लिखा जाता है, तो S में x की विजिबिलिटी क्या होगी?)

(A) public (because S is a struct, defaulting inheritance to public)

(B) protected

(C) Compilation error because struct cannot inherit class

(D) private (because B is a class)

Correct Answer: (A)

[EN] The default inheritance mode depends on the derived type keyword (struct -> public, class -> private), NOT the base type keyword.

[HI] डिफ़ॉल्ट इनहेरिटेंस मोड डिराइव्ड टाइप के कीवर्ड (struct के लिए public) पर निर्भर करता है, न कि बेस क्लास के कीवर्ड पर।

Q14. What happens if main() tries to read obj.prot where prot is declared under protected: inside Base?

(यदि main() में obj.prot पढ़ने का प्रयास किया जाए जहाँ prot Base के protected: सेक्शन में है, तो क्या होगा?)

(A) It prints 0 silently

(B) It compiles but throws a runtime exception

(C) It prints the memory address of prot

(D) Compile-time error stating that prot is protected within this context

Correct Answer: (D)

[EN] Outside code such as main() cannot access protected members via an object; protected only grants access to derived classes and friends.

[HI] main() जैसा बाहरी कोड ऑब्जेक्ट के माध्यम से protected मेंबर को एक्सेस नहीं कर सकता, जिससे कंपाइल-टाइम एरर आता है।

Q15. With private inheritance (class D : private B), how can D selectively expose one inherited public method foo() of B to D's callers?

(private इनहेरिटेंस (class D : private B) में D अपने कॉलर्स के लिए B के केवल एक मेथड foo() को फिर से public कैसे बना सकता है?)

(A) Declare B as static inside D

(B) Write using B::foo; inside the public: section of D

(C) It is impossible in C++

(D) Write friend B::foo; in main()

Correct Answer: (B)

[EN] A using-declaration (using B::foo;) inside D's public section restores public access to that specific base member.

[HI] D के public: सेक्शन में using B::foo; लिखकर चुनिंदा बेस मेथड को फिर से public बनाया जा सकता है।

Q16. Which of the following statements about C++ access specifiers is NOT true?

(C++ एक्सेस स्पेसिफायर्स के बारे में निम्नलिखित में से कौन-सा कथन सत्य नहीं है?)

(A) private is the default visibility in a C++ class

(B) Public inheritance preserves public and protected access in the derived class

(C) Friends of a class can access both private and protected members

(D) protected members of a class can be accessed directly through an object inside main()

Correct Answer: (D)

[EN] protected members are inaccessible via objects in unrelated code such as main(); they are accessible only inside the class, its friends, and derived classes.

[HI] protected मेंबर्स को main() में ऑब्जेक्ट के माध्यम से सीधे एक्सेस नहीं किया जा सकता; वे केवल क्लास, फ्रेंड्स और डिराइव्ड क्लासेस में उपलब्ध होते हैं।

Q17. Within Access Specifiers: private, public, protected in C++, which specifier / mode directly maps to: "Accessible ONLY inside same class & friends"?

(Access Specifiers: private, public, protected in C++ में कौन सा Specifier / Mode सीधे "Accessible ONLY inside same class & friends" से संबंधित है?)

(A) private inheritance

(B) protected member

(C) public member

(D) private member

Correct Answer: (D)

[EN] In Access Specifiers: private, public, protected in C++, 'private member' handles 'Accessible ONLY inside same class & friends' (Inaccessible to both derived class and main()).

[HI] Access Specifiers: private, public, protected in C++ में 'private member' का कार्य/विवरण 'Accessible ONLY inside same class & friends' (Inaccessible to both derived class and main()) है।

Q18. Which option accurately describes 'protected member' in Access Specifiers: private, public, protected in C++?

(Access Specifiers: private, public, protected in C++ के संदर्भ में 'protected member' की सही विशेषता कौन सी है?)

(A) Accessible in class, friends & derived classes

(B) Accessible ONLY inside same class & friends

(C) Accessible anywhere the object is visible

(D) Default mode for class Derived : Base

Correct Answer: (A)

[EN] 'protected member' provides 'Accessible in class, friends & derived classes' — key point: Hidden from external callers like main().

[HI] 'protected member' का मुख्य विवरण 'Accessible in class, friends & derived classes' है (Hidden from external callers like main())।

Q19. In Access Specifiers: private, public, protected in C++, identify the correct parameter for the specification: "Accessible anywhere the object is visible".

(Access Specifiers: private, public, protected in C++ में "Accessible anywhere the object is visible" विवरण किस विकल्प के लिए सही है?)

(A) protected member

(B) private inheritance

(C) public member

(D) private member

Correct Answer: (C)

[EN] 'public member' corresponds to 'Accessible anywhere the object is visible' (Accessible in derived class and via object in main()).

[HI] 'public member' का संबंध 'Accessible anywhere the object is visible' (Accessible in derived class and via object in main()) से है।

Q20. Considering Access Specifiers: private, public, protected in C++, what key behavior defines 'private inheritance'?

(Access Specifiers: private, public, protected in C++ में 'private inheritance' की मुख्य भूमिका या मान क्या है?)

(A) Default mode for class Derived : Base

(B) Accessible ONLY inside same class & friends

(C) Accessible anywhere the object is visible

(D) Accessible in class, friends & derived classes

Correct Answer: (A)

[EN] 'private inheritance' represents 'Default mode for class Derived : Base' (Turns Base public & protected into Derived private).

[HI] 'private inheritance' का अर्थ/मान 'Default mode for class Derived : Base' है (Turns Base public & protected into Derived private)।

Access Specifiers: private, public, protected in C++ — Exam FAQs

Q1: Does using private or protected access specifiers make data unhackable in RAM?

क्या private या protected लगाने से RAM में डेटा हैकिंग से सुरक्षित हो जाता है?

[EN] No; access specifiers enforce compile-time software engineering boundaries against accidental misuse, not OS-level memory encryption or runtime security isolation.

[HI] नहीं; एक्सेस स्पेसिफायर्स केवल कंपाइल-टाइम पर अनजाने दुरुपयोग को रोकने के सॉफ्टवेयर नियम हैं, वे RAM एन्क्रिप्शन या रनटाइम सुरक्षा नहीं देते।

Q2: Why does C++ provide protected instead of just public and private?

C++ में केवल public और private के बजाय protected क्यों दिया गया है?

[EN] protected lets derived classes customize or reuse internal helpers directly while still hiding them from unrelated external callers.

[HI] protected डिराइव्ड क्लासेस को आंतरिक हेल्पर्स सीधे उपयोग करने की सुविधा देता है जबकि बाहरी कॉलर्स से उन्हें छिपाकर रखता है।

Q3: Do friend declarations violate encapsulation?

क्या friend घोषणाएं एनकैप्सुलेशन का उल्लंघन करती हैं?

[EN] When used sparingly for tightly coupled operations like operator<< or helper iterators, friends actually strengthen encapsulation by keeping fields private instead of exposing public setters.

[HI] यदि operator<< जैसे निकट संबंधी कार्यों के लिए सीमित रूप से प्रयोग किया जाए, तो friend फील्ड्स को public करने के बजाय private रखकर एनकैप्सुलेशन को मजबूत ही करता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)