Question

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

Option 2 : Compilation error due to ambiguity

Advertisement

Detailed Solution

सही उत्तर Compilation error due to ambiguity है।

Key Points
Helpful?
PrepSnapprepsnap.in • Part -A

Question:

What is the result of compiling and running the following C++ program? class X { public: int v = 1; }; class Y : public X {}; class Z : public X {}; class W : public Y, public Z {}; int main() { W w; cout << w.v; } / निम्नलिखित C++ प्रोग्राम को कंपाइल और रन करने का परिणाम क्या है?

1.
1
2.
Compilation error due to ambiguity
3.
2
4.
Runtime error

Correct Answer: Compilation error due to ambiguity

W inherits two separate copies of X (the classic diamond problem), so w.v is ambiguous and the program fails to compile. It can be fixed with virtual inheritance. / W को X की दो अलग-अलग प्रतियाँ विरासत में मिलती हैं (प्रसिद्ध diamond समस्या), इसलिए w.v अस्पष्ट है और प्रोग्राम कंपाइल नहीं होता। virtual inheritance से इसे ठीक किया जा सकता है।

Downloaded from PrepSnap (prepsnap.in)