I had C++ exam few days back and I got this question and I found it not clear for me, the question was:
Explain the difference among the terms Polymorphism, virtual function and overriding. By using example getting the area of shape for rectangle and triangle, write two different snippet codes to show the implementation of polymorphism and implementation of polymorphism and virtual function.
Give example of output from both snippet codes.
while the definition of polymorphism according to Absolute C++ 5th p669 is:
Polymorphism refers to the ability to associate many meanings to one function name by means of the late-binding mechanism. Thus, polymorphism, late binding, and virtual functions are really all the same topic.
I could understand from this definition that there is no polymorphism without using virtual functions, isn't it? so there are not two different snippet implementation for this question, right ? only one by using virtual function
My question is: Is this a valid question to be asked?
My question is: Is this a valid question to be asked?
No, it isn't regarding the intended answer "only virtual functions provide polymorphism in c++" as it seems. The question is too narrow, and misleading.
Polymorphism refers to the ability to associate many meanings to one function name by means of the late-binding mechanism. Thus, polymorphism, late binding, and virtual functions are really all the same topic.
I could understand from this definition that there is no polymorphism without using virtual functions, isn't it?
Actually you can have polymorphism without virtual
functions.
It's called static polymorphism, lookup the CRTP pattern and SFINAE.
Well, emphasis of late binding actually narrows the question for dynamic polymorphism and (pure) virtual
functions. But IMHO it's still a bad exam and too narrow/unclear question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With