Inheritance in C++: Single, Multilevel, Multiple, Hierarchical Notes in Hindi & English : इनहेरिटेंस: सिंगल, मल्टीलेवल, मल्टीपल और हायरार्किकल इनहेरिटेंस और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

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

Overview: Inheritance in C++ enables a derived class to acquire properties and behaviors from one or more base classes, establishing an IS-A relationship and promoting code reuse under ISO/IEC 14882. C++ supports single, multilevel, multiple, and hierarchical inheritance models alongside public, protected, and private visibility modes that govern member accessibility.

अध्याय सार: C++ में इनहेरिटेंस एक डिराइव्ड क्लास को एक या अधिक बेस क्लास से प्रॉपर्टीज और मेथड्स प्राप्त करने की सुविधा देता है, जिससे IS-A संबंध और कोड रीयूजबिलिटी बनती है। C++ सिंगल, मल्टीलेवल, मल्टीपल और हायरार्किकल इनहेरिटेंस प्रकारों के साथ-साथ public, protected और private एक्सेस मोड्स को सपोर्ट करता है।

Inheritance in C++: Single, Multilevel, Multiple, Hierarchical: Detailed Notes & Architecture (इनहेरिटेंस: सिंगल, मल्टीलेवल, मल्टीपल और हायरार्किकल इनहेरिटेंस)

1. Core Inheritance Types in C++ (C++ में इनहेरिटेंस के मुख्य प्रकार)

[EN] Single inheritance derives one class from a single base class (class B : public A), creating a direct parent-child hierarchy. Multilevel inheritance forms a chain (A -> B -> C) where class C inherits from B, which itself inherits from A. Multiple inheritance allows one derived class to inherit directly from two or more base classes (class C : public A, public B), whereas hierarchical inheritance derives multiple sibling classes (B, C, D) from a single common base class A.

[HI] सिंगल इनहेरिटेंस में एक डिराइव्ड क्लास केवल एक बेस क्लास से गुण लेती है (class B : public A)। मल्टीलेवल इनहेरिटेंस एक श्रृंखला (A -> B -> C) बनाता है जहाँ क्लास C, B से और B, A से इनहेरिट करती है। मल्टीपल इनहेरिटेंस में एक क्लास दो या अधिक बेस क्लास (class C : public A, public B) से इनहेरिट करती है, जबकि हायरार्किकल इनहेरिटेंस में एक बेस क्लास A से कई सिबलिंग क्लासेस (B, C, D) बनती हैं।

2. Visibility Modes and Access Specifiers (विजिबिलिटी मोड्स और एक्सेस स्पेसिफायर्स)

[EN] The inheritance access specifier (public, protected, or private) caps the visibility of inherited base members inside the derived class. Under public inheritance, public base members remain public and protected members remain protected; under protected inheritance, both become protected; under private inheritance, both become private. Base private members always exist in the derived object memory layout (affecting sizeof), yet remain inaccessible to derived methods; note that default inheritance is private for class and public for struct.

[HI] इनहेरिटेंस एक्सेस स्पेसिफायर (public, protected, या private) डिराइव्ड क्लास के भीतर इनहेरिट किए गए बेस मेंबर्स की विजिबिलिटी तय करता है। public इनहेरिटेंस में public मेंबर public व protected मेंबर protected रहते हैं; protected में दोनों protected हो जाते हैं; तथा private में दोनों private बन जाते हैं। बेस के private मेंबर्स मेमोरी (sizeof) में मौजूद रहते हैं पर डिराइव्ड क्लास द्वारा सीधे एक्सेस नहीं किए जा सकते; class के लिए डिफ़ॉल्ट मोड private और struct के लिए public होता है।

3. Constructor and Destructor Execution Order (कंस्ट्रक्टर और डिस्ट्रक्टर के निष्पादन का क्रम)

