1

I know how virtual function is used to achieve RT polymorphism .By using base class reference and storing derived class object in it.And then calling the overridden method using this reference. But Is this also true?

class Base
{
 public:
 void show();
 {
  cout << "Base class\t";
 }
};
class Derived:public Base
{
 public:
 void show()
 {
  cout << "Derived Class";
 }
}

int main()
{
 Base b;       //Base class object
 Derived d;     //Derived class object
 d.show();   // is this run time polymorphism??
}

//Output : Derived class

  • It is not (cannot be) run-time polymorphism because the exact method is determined during compilation based only on the type information. There is no run-time dispatch involved. – user2864740 Sep 22 '15 at 19:41
  • so this is not even method overriding? I got the code from http://www.studytonight.com/cpp/function-overriding.php please tell if this example for overriding is wrongly put in the site – Saurabh Kumar Sep 22 '15 at 19:59
  • This is known as [Function Hiding](http://stackoverflow.com/questions/19736281/what-are-the-differences-between-overriding-virtual-functions-and-hiding-non-vir). – user2864740 Sep 22 '15 at 20:05
  • @user2864740 thanks a lot. This cleared my doubt. – Saurabh Kumar Sep 22 '15 at 20:14

1 Answers1

2

No, it is not. Because this

 Derived d;
 Base *b = &d;
 b->show();

prints

Base class

Whereas with run-time polymorphism it would print

Derived Class

There is no polymorphism in your example because the exact type of the object is known at the calling site. Also you hide the base function, not override nor overload it.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • because for that you have to declare show method in base as virtual.What I am asking here is using derived class obj to call overridden method as RT polymorphism – Saurabh Kumar Sep 22 '15 at 19:39
  • The code has polymorphism.i think.It is a simple example of function overridding. And function overriding is considered as Polymorphism right – Saurabh Kumar Sep 22 '15 at 19:43
  • @SaurabhKumar: no, it is not overriding. – Yakov Galka Sep 22 '15 at 19:44
  • @SaurabhKumar: polymorphism is the scenario when the code can do different things depending on the exact type of the object which is not known at that point in code. Static polymorphism is when the type is determined at the time of template instantiation, dynamic is when it is determined at runtime through the vtable. It has nothing to do with overriding. – Yakov Galka Sep 22 '15 at 19:47
  • May be you both are right.I read the code from this site: http://www.studytonight.com/cpp/function-overriding.php Please tell me if the information in the site is wrong – Saurabh Kumar Sep 22 '15 at 19:49
  • @SaurabhKumar a bit late sorry. It's nice that your article starts with early and late binding + inheritance before presenting the virtual keyword, but at the same time, it's totally confusing, that they use the word "override" for the non-virtual case (early binding) too. Yeah, one might start deep philosophical discussions whether this terminus might be applied here too but for various reasons, I'd say no. It's hiding and not overriding. There's a reason for having these both (orthogonal) qualities and they shouldn't be mixed. – Secundi Dec 10 '21 at 15:44