Virtual Functions, Pure Virtual Functions and Abstract Classes in C++ Notes in Hindi & English : रन-टाइम पॉलीमॉर्फिज्म: वर्चुअल फंक्शन्स, vtable और एब्स्ट्रैक्ट क्लास और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

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

Overview: Runtime polymorphism (dynamic binding) in C++ dispatches overridden virtual function calls based on the dynamic type of the object pointed to by a base pointer or reference via vtable and vptr mechanisms. Pure virtual functions (`= 0`) define abstract classes that cannot be instantiated directly, while virtual destructors prevent resource leaks during polymorphic deletion.

अध्याय सार: C++ में रन-टाइम पॉलीमॉर्फिज्म (डायनामिक बाइंडिंग) बेस क्लास पॉइंटर या रेफरेंस के जरिए `vtable` और `vptr` मैकेनिज्म का उपयोग करके रन-टाइम पर सही ओवरराइड फंक्शन को कॉल करता है। प्योर वर्चुअल फंक्शन्स (`= 0`) एब्स्ट्रैक्ट क्लास बनाते हैं जिन्हें सीधे इंस्टैंशिएट नहीं किया जा सकता, तथा वर्चुअल डिस्ट्रक्टर मेमोरी लीक रोकते हैं।

Virtual Functions, Pure Virtual Functions and Abstract Classes in C++: Detailed Notes & Architecture (रन-टाइम पॉलीमॉर्फिज्म: वर्चुअल फंक्शन्स, vtable और एब्स्ट्रैक्ट क्लास)

1. Virtual Functions and Late Binding (वर्चुअल फंक्शन्स और लेट बाइंडिंग (Late Binding))

[EN] Marking a base member function with the `virtual` keyword instructs the compiler to defer call resolution until runtime whenever invoked through a base pointer (`Base*`) or reference (`Base&`). If a derived class provides a function with the exact same name, parameter list, and cv-qualifiers (and covariant return type if applicable), it overrides the base implementation. Without `virtual`, `Base* ptr = new Derived(); ptr->show();` statically binds to `Base::show()` based solely on the pointer's static type.

[HI] बेस क्लास के फंक्शन को `virtual` घोषित करने पर `Base*` पॉइंटर या `Base&` रेफरेंस से की गई कॉल का निर्णय कंपाइल समय के बजाय रन-टाइम पर ऑब्जेक्ट के वास्तविक प्रकार से होता है। यदि डिराइव्ड क्लास समान सिग्नेचर वाला फंक्शन लिखती है तो वह बेस फंक्शन को ओवरराइड करती है। `virtual` कीवर्ड न होने पर `Base* ptr = new Derived(); ptr->show();` स्टैटिक बाइंडिंग के कारण पॉइंटर टाइप `Base::show()` को ही कॉल करता है।

2. How vtable and vptr Work Under the Hood (आंतरिक कार्यप्रणाली: vtable और vptr)

[EN] For every class that declares or inherits at least one virtual function, the compiler generates one static read-only lookup table of function pointers called the **vtable** (Virtual Method Table). Every object instance of such a polymorphic class contains a hidden pointer called **vptr**, automatically initialized by constructors to point to that class's vtable, increasing object size by `sizeof(void*)` (8 bytes on 64-bit). A virtual call translates into dereferencing `obj->vptr[slot_index]` at runtime, incurring one pointer lookup overhead and blocking ordinary inlining.

[HI] जिस क्लास में कम से कम एक वर्चुअल फंक्शन होता है, उसके लिए कंपाइलर फंक्शन पॉइंटर्स की एक स्टैटिक लुकअप टेबल बनाता है जिसे **vtable** कहते हैं। ऐसे हर ऑब्जेक्ट के अंदर एक छिपा हुआ **vptr** पॉइंटर जुड़ जाता है (जो 64-बिट सिस्टम पर ऑब्जेक्ट साइज में 8 बाइट्स बढ़ाता है) और कंस्ट्रक्टर द्वारा सही vtable पर सेट होता है। रन-टाइम पर वर्चुअल कॉल `obj->vptr[index]` के माध्यम से वास्तविक फंक्शन तक पहुँचती है।

3. Pure Virtual Functions and Abstract Classes (प्योर वर्चुअल फंक्शन्स और एब्स्ट्रैक्ट क्लास)