[EN] Constructors and destructors are not inherited (unless explicitly brought in via C++11 using Base::Base), but derived constructors automatically invoke base constructors. In multilevel inheritance (A -> B -> C), constructors execute top-down from base to most-derived (A, then B, then C), while destructors execute in exact reverse order (C, then B, then A). In multiple inheritance (class C : public A, public B), base constructors execute strictly in the declaration order after the colon (A then B), regardless of their order in C's member initializer list.

[HI] कंस्ट्रक्टर और डिस्ट्रक्टर इनहेरिट नहीं होते (जब तक C++11 में using Base::Base का उपयोग न हो), लेकिन डिराइव्ड कंस्ट्रक्टर स्वतः बेस कंस्ट्रक्टर को कॉल करता है। मल्टीलेवल इनहेरिटेंस (A -> B -> C) में कंस्ट्रक्टर ऊपर से नीचे (A, B, C) चलते हैं, जबकि डिस्ट्रक्टर ठीक विपरीत क्रम (C, B, A) में निष्पादित होते हैं। मल्टीपल इनहेरिटेंस (class C : public A, public B) में बेस कंस्ट्रक्टर मेंबर इनिशियलाइजर लिस्ट के बजाय क्लास डेफिनिशन के क्रम (पहले A फिर B) में चलते हैं।

4. Non-Inherited Members and Name Ambiguity (नॉन-इनहेरिटेड मेंबर्स और नाम अस्पष्टता (Ambiguity))

[EN] Along with constructors and destructors, copy/move assignment operators (operator=) and friend declarations are never inherited by derived classes. When a derived class defines a function with the same name as a base function, name hiding occurs and conceals all base overloads unless revealed with a using Base::func declaration. In multiple inheritance, if both A and B define a method show(), calling c.show() triggers a compile-time ambiguity error, which must be resolved explicitly via scope resolution such as c.A::show().

[HI] कंस्ट्रक्टर और डिस्ट्रक्टर के अलावा असाइनमेंट ऑपरेटर (operator=) और friend रिलेशन भी डिराइव्ड क्लास में इनहेरिट नहीं होते हैं। यदि डिराइव्ड क्लास में बेस फंक्शन जैसा ही नाम हो तो नेम हाइडिंग (name hiding) होती है, जिसे using Base::func से हटाया जा सकता है। मल्टीपल इनहेरिटेंस में यदि A और B दोनों में show() हो, तो c.show() कंपाइल एरर देता है, जिसे c.A::show() जैसे स्कोप रेजोल्यूशन ऑपरेटर से हल किया जाता है।

Syntax, Formula & Technical Example (Inheritance in C++: Single, Multilevel, Multiple, Hierarchical)

#include <iostream>
class Vehicle { public: int wheels = 4; };
class Engine { public: void start() { std::cout << "On\n"; } };
class Car : public Vehicle, public Engine {
public:
    void info() { start(); std::cout << wheels << "\n"; }
};
int main() { Car c; c.info(); return 0; }

Inheritance in C++: Single, Multilevel, Multiple, Hierarchical Quick Reference & Comparison Table

Inheritance TypeClass StructureKey Technical Fact
Single Inheritance1 Base -> 1 DerivedDirect parent-child reuse; minimal overhead
Multilevel InheritanceBase -> Mid -> DerivedConstructors run top-down; destructors bottom-up
Multiple Inheritance2+ Bases -> 1 DerivedRequires :: operator to resolve same-name clashes
Hierarchical Inheritance1 Base -> 2+ DerivedShared base interface across sibling classes

Inheritance in C++: Single, Multilevel, Multiple, Hierarchical Top 20 Bilingual MCQs with Answers

Q1. What is the default inheritance visibility mode when the access specifier is omitted in `class Derived : Base`?

(`class Derived : Base` में एक्सेस स्पेसिफायर न लिखने पर डिफ़ॉल्ट इनहेरिटेंस मोड क्या होता है?)

(A) protected

(B) friend

(C) private

(D) public

