Polymorphism (polymorphic)
Upcast:take an object of the derived class as an object of the base one.
-ellipse can treated as a Shape
Dynamic binding (dynamically bound)
-binding:which function to be called
-static Binding:call The function as the code (static binding, compiled when already determined,)
-dynamic Binding:call The function of the object (dynamic binding, which determines which function k is called by the runtime based on the object pointed to by the pointer, is related to virtual render ())
classxypos{};//Xy,pointclassshape{ Public: Shape (); Virtual~shape ();//virtual function,a subclass is related to a function of the parent class Virtual voidrender (); voidMoveConstXypos &); Virtual voidresize ();protected: Xypos Center;};classEllipse: Publicshape{ Public: Ellipse (floatMajfloatMinr); Virtual voidRender ();//Would define ownprotected: floatMajor_axis, Minor_axis;};classCircle: Publicellipse{ Public: Circle (floatradius): Ellipse (radius, radius) {}; Virtual voidrender ();};voidRender (shape*p) {P->render ();//invokes the correct render function according to the given shape;}voidfunc () {Ellipse ell (Ten, -); Ell.render (); Circle Circ ( +); Circ.render (); Render (&ell); Render (&circ);}
C + + polymorphic