-
Polymorphism
Hi guys
I just want to clear an issue out regarding polymorphism. Lets say i have a method Display in base class and in derived class, both as virtual.. how is it possible to call the Display from base class ? Would that just be like base:: Display() ? (dont mind the space .. thats simply because ::D shows a smiley hehe)
anyway here is a small implementation just to give a better picture of what im asking
Base Class
Code:
class Base
{
public:
virtual void Display()
{
cout << "Class Base";
}
};
Derived Class
Code:
class Derived : public Base
{
public:
virtual void Display()
{
cout << "Class Derived";
}
};
Main
Code:
void main()
{
Base *temp = new Derived;
temp->Display();
}
lets say i want temp to show "Class Base" as output... would that mean id have to call it like i mentioned above ? i.e temp->Base:: Display(); ? And if that is the case, will that still be 'polymorphism' since it could be more like overriding and not needing to use virtual in prototype
Thanks for your help!
-
>> would that mean id have to call it like i mentioned above ? i.e temp->Base::Display(); ?
Yes. (Disable smilies in advanced forum editor to remove the smilie).
>> will that still be 'polymorphism'
No, it isn't polymorphism. In most cases you don't really want to do that because the entire point of a virtual function is to let the derived class implement it however it wants. The most common use of Base::Display() is if a derived class wanted to call the base class version itself.