Correct Answer: (C)

[EN] In C++, class inheritance defaults to private, whereas struct inheritance defaults to public.

[HI] C++ में class के लिए डिफ़ॉल्ट इनहेरिटेंस private होता है, जबकि struct के लिए यह public होता है।

Q2. Which type of inheritance is modeled by `class C : public A, protected B`?

(`class C : public A, protected B` किस प्रकार के इनहेरिटेंस को दर्शाता है?)

(A) Multiple inheritance

(B) Hierarchical inheritance

(C) Single inheritance

(D) Multilevel inheritance

Correct Answer: (A)

[EN] Inheriting directly from two distinct base classes A and B defines multiple inheritance.

[HI] दो अलग-अलग बेस क्लासेस A और B से सीधे इनहेरिट करना मल्टीपल इनहेरिटेंस कहलाता है।

Q3. Under `protected` inheritance, what access level do `public` members of the base class acquire inside the derived class?

(`protected` इनहेरिटेंस में बेस क्लास के `public` मेंबर्स को डिराइव्ड क्लास में कौन-सा एक्सेस स्तर मिलता है?)

(A) protected

(B) inaccessible

(C) public

(D) private

Correct Answer: (A)

[EN] Protected inheritance converts both public and protected base members into protected members of the derived class.

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

Q4. In multilevel inheritance `A -> B -> C`, in what order are destructors invoked when an object of `C` goes out of scope?

(मल्टीलेवल इनहेरिटेंस `A -> B -> C` में `C` का ऑब्जेक्ट समाप्त होने पर डिस्ट्रक्टर किस क्रम में चलते हैं?)

(A) Only ~C() is invoked

(B) ~A(), ~B(), ~C()

(C) ~B(), ~C(), ~A()

(D) ~C(), ~B(), ~A()

Correct Answer: (D)

[EN] Destructors always execute in reverse order of construction: most-derived first, then up to the root base.

[HI] डिस्ट्रक्टर हमेशा कंस्ट्रक्टर के विपरीत क्रम में चलते हैं: सबसे पहले ~C(), फिर ~B() और अंत में ~A()।

Q5. If class `Base` has only a single private `int x;` member (4 bytes) and `Derived` inherits `Base` publicly with one `int y;` (4 bytes), what is `sizeof(Derived)` on a standard 32/64-bit platform?

(यदि `Base` में केवल एक private `int x;` (4 बाइट्स) है और `Derived` में एक `int y;` (4 बाइट्स) है, तो `sizeof(Derived)` कितना होगा?)

(A) 12 bytes

(B) 4 bytes

(C) 8 bytes

(D) 0 bytes

Correct Answer: (C)

[EN] Private base members are not directly accessible by Derived, but they still occupy memory inside the Derived object.

[HI] बेस के private मेंबर्स सीधे एक्सेस नहीं होते, फिर भी वे डिराइव्ड ऑब्जेक्ट की मेमोरी (8 बाइट्स) में शामिल रहते हैं।

Q6. Which of the following is NOT automatically inherited by a derived class in C++?

(C++ में निम्नलिखित में से कौन डिराइव्ड क्लास में स्वतः इनहेरिट नहीं होता है?)

(A) Friend functions of the base class

(B) Protected member functions

(C) Public non-static data members

(D) Public static member variables

Correct Answer: (A)

[EN] Friendship is neither transitive nor inherited; constructors, destructors, and operator= are also not inherited.

[HI] फ्रेंड फंक्शन, कंस्ट्रक्टर, डिस्ट्रक्टर और operator= डिराइव्ड क्लास में इनहेरिट नहीं होते हैं।

Q7. In `class D : public B, public A { D() : A(), B() {} };`, which base constructor executes first?

(`class D : public B, public A { D() : A(), B() {} };` में सबसे पहले कौन-सा बेस कंस्ट्रक्टर चलेगा?)

(A) D()

(B) B()

(C) Compilation fails due to initializer order

