1 2 3 4 5 6 7 8 9 10 11

If pis a pointer to a Shapeobject, then since Circleand Squareare derived from the Shapeclass, pcould be pointing to either one of these classes. If ppoints to a Circleobject then Circle::growwill be used, and conversely Square::growif ppoints to a Squarewhich has to be expanded.

Shape* p; Circle c; Square s;

p = &c;
p->grow(); p = &s;
p->grow();

// p points to a Circle // calls Circle :: grow // p points to a Square // calls Square :: grow

Notice the use of ->to call grow. Calling a member function from a pointer to an object requires ->instead of the dot operator.

Many function calls--even overloaded ones--can be resolved by the compiler. Others can't and require dynamic binding by the loader (just how this is done is left to the operating systems course), but the need is clear. Consider:
if (....)
p = &c;
else
p = &s;
p->grow();// calls either Circle::grow
//orSquare::grow
There is no way that the compiler can know how p will be set during execution.

March 14, 1998

Page 2

C201/TAM