[EN] A pure virtual function uses the `= 0` pure-specifier (`virtual double area() const = 0;`) and turns its enclosing class into an **Abstract Class**. An abstract class cannot be instantiated directly (`Shape s;` causes a compile error), yet you can declare `Shape*` pointers and `Shape&` references for polymorphic interfaces. Any concrete derived class must override all inherited pure virtual functions; otherwise it remains abstract. Notably, C++ allows providing a body for a pure virtual function outside the class.

[HI] जब किसी वर्चुअल फंक्शन के अंत में `= 0` (`virtual double area() const = 0;`) लिखा जाता है तो वह प्योर वर्चुअल फंक्शन कहलाता है और वह क्लास **एब्स्ट्रैक्ट क्लास** बन जाती है। एब्स्ट्रैक्ट क्लास का सीधे ऑब्जेक्ट नहीं बनाया जा सकता, लेकिन पॉलीमॉर्फिज्म के लिए इसके पॉइंटर या रेफरेंस (`Shape*`, `Shape&`) बनाए जा सकते हैं। यदि डिराइव्ड क्लास सभी प्योर वर्चुअल फंक्शन्स को ओवरराइड नहीं करती तो वह भी एब्स्ट्रैक्ट रहती है; प्योर वर्चुअल फंक्शन की बॉडी क्लास के बाहर परिभाषित की जा सकती है।

4. Virtual Destructors, override, and final (वर्चुअल डिस्ट्रक्टर, override और final स्पेसिफायर्स)

[EN] Whenever a base class is designed for polymorphic deletion (`Base* p = new Derived(); delete p;`), the base destructor MUST be `virtual`; otherwise deleting `p` invokes only `~Base()`, skipping `~Derived()` and causing undefined behavior and memory leaks. Constructors, `static` functions, and `friend` functions cannot be virtual (since constructors initialize `vptr` and static/friend functions lack `this`). C++11 introduced `override` to catch signature typos at compile time and `final` to forbid further overriding of a virtual method.

[HI] जब किसी डिराइव्ड ऑब्जेक्ट को बेस पॉइंटर से `delete` किया जाता है (`Base* p = new Derived(); delete p;`), तो बेस डिस्ट्रक्टर का `virtual` होना अनिवार्य है, वरना `~Derived()` नहीं चलेगा और मेमोरी लीक होगी। कंस्ट्रक्टर, `static` और `friend` फंक्शन कभी वर्चुअल नहीं हो सकते। C++11 का `override` कीवर्ड सिग्नेचर की गलतियों को कंपाइल टाइम पर पकड़ता है और `final` कीवर्ड किसी वर्चुअल फंक्शन को आगे ओवरराइड होने से रोकता है।

Syntax, Formula & Technical Example (Virtual Functions, Pure Virtual Functions and Abstract Classes in C++)

#include <iostream>
class Shape {
public:
    virtual void draw() const = 0;
    virtual ~Shape() = default;
};
class Circle : public Shape {
public:
    void draw() const override { std::cout << "Circle\n"; }
};

Virtual Functions, Pure Virtual Functions and Abstract Classes in C++ Quick Reference & Comparison Table

Construct / ConceptSyntax / StructureKey Runtime & Compile Rule
Virtual Functionvirtual void f();Enables dynamic dispatch via vptr/vtable
Pure Virtual Functionvirtual void f() = 0;Makes class abstract; blocks instantiation
Virtual Destructorvirtual ~Base();Ensures ~Derived() executes on delete basePtr
C++11 override Specifiervoid f() override;Verifies base virtual signature at compile time

Virtual Functions, Pure Virtual Functions and Abstract Classes in C++ Top 20 Bilingual MCQs with Answers

Q1. Where is the `vtable` (Virtual Method Table) stored, and how many `vtable` instances exist per polymorphic class?

(`vtable` कहाँ स्टोर होती है और प्रत्येक पॉलीमॉर्फिक क्लास के लिए इसकी कितनी कॉपियाँ बनती हैं?)

(A) Inside CPU general-purpose registers

(B) On the runtime heap allocated by `new`

(C) One new vtable inside every object instance

(D) One shared static table per polymorphic class (not per object)

Correct Answer: (D)

[EN] Each polymorphic class has one shared vtable in static/read-only memory, while every object holds a `vptr` pointing to it.

[HI] प्रत्येक पॉलीमॉर्फिक क्लास के लिए एक साझा स्टैटिक `vtable` बनती है, जबकि हर ऑब्जेक्ट में उस तक जाने वाला एक `vptr` होता है।

Q2. Suppose `class Test { int a; virtual void f(); };` is compiled on a 64-bit platform (where `int` is 4 bytes and pointers are 8 bytes). What is `sizeof(Test)`?

(मान लीजिए `class Test { int a; virtual void f(); };` को 64-बिट सिस्टम (`int` = 4 बाइट्स, पॉइंटर = 8 बाइट्स) पर कंपाइल किया जाए, तो `sizeof(Test)` कितना होगा?)

(A) 4 bytes

(B) 8 bytes

(C) 16 bytes

(D) 12 bytes

Correct Answer: (C)

[EN] The hidden 8-byte `vptr` plus 4-byte `int a` (12 bytes) aligns to an 8-byte boundary, resulting in 16 bytes.

[HI] 8-बाइट के हिडन `vptr` और 4-बाइट `int` के साथ 8-बाइट एलाइनमेंट पैडिंग जुड़कर कुल आकार 16 बाइट्स हो जाता है।

Q3. Which syntax declares a pure virtual function in C++?

(C++ में प्योर वर्चुअल फंक्शन घोषित करने का सही सिंटैक्स कौन-सा है?)

(A) `abstract void render();`

(B) `pure virtual void render();`

(C) `virtual void render() = NULL;`

(D) `virtual void render() = 0;`

Correct Answer: (D)

[EN] A pure virtual function is declared by appending `= 0` (the pure-specifier) to a virtual member function declaration.

[HI] फंक्शन घोषणा के अंत में `= 0` लगाने से वह प्योर वर्चुअल फंक्शन बनता है: `virtual void render() = 0;`।

Q4. Which of the following statements about an Abstract Class in C++ is TRUE?

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

(A) An abstract class cannot have constructors or data members

(B) You can declare pointers and references to an abstract class, but cannot instantiate objects of it

(C) You cannot declare pointers to an abstract class

(D) An abstract class must use the `abstract` keyword

Correct Answer: (B)

[EN] Abstract classes cannot be instantiated directly (`Abstract a;`), but `Abstract*` and `Abstract&` are essential for polymorphism, and abstract classes can have constructors and data members.

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

Q5. What happens if you delete a `Derived` object through a `Base*` pointer when `~Base()` is NOT virtual?

(यदि `~Base()` वर्चुअल न हो और `Base* p = new Derived(); delete p;` किया जाए, तो क्या होता है?)

(A) Undefined behavior: `~Derived()` is skipped, causing potential memory/resource leaks

(B) A compile-time error occurs on `delete p;`

(C) `~Base()` automatically calls `~Derived()`

(D) Both `~Derived()` and `~Base()` run normally

Correct Answer: (A)

[EN] Without a virtual destructor in Base, `delete p` performs static binding and executes only `~Base()`, leaking Derived resources.

[HI] `~Base()` वर्चुअल न होने पर केवल बेस का डिस्ट्रक्टर चलता है और `~Derived()` छूट जाता है, जिससे अनडिफाइंड बिहेवियर व मेमोरी लीक होता है।

Q6. Which C++ member CANNOT be declared `virtual`?

(C++ में निम्नलिखित में से किसे `virtual` घोषित नहीं किया जा सकता है?)

(A) Const member functions

(B) Constructors (and `static` member functions)

(C) Overloaded operators like `operator+`

(D) Destructors

Correct Answer: (B)

[EN] Constructors initialize the `vptr` before the object exists, and `static` functions have no `this` pointer; neither can be virtual.

[HI] कंस्ट्रक्टर (क्योंकि वह स्वयं `vptr` सेट करता है) और `static` मेंबर फंक्शन (जिनमें `this` नहीं होता) कभी `virtual` नहीं हो सकते।

Q7. What occurs if a class `Sub` inherits from an abstract class `Base` and overrides only 1 of `Base`'s 2 pure virtual functions?

(यदि `Sub` क्लास एब्स्ट्रैक्ट क्लास `Base` के 2 प्योर वर्चुअल फंक्शन्स में से केवल 1 को ओवरराइड करती है, तो क्या होगा?)

(A) `Sub` automatically remains an abstract class and cannot be instantiated

(B) The compiler generates a default empty body for the second function

(C) `Sub` becomes concrete but throws at runtime

(D) Compilation fails immediately inside `Sub`'s declaration

Correct Answer: (A)

[EN] A derived class must implement every inherited pure virtual function to become concrete; otherwise it stays abstract too.

[HI] सभी प्योर वर्चुअल फंक्शन्स को ओवरराइड न करने पर डिराइव्ड क्लास `Sub` स्वयं भी एब्स्ट्रैक्ट बनी रहती है।

Q8. If `Base` defines `virtual void f() { cout << "B"; }` and `Derived` defines `void f() { cout << "D"; }`, what does `Base b = Derived(); b.f();` print?

(यदि `Base` में `virtual void f()` (`"B"`) और `Derived` में `void f()` (`"D"`) हो, तो `Base b = Derived(); b.f();` क्या प्रिंट करेगा?)

(A) B (due to object slicing)

(B) D (via dynamic dispatch)

(C) BD

(D) Compile-time error

Correct Answer: (A)

[EN] Assigning `Derived()` by value to `Base b` slices off the Derived part, leaving a plain `Base` object whose `vptr` points to `Base`'s vtable.

[HI] ऑब्जेक्ट को वैल्यू द्वारा `Base b` में कॉपी करने पर **ऑब्जेक्ट स्लाइसिंग** होती है और `b` का `vptr` `Base` की vtable पर सेट रहता है, इसलिए `B` प्रिंट होता है।

Q9. What happens when a virtual function is called directly inside a base class constructor (`Base::Base() { init(); }`)?

(बेस क्लास के कंस्ट्रक्टर के अंदर से किसी वर्चुअल फंक्शन को कॉल करने पर कौन-सा वर्जन निष्पादित होता है?)

(A) A pure virtual runtime crash always happens

(B) The most-derived class override runs

(C) The compiler forbids calling virtual functions in constructors

(D) `Base`'s own version runs, NOT the derived override

Correct Answer: (D)

[EN] While `Base()` is executing, the derived part is not yet constructed, so `vptr` temporarily points to `Base`'s vtable.

[HI] बेस कंस्ट्रक्टर चलते समय डिराइव्ड ऑब्जेक्ट अभी बना नहीं होता, इसलिए `vptr` बेस क्लास की vtable की ओर होता है और `Base` का ही वर्जन चलता है।

Q10. Can a pure virtual function (`virtual void log() = 0;`) have a function definition/body in C++?

(क्या C++ में प्योर वर्चुअल फंक्शन (`virtual void log() = 0;`) की अपनी फंक्शन बॉडी (परिभाषा) हो सकती है?)

(A) Only in C++20 modules

(B) Yes, defined outside the class (`void Base::log() { ... }`) and called explicitly via `Base::log()`

(C) Yes, only inline inside the class braces

(D) No, `= 0` forbids writing a body anywhere

Correct Answer: (B)

[EN] C++ permits giving a pure virtual function an out-of-line implementation so derived classes can reuse common base logic via `Base::log()`.

[HI] हाँ, क्लास के बाहर `void Base::log() { ... }` लिखकर बॉडी दी जा सकती है और डिराइव्ड क्लास उसे `Base::log()` से कॉल कर सकती है।

Q11. What is the primary benefit of appending the C++11 `override` specifier to a derived method?

(डिराइव्ड क्लास के मेथड में C++11 का `override` स्पेसिफायर लगाने का मुख्य लाभ क्या है?)

(A) It prevents further subclasses from overriding the method

(B) It automatically marks the base method as `virtual`

(C) It causes a compile error if the signature does not actually override a base virtual function

(D) It removes the `vtable` entry for faster execution

Correct Answer: (C)

[EN] `override` prevents silent bugs where a mismatched parameter type or missing `const` accidentally creates a new overloaded function instead of overriding.

[HI] `override` पैरामीटर टाइप या `const` की गलती होने पर कंपाइलर से एरर दिलवाता है ताकि गलती से नया फंक्शन न बन जाए।

Q12. What does marking a virtual function `final` (`void draw() override final;`) enforce?

(किसी वर्चुअल फंक्शन के साथ `final` (`void draw() override final;`) लगाने से क्या नियम लागू होता है?)

(A) `draw()` becomes `static`

(B) `draw()` cannot be called via a base pointer

(C) `draw()` becomes a pure virtual function

(D) No further derived class can override `draw()`

Correct Answer: (D)

[EN] `final` on a virtual member function prevents any subclass further down the hierarchy from overriding that method.

[HI] `final` लगाने के बाद कोई भी आगे की सब-क्लास उस वर्चुअल फंक्शन को दोबारा ओवरराइड नहीं कर सकती।

Q13. In C++, what is the nearest equivalent to a Java/C# `interface`?

(C++ में Java या C# के `interface` के समकक्ष क्या माना जाता है?)

(A) A `union` of pointers

(B) A class with only pure virtual functions, a virtual destructor, and no data members

(C) A `struct` with only `static` variables

(D) A `namespace` containing `friend` functions

Correct Answer: (B)

[EN] C++ has no `interface` keyword; instead, an abstract class consisting solely of pure virtual methods and a virtual destructor acts as an interface.

[HI] C++ में `interface` कीवर्ड नहीं है; केवल प्योर वर्चुअल फंक्शन्स और वर्चुअल डिस्ट्रक्टर वाली बिना डेटा मेंबर की एब्स्ट्रैक्ट क्लास ही इंटरफ़ेस कहलाती है।

Q14. What happens if a constructor calls a pure virtual function that has no implementation, directly or indirectly?

(यदि कंस्ट्रक्टर से कोई ऐसा प्योर वर्चुअल फंक्शन कॉल हो जाए जिसकी कोई बॉडी नहीं है, तो क्या होता है?)

(A) The derived class override safely handles it

(B) Undefined behavior (typically abnormal termination: 'pure virtual method called')

(C) It converts into a `friend` call

(D) The compiler silently skips the call

Correct Answer: (B)

[EN] During base construction the derived override is inactive, so calling an unimplemented pure virtual function triggers undefined behavior / runtime abort.

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

Q15. Which casting operator safely checks `vtable` RTTI at runtime when downcasting `Base*` to `Derived*`?

(`Base*` से `Derived*` में सुरक्षित डाउनकास्टिंग करते समय रन-टाइम पर `vtable` RTTI की जाँच कौन-सा ऑपरेटर करता है?)

(A) `dynamic_cast`

(B) `const_cast`

(C) C-style cast `(Derived*)`

(D) `reinterpret_cast`

Correct Answer: (A)

[EN] `dynamic_cast<Derived*>(basePtr)` uses Run-Time Type Information (RTTI) stored alongside the vtable and returns `nullptr` if the cast is invalid.

[HI] `dynamic_cast` रन-टाइम पर RTTI (जो vtable से जुड़ा होता है) की जाँच करता है और असफल होने पर पॉइंटर के लिए `nullptr` लौटाता है।

Q16. Which statement about virtual functions in C++ is NOT true?

(C++ में वर्चुअल फंक्शन्स के बारे में निम्नलिखित में से कौन-सा कथन सत्य नहीं है?)

(A) A pure virtual destructor still requires an implementation body

(B) Virtual functions enable runtime polymorphism via base pointers or references

(C) Virtual functions can be `static` so they dispatch without an object instance

(D) Each polymorphic object carries a hidden `vptr` pointer

Correct Answer: (C)

[EN] Static member functions belong to the class rather than any object (`this` does not exist), so they can never be virtual.

[HI] स्टैटिक मेंबर फंक्शन में `this` पॉइंटर नहीं होता और न ही `vptr` से जुड़ता है, इसलिए स्टैटिक फंक्शन कभी वर्चुअल नहीं हो सकता।

Q17. Within Virtual Functions, Pure Virtual Functions and Abstract Classes in C++, which construct / concept directly maps to: "virtual void f();"?

(Virtual Functions, Pure Virtual Functions and Abstract Classes in C++ में कौन सा Construct / Concept सीधे "virtual void f();" से संबंधित है?)

(A) Virtual Destructor

(B) Virtual Function

(C) C++11 override Specifier

(D) Pure Virtual Function

Correct Answer: (B)

[EN] In Virtual Functions, Pure Virtual Functions and Abstract Classes in C++, 'Virtual Function' handles 'virtual void f();' (Enables dynamic dispatch via vptr/vtable).

[HI] Virtual Functions, Pure Virtual Functions and Abstract Classes in C++ में 'Virtual Function' का कार्य/विवरण 'virtual void f();' (Enables dynamic dispatch via vptr/vtable) है।

Q18. Which option accurately describes 'Pure Virtual Function' in Virtual Functions, Pure Virtual Functions and Abstract Classes in C++?

(Virtual Functions, Pure Virtual Functions and Abstract Classes in C++ के संदर्भ में 'Pure Virtual Function' की सही विशेषता कौन सी है?)

(A) virtual ~Base();

(B) virtual void f() = 0;

(C) virtual void f();

(D) void f() override;

Correct Answer: (B)

[EN] 'Pure Virtual Function' provides 'virtual void f() = 0;' — key point: Makes class abstract; blocks instantiation.

[HI] 'Pure Virtual Function' का मुख्य विवरण 'virtual void f() = 0;' है (Makes class abstract; blocks instantiation)।

Q19. In Virtual Functions, Pure Virtual Functions and Abstract Classes in C++, identify the correct parameter for the specification: "virtual ~Base();".

(Virtual Functions, Pure Virtual Functions and Abstract Classes in C++ में "virtual ~Base();" विवरण किस विकल्प के लिए सही है?)

(A) Pure Virtual Function

(B) C++11 override Specifier

(C) Virtual Function

(D) Virtual Destructor

Correct Answer: (D)

[EN] 'Virtual Destructor' corresponds to 'virtual ~Base();' (Ensures ~Derived() executes on delete basePtr).

[HI] 'Virtual Destructor' का संबंध 'virtual ~Base();' (Ensures ~Derived() executes on delete basePtr) से है।

Q20. Considering Virtual Functions, Pure Virtual Functions and Abstract Classes in C++, what key behavior defines 'C++11 override Specifier'?

(Virtual Functions, Pure Virtual Functions and Abstract Classes in C++ में 'C++11 override Specifier' की मुख्य भूमिका या मान क्या है?)

(A) virtual void f() = 0;

(B) virtual ~Base();

(C) virtual void f();

(D) void f() override;

Correct Answer: (D)

[EN] 'C++11 override Specifier' represents 'void f() override;' (Verifies base virtual signature at compile time).

[HI] 'C++11 override Specifier' का अर्थ/मान 'void f() override;' है (Verifies base virtual signature at compile time)।

Virtual Functions, Pure Virtual Functions and Abstract Classes in C++ — Exam FAQs

Q1: Why must a pure virtual destructor still have a body (`Shape::~Shape() {}`)?

प्योर वर्चुअल डिस्ट्रक्टर (`virtual ~Shape() = 0;`) की बॉडी लिखना क्यों अनिवार्य होता है?

[EN] Because whenever a derived object is destroyed, the compiler automatically invokes the base class destructor at the end of destruction; without a body, the linker fails with an undefined reference.

[HI] क्योंकि डिराइव्ड ऑब्जेक्ट नष्ट होते समय अंत में बेस डिस्ट्रक्टर का कॉल होना अनिवार्य है; बॉडी न होने पर लिंकर `undefined reference` एरर दे देता है।

Q2: Can virtual functions accept default arguments, and how do they behave?

क्या वर्चुअल फंक्शन्स में डिफ़ॉल्ट आर्गुमेंट्स दिए जा सकते हैं और वे कैसे काम करते हैं?

[EN] Yes, but default arguments are statically bound at compile time from the pointer/reference type (`Base*`), even though the function body is dynamically bound from the object (`Derived`).

[HI] हाँ, लेकिन डिफ़ॉल्ट आर्गुमेंट्स कंपाइल समय पर पॉइंटर के स्टैटिक टाइप (`Base*`) से लिए जाते हैं, जबकि फंक्शन बॉडी रन-टाइम पर ऑब्जेक्ट के टाइप से चलती है।

Q3: Does an empty class with one virtual function still have `sizeof` > 1 byte?

क्या एक वर्चुअल फंक्शन वाली बिना डेटा मेंबर की खाली क्लास का `sizeof` 1 बाइट से अधिक होता है?

[EN] Yes; instead of the 1-byte empty-class size, it holds a hidden `vptr`, making `sizeof` typically 4 bytes on 32-bit and 8 bytes on 64-bit architectures.

[HI] हाँ; सामान्य खाली क्लास का साइज 1 बाइट होता है, लेकिन वर्चुअल फंक्शन जोड़ते ही उसमें हिडन `vptr` जुड़ जाता है जिससे साइज 4 या 8 बाइट्स हो जाता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)