(D) A()

Correct Answer: (B)

[EN] Base constructors always run in the order listed in the class inheritance header (B then A), ignoring the initializer list order.

[HI] बेस कंस्ट्रक्टर हमेशा क्लास डेफिनिशन की इनहेरिटेंस सूची के क्रम (पहले B फिर A) में चलते हैं, इनिशियलाइजर लिस्ट के क्रम में नहीं।

Q8. How do you resolve an ambiguous call `obj.display()` when both base classes `Base1` and `Base2` define `display()`?

(यदि दोनों बेस क्लासेस `Base1` और `Base2` में `display()` फंक्शन है, तो `obj.display()` की अस्पष्टता कैसे दूर की जाती है?)

(A) Use scope resolution: obj.Base1::display()

(B) Use the friend keyword inside Derived

(C) Declare display() as static in Derived

(D) Use pointer arrow: obj->display()

Correct Answer: (A)

[EN] Qualifying the call with the base class name and :: operator disambiguates which inherited version to invoke.

[HI] बेस क्लास के नाम और स्कोप रेजोल्यूशन ऑपरेटर (`obj.Base1::display()`) का उपयोग करके अस्पष्टता दूर की जाती है।

Q9. Which inheritance pattern occurs when classes `Circle`, `Rectangle`, and `Triangle` all inherit from a single base class `Shape`?

(जब `Circle`, `Rectangle` और `Triangle` तीनों एक ही बेस क्लास `Shape` से इनहेरिट करती हैं, तो यह कौन-सा इनहेरिटेंस है?)

(A) Single inheritance

(B) Hierarchical inheritance

(C) Multilevel inheritance

(D) Multiple inheritance

Correct Answer: (B)

[EN] Multiple derived classes branching out from one shared base class form hierarchical inheritance.

[HI] एक ही बेस क्लास से कई डिराइव्ड क्लासेस का बनना हायरार्किकल इनहेरिटेंस कहलाता है।

Q10. What happens if a derived class accesses a `private` member of its base class directly inside a derived member function?

(यदि डिराइव्ड क्लास का मेंबर फंक्शन अपनी बेस क्लास के `private` मेंबर को सीधे एक्सेस करने की कोशिश करे तो क्या होगा?)

(A) Compile-time error

(B) Runtime segmentation fault

(C) The compiler promotes it to protected

(D) It accesses it only under public inheritance

Correct Answer: (A)

[EN] Base private members are strictly hidden from derived classes; compilation fails unless accessed via base public/protected interfaces or friends.

[HI] बेस के private मेंबर्स डिराइव्ड क्लास में सीधे एक्सेस नहीं हो सकते और ऐसा करने पर कंपाइल-टाइम एरर आता है।

Q11. Which C++ keyword prevents a class from being used as a base class for any further inheritance?

(C++11 में कौन-सा कीवर्ड किसी क्लास को आगे इनहेरिट होने से रोकता है?)

(A) final

(B) const

(C) static

(D) sealed

Correct Answer: (A)

[EN] Declaring `class Leaf final {};` makes it a compile-time error to inherit from `Leaf`.

[HI] C++11 में `final` कीवर्ड (जैसे `class Leaf final {}`) क्लास के इनहेरिटेंस को रोकता है।

Q12. In `struct Derived : Base {};`, what is the default inheritance mode?

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

(A) virtual

(B) public

(C) protected

(D) private

Correct Answer: (B)

[EN] Unlike `class`, `struct` defaults to public member access and public base-class inheritance.

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

Q13. How can a derived class unhide overloaded functions of a base class that share the same name as a newly defined derived function?

(डिराइव्ड क्लास में समान नाम का नया फंक्शन बनाने पर छिपे हुए बेस ओवरलोड्स को पुनः उपलब्ध कैसे किया जाता है?)

(A) By inheriting privately from `Base`

(B) By marking `func` as `inline`

