B
babu
class CPrintString
{
void printName(Person* p)
{
printf("person");
}
void printName(Student * s)
{
printf("student");
}
};
Here, Person is the base class of Student. Now if we write -
Person * st = new Student();
CPrintString * pr =new CPrintString();
pr->printName(st);
The output is "person". But we need "student" to be printed. Why such
thing occurs?
Now What is the solution so that the correct function is called at
runtime based on the parameter object type, so that "student" can be
printed in the previous calling sequence? Is there any solution
without changing class CPrintString? If exists please give.
Thanks
Babu
{
void printName(Person* p)
{
printf("person");
}
void printName(Student * s)
{
printf("student");
}
};
Here, Person is the base class of Student. Now if we write -
Person * st = new Student();
CPrintString * pr =new CPrintString();
pr->printName(st);
The output is "person". But we need "student" to be printed. Why such
thing occurs?
Now What is the solution so that the correct function is called at
runtime based on the parameter object type, so that "student" can be
printed in the previous calling sequence? Is there any solution
without changing class CPrintString? If exists please give.
Thanks
Babu