PART I
C h a p t e r 1 1 :
I n h e r i t a n c e
301
PART IPART I
Let’s examine this program closely. First, as explained,
Area( )
is declared as
virtual
in
the
TwoDShape
class and is overridden by
Triangle
and
Rectangle
. Inside
TwoDShape
,
Area( )
is given a placeholder implementation that simply informs the user that this method
must be overridden by a derived class. Each override of
Area( )
supplies an implementation
that is suitable for the type of object encapsulated by the derived class. Thus, if you were to
implement an ellipse class, for example, then
Area( )
would need to compute the area of an
ellipse.
There is one other important feature in the preceding program. Notice in
Do'stlaringiz bilan baham: |