(C) By declaring `friend Base::func;`

(D) By writing `using Base::func;` inside the derived class

Correct Answer: (D)

[EN] A `using Base::func;` declaration brings all base class overloads of `func` into the derived class scope.

[HI] डिराइव्ड क्लास में `using Base::func;` लिखकर बेस क्लास के सभी ओवरलोडेड वर्जन को स्कोप में लाया जा सकता है।

Q14. Which relationship is canonically represented by `public` inheritance in object-oriented C++ design?

(ऑब्जेक्ट-ओरिएंटेड C++ डिजाइन में `public` इनहेरिटेंस किस संबंध को दर्शाता है?)

(A) USES-A relationship

(B) IS-A relationship

(C) HAS-A relationship

(D) PART-OF relationship

Correct Answer: (B)

[EN] Public inheritance models an IS-A relationship (e.g., Car IS-A Vehicle), whereas composition models HAS-A.

[HI] public इनहेरिटेंस IS-A संबंध (जैसे Car एक Vehicle है) दर्शाता है, जबकि कंपोजिशन HAS-A दर्शाता है।

Q15. What does `private` inheritance (`class Stack : private Vector`) typically model in C++?

(C++ में `private` इनहेरिटेंस (`class Stack : private Vector`) सामान्यतः क्या दर्शाता है?)

(A) Polymorphic IS-A substitution via base pointers

(B) Implemented-in-terms-of (restricting base interface from external callers)

(C) Sharing a single Vector instance across all Stack objects

(D) Automatic public exposure of Vector methods

Correct Answer: (B)

[EN] Private inheritance hides the base class's public API from external users and prevents implicit derived-to-base pointer conversion.

[HI] private इनहेरिटेंस बेस क्लास के पब्लिक इंटरफ़ेस को बाहरी यूजर से छुपाता है और केवल आंतरिक इम्प्लीमेंटेशन रीयूज के लिए उपयोग होता है।

Q16. Which statement about C++ inheritance is NOT true?

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

(A) Constructors run from base to derived

(B) Multiple inheritance can introduce member name ambiguity

(C) Derived class pointers can only point to base objects, never the reverse

(D) A base class pointer can point to a publicly derived class object

Correct Answer: (C)

[EN] Upcasting allows a base pointer to hold a publicly derived object's address; a derived pointer cannot safely point to a plain base object without downcasting.

[HI] पब्लिक इनहेरिटेंस में बेस पॉइंटर डिराइव्ड ऑब्जेक्ट को पॉइंट कर सकता है (अपकास्टिंग), न कि डिराइव्ड पॉइंटर बिना कास्ट के बेस ऑब्जेक्ट को।

Q17. Within Inheritance in C++: Single, Multilevel, Multiple, Hierarchical, which inheritance type directly maps to: "1 Base -> 1 Derived"?

(Inheritance in C++: Single, Multilevel, Multiple, Hierarchical में कौन सा Inheritance Type सीधे "1 Base -> 1 Derived" से संबंधित है?)

(A) Hierarchical Inheritance

(B) Single Inheritance

(C) Multiple Inheritance

(D) Multilevel Inheritance

Correct Answer: (B)

[EN] In Inheritance in C++: Single, Multilevel, Multiple, Hierarchical, 'Single Inheritance' handles '1 Base -> 1 Derived' (Direct parent-child reuse; minimal overhead).

[HI] Inheritance in C++: Single, Multilevel, Multiple, Hierarchical में 'Single Inheritance' का कार्य/विवरण '1 Base -> 1 Derived' (Direct parent-child reuse; minimal overhead) है।

Q18. Which option accurately describes 'Multilevel Inheritance' in Inheritance in C++: Single, Multilevel, Multiple, Hierarchical?

(Inheritance in C++: Single, Multilevel, Multiple, Hierarchical के संदर्भ में 'Multilevel Inheritance' की सही विशेषता कौन सी है?)

(A) Base -> Mid -> Derived

