Zingo
Zingo

Reputation: 650

polymorphism in C++ and my exam

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?

Upvotes: 7

Views: 468

Answers (2)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

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.

Upvotes: 6

Jean-Baptiste Yunès
Jean-Baptiste Yunès

Reputation: 36391

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

  • Overloading (ad-hoc polymorphism). Use a single identifier to denote (in general) different functions
  • Subtyping (inclusion polymorphism). The preferred form of polymorphism in OOP. The ability of a single function to work with different types of objects, but for which their types are all related by subtyping.
  • Genericity (parametric polymorphism). The ability of some code to be written in a very abstract way so that many different values of different types can be used, either with unrelated types.

In OOP overriding is the ability to redefine in a subtype a function that has been previously defined in a super type.

In C++, virtual function member is the qualification of a method to let the subtype polymorphism operates on 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
  a.f(); // 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
...

Upvotes: 3

Related Questions