Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

polymorphism in C++ and my exam

Tags:

c++

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?

like image 827
Zingo Avatar asked Jan 29 '16 19:01

Zingo


1 Answers

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.

like image 146
πάντα ῥεῖ Avatar answered Sep 21 '22 14:09

πάντα ῥεῖ