(B) 1 Base -> 2+ Derived

(C) 1 Base -> 1 Derived

(D) 2+ Bases -> 1 Derived

Correct Answer: (A)

[EN] 'Multilevel Inheritance' provides 'Base -> Mid -> Derived' — key point: Constructors run top-down; destructors bottom-up.

[HI] 'Multilevel Inheritance' का मुख्य विवरण 'Base -> Mid -> Derived' है (Constructors run top-down; destructors bottom-up)।

Q19. In Inheritance in C++: Single, Multilevel, Multiple, Hierarchical, identify the correct parameter for the specification: "2+ Bases -> 1 Derived".

(Inheritance in C++: Single, Multilevel, Multiple, Hierarchical में "2+ Bases -> 1 Derived" विवरण किस विकल्प के लिए सही है?)

(A) Multiple Inheritance

(B) Hierarchical Inheritance

(C) Single Inheritance

(D) Multilevel Inheritance

Correct Answer: (A)

[EN] 'Multiple Inheritance' corresponds to '2+ Bases -> 1 Derived' (Requires :: operator to resolve same-name clashes).

[HI] 'Multiple Inheritance' का संबंध '2+ Bases -> 1 Derived' (Requires :: operator to resolve same-name clashes) से है।

Q20. Considering Inheritance in C++: Single, Multilevel, Multiple, Hierarchical, what key behavior defines 'Hierarchical Inheritance'?

(Inheritance in C++: Single, Multilevel, Multiple, Hierarchical में 'Hierarchical Inheritance' की मुख्य भूमिका या मान क्या है?)

(A) 2+ Bases -> 1 Derived

(B) 1 Base -> 2+ Derived

(C) Base -> Mid -> Derived

(D) 1 Base -> 1 Derived

Correct Answer: (B)

[EN] 'Hierarchical Inheritance' represents '1 Base -> 2+ Derived' (Shared base interface across sibling classes).

[HI] 'Hierarchical Inheritance' का अर्थ/मान '1 Base -> 2+ Derived' है (Shared base interface across sibling classes)।

Inheritance in C++: Single, Multilevel, Multiple, Hierarchical — Exam FAQs

Q1: Can a derived class access protected members of its base class through another base object?

क्या डिराइव्ड क्लास किसी अन्य बेस ऑब्जेक्ट के माध्यम से बेस के protected मेंबर्स को एक्सेस कर सकती है?

[EN] No; a derived class member function can access protected base members only through an object or pointer of its own derived type (or further derived), not via a standalone Base object.

[HI] नहीं; डिराइव्ड क्लास का फंक्शन केवल अपने ही डिराइव्ड टाइप के ऑब्जेक्ट/पॉइंटर के जरिए protected मेंबर्स एक्सेस कर सकता है, सीधे Base ऑब्जेक्ट से नहीं।

Q2: What is the difference between private inheritance and composition (HAS-A)?

private इनहेरिटेंस और कंपोजिशन (HAS-A) में क्या मुख्य अंतर है?

[EN] Composition is preferred for HAS-A relationships, while private inheritance is used only when the derived class needs to override a virtual base method or access protected base members.

[HI] HAS-A संबंध के लिए कंपोजिशन बेहतर होता है; private इनहेरिटेंस तभी चुनते हैं जब बेस के protected मेंबर्स या वर्चुअल फंक्शन ओवरराइड करने हों।

Q3: Can we inherit constructors from a base class in modern C++?

क्या आधुनिक C++ में बेस क्लास के कंस्ट्रक्टर को इनहेरिट किया जा सकता है?

[EN] Yes, since C++11 you can write `using Base::Base;` inside the derived class to inherit all base class constructors automatically.

[HI] हाँ, C++11 से डिराइव्ड क्लास के भीतर `using Base::Base;` लिखकर बेस क्लास के कंस्ट्रक्टर्स को इनहेरिट किया जा सकता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)