Dear sir!
I have to tell you what I know and what I donβt know about the question asked, so that you can turn to a weak area of ββmy understanding.
I know that C ++ implements polymorphism using Vtable, which is an array of pointers, each pointer points to a virtual function of the class, each class in the hierarchy has a vtable. now suppose i have a class
class person { char name[20]; public: person(char* pname) { strcpy(name,pname); } virtual void show() { cout<<"inside person show method, Name: "<<name; } }; class teacher:public person { int scale; teacher(char*pname, int s):person(pname) { scale=s; } void show() { cout<<"inside the teacher show method, Scale: "<<scale; } };
Now suppose I write in the main program
person *ptr; ptr=new teacher(16,"Zia"); ptr->show();
now i'm confused at this point, the call will go to the show function of the base class, now since it is a virtual function, so inturn calls the corresponding function. I know that I am not right here. I am confused by what will be the sequence of calls. What role does Vtable play and how does it work, please specify.
c ++ oop
Zia ur rahman
source share