Polymorphism in C ++ and my exam

I had a C ++ exam a few days ago, and I got this question, and I found this incomprehensible to me, the question was:

Explain the difference between the terms Polymorphism, virtual function, and most important. Using the example of obtaining the shape region for a rectangle and a triangle, write two different fragment codes to show the implementation of polymorphism and the implementation of polymorphism and a virtual function.
Give an example of output from both code fragments.

while the definition of polymorphism is in accordance with Absolute C ++ 5th p669:

Polymorphism refers to the ability to bind many values ​​to a single function name using a late-binding mechanism. Thus, polymorphism, late binding, and virtual functions are all the same topics.

I understood from this definition that polymorphism does not exist without using virtual functions, right? so there are no two different fingerprint options for this question, right? only one using virtual function

My question is: is the question really asked?

+6
source share
2 answers

My question is: is the question really asked?

No, this does not apply to the alleged answer, "it seems that only virtual functions provide polymorphism in C ++." The question is too narrow and misleading.

Polymorphism refers to the ability to bind many values ​​to a single function name using a late-binding mechanism. Thus, polymorphism, late binding, and virtual functions are all the same topics .

I understood from this definition that there is no polymorphism without using virtual functions, right?

In fact, you can have polymorphism without virtual functions.

He called static polymorphism ; view the CRTP and SFINAE pattern .

Well, highlighting late binding actually narrows the question of dynamic polymorphisms and (pure) virtual functions. But IMHO this is still a bad exam and a too narrow / unclear question.

+6
source

Polymorphism is a very abstract term that refers to 3 things in CS / CP:

  • Overload (ad-hoc polymorphism). Use one identifier to denote (generally) different functions
  • Subtyping (inclusion polymorphism). The preferred form of polymorphism in OOP. The ability of one function to work with various types of objects, but for which all their types are associated with subtyping.
  • Genericity (parametric polymorphism). The ability of some code to write is very abstract, so that you can use many different values ​​of different types or with unrelated types.

In OOP, redefinition is the ability to redefine a function that was previously defined in a supertype in a subtype.

In C ++, the virtual function member is the qualification of a method that allows a subtype polymorphism to work with all subtypes of this type.

 class A { // A is a polymorphic type (aka contains virtual function) public: virtual void f() { cout << "A::f()" << endl; } // virtual for polymorphism }; class B : public A { // public is necessary to obtain subtyping public: virtual void f() { cout << "B::f()" << endl; } // overriding }; ... void func(A &a) { // polymorphism can be obtain either on pointers or references types af(); // polymorphic call, no one can say what will happens here, at least no more than "some function f will be called" } ... A a; B b; func(a); // guess what will be printed func(b); // guess what will be printed ... 
+2
source

All Articles