C + + polymorphic

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.