Guess the output?
#include<iostream>
class Language
{
public:
virtual void getName()
{
std::cout<<"my name is
Language"<<std::endl;
}
void getProperties()
{
getName();
}
};
class cpp : public
Language
{
public:
virtual void getName()
{
std::cout<<"my name is
cpp"<<std::endl;
}
};
int main()
{
Language *vptr=new cpp;
vptr->getProperties();
return 0;
}
OUTPUT:
my name is cpp
EXPLANATION:
Even though the virtual function is called within the other member
function of any class, always virtual function will resolve based on vtable.
Possibly getName function in getProperties will resolve in following
manner
*(this->vptr[0])()
//Here 0 refers the getName
function.
No comments:
Post a Comment