Question

1.
2.
3.
4.
Answer (Detailed Solution Below)

Option 2 : Only the base-class destructor runs; derived-class resources leak / केवल बेस-क्लास destructor चलता है; डेरिव्ड-क्लास के संसाधन लीक होते हैं

Advertisement

Detailed Solution

सही उत्तर Only the base-class destructor runs; derived-class resources leak / केवल बेस-क्लास destructor चलता है; डेरिव्ड-क्लास के संसाधन लीक होते हैं है।

Key Points
Helpful?
PrepSnapprepsnap.in • Part -A

Question:

In C++, what happens when a derived-class object is deleted through a base-class pointer and the base class destructor is NOT virtual? / C++ में जब बेस-क्लास पॉइंटर से डेरिव्ड-क्लास ऑब्जेक्ट delete किया जाए और बेस क्लास का destructor virtual न हो, तो क्या होता है?

1.
Both destructors run normally / दोनों destructor सामान्य रूप से चलते हैं
2.
Only the base-class destructor runs; derived-class resources leak / केवल बेस-क्लास destructor चलता है; डेरिव्ड-क्लास के संसाधन लीक होते हैं
3.
Compilation error / कंपाइलेशन त्रुटि
4.
The derived destructor runs first, then the program crashes / डेरिव्ड destructor पहले चलता है, फिर प्रोग्राम क्रैश होता है

Correct Answer: Only the base-class destructor runs; derived-class resources leak / केवल बेस-क्लास destructor चलता है; डेरिव्ड-क्लास के संसाधन लीक होते हैं

Without a virtual destructor, deleting through a base pointer invokes only the base destructor (static binding) — the derived destructor never runs, so resources owned by the derived part leak; the behaviour is actually undefined. The trap is assuming polymorphism applies to destruction automatically. / virtual destructor के बिना बेस पॉइंटर से delete करने पर केवल बेस destructor चलता है (static binding) — डेरिव्ड destructor कभी नहीं चलता, अतः उसके संसाधन लीक होते हैं; व्यवहार वस्तुतः undefined है। जाल यह मानना है कि destruction में polymorphism स्वतः लागू होता है।

Downloaded from PrepSnap (prepsnap.in)