R
Rahul K
Hi all
I tried running the following code:
#include<iostream.h>
class Base
{
public:
virtual void func()
{
cout << "Inside Base Class" << endl;
}
};
class Derivedublic Base
{
private:
void func()
{
cout << "Inside Derived" << endl;
}
};
void main()
{
Derived d;
Base *bptr;
bptr = &d;
bptr->func();
}
As per my knowledge, I thought that since the derived class does not
publicly override the virtual function of base class, the func() of
base class will be called. Also, since the derived class defines func()
in private, there is no question of it getting called from main.
However to my surprise, the output was :
Inside Derived
Can anybody explain me this behaviour that how a private function was
called. Does it means that overriding a virtual function in derived
class either in private or public will lead to run time polymorphism.
I tried running the following code:
#include<iostream.h>
class Base
{
public:
virtual void func()
{
cout << "Inside Base Class" << endl;
}
};
class Derivedublic Base
{
private:
void func()
{
cout << "Inside Derived" << endl;
}
};
void main()
{
Derived d;
Base *bptr;
bptr = &d;
bptr->func();
}
As per my knowledge, I thought that since the derived class does not
publicly override the virtual function of base class, the func() of
base class will be called. Also, since the derived class defines func()
in private, there is no question of it getting called from main.
However to my surprise, the output was :
Inside Derived
Can anybody explain me this behaviour that how a private function was
called. Does it means that overriding a virtual function in derived
class either in private or public will lead to run time polymorphism.