Mastering C++: A Comprehensive Quiz Based on 'Thinking in C++'

Disable ads (and more) with a membership for a one time $2.99 payment

Test your C++ skills with our quiz based on Bruce Eckel's 'Thinking in C++'. Dive into object-oriented programming, advanced topics, and fundamentals. Perfect for learners and experts alike. Assess your knowledge and become a C++ master!

Practice this question and more.


What is the output when 'tune()' is called with a 'Wind' object after making 'play()' virtual in the 'Instrument' base class?

  1. "Instrument::play"

  2. "Wind::play"

  3. "Percussion::play"

  4. "None of the above"

The correct answer is: "Wind::play"

Calling 'tune()' with a 'Wind' object after making 'play()' virtual in the 'Instrument' base class will result in the output being "Wind:play". This is because when a function is declared as virtual in a base class, it allows the derived class to override the function and provide its own implementation. So in this scenario, the derived class 'Wind' will have its own implementation of the 'play()' function, which will be called when 'tune()' is invoked. Option A, "Instrument::play", is incorrect because 'Wind' is the derived class and has its own implementation of 'play()', so the base class implementation would not be called. Option C, "Percussion::play", and option D, "None of the above", can be eliminated because they are irrelevant to the scenario and do not involve the 'Wind' and 'Instrument' classes