Invoke the virtual toString() function

To invoke the virtual toString() function defined in GeometricObject from a Circle object c, use :
A. ((GeometricObject*)c)->toString();
B. c.super.toString()
C. (GeometricObject*)c->toString();
D. c->GeometricObject::toString()

Questions by dlee6565

Showing Answers 1 - 5 of 5 Answers

When using polymorphic functions(virtual), in order to access the BASE class version of the function from WITHIN the Derived class, one would simply say "Base::functionName()".

For e.g:

class CGeometricObject
{
public:
virtual void toString()
{
// do something..
}
};

class CCircle : public CGeometricObject
{
public:
void toString()
{
CGeometricObject::toString();  // call BASE class version of toString (first)
}
}

  Was this answer useful?  Yes

If we take the following class definitions:

class CGeometricObject
{
public:
virtual void toString()
{
 printf("I am in base");
}
};

class Circle : public GeometricObject
{
public:
void toString()
{
 
printf("I am in derived");
}
};




then since we are not declaring pointer to object for circle object , so we can not use '->' operator to call function.
So 'D' option would be right if we call funtion in this way

c.GeometricObject::toString()

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions