C + + polymorphic polymorphism

Source: Internet
Author: User

//pointers to base class#include <iostream>using namespacestd;classPolygon {protected:    intwidth, height;  Public:    voidSet_values (intAintb) {width=a; height=b;}};classRectangle: PublicPolygon { Public:    intArea () {returnwidth*height;}};classTriangle: PublicPolygon { Public:    intArea () {returnwidth*height/2; }};intMain () {Rectangle rect;  Triangle Trgl; Polygon* Ppoly1 = &rect; Polygon* Ppoly2 = &Trgl; Ppoly1->set_values (4,5); The function of the base class is used here Ppoly2->set_values (4,5); cout<< Rect.area () <<'\ n'; A function call for a subclass is used here, and the base class can only be used with the cout of the base class<< Trgl.area () <<'\ n'; return 0;}

Virtual member Vitual members

//Virtual Members#include <iostream>using namespacestd;classPolygon {protected:    intwidth, height;  Public:    voidSet_values (intAintb) {width=a; height=b;} Virtual intArea ()//virtual function {return 0; }};classRectangle: PublicPolygon { Public:    intArea () {returnWidth *height;}};classTriangle: PublicPolygon { Public:    intArea () {return(Width * Height/2); }};intMain () {Rectangle rect;  Triangle Trgl;  Polygon Poly; Polygon* Ppoly1 = &rect; Polygon* Ppoly2 = &Trgl; Polygon* Ppoly3 = &Poly; Ppoly1->set_values (4,5); Ppoly2->set_values (4,5); Ppoly3->set_values (4,5); cout<< Ppoly1->area () <<'\ n'; cout<< Ppoly2->area () <<'\ n'; Ten cout<< Ppoly3->area () <<'\ n'; 0 This call is a default, but it is possible to use the non-standard, forget the implementation is not very good
return 0;}

Abstract Base CLASS,ABC is a class in which a pure virtual member function is defined, and the pure virtual function only provides an interface, and there is no specific implementation. Abstract classes cannot be instantiated (objects cannot be created), usually as a base class for subclass inheritance, overriding virtual functions in subclasses to implement specific interfaces. The ABC method is more systematic and more canonical in dealing with inheritance issues.

//pure virtual members can be called//From the abstract base class#include <iostream>using namespacestd;classPolygon {protected:    intwidth, height;  Public:    voidSet_values (intAintb) {width=a; height=b;} Virtual intArea () =0; Pure virtual function, to the sub-class to achievevoidPrintArea () {cout<< This->area () <<'\ n'; }};classRectangle: PublicPolygon { Public:    intArea (void)      { return(Width *height); }};classTriangle: PublicPolygon { Public:    intArea (void)      { return(Width * Height/2); }};intMain () {Rectangle rect;  Triangle Trgl; Polygon* Ppoly1 = &rect; Polygon* Ppoly2 = &Trgl; Ppoly1->set_values (4,5); Ppoly2->set_values (4,5); Ppoly1-PrintArea (); Ppoly2-PrintArea (); Tenreturn 0;}
//dynamic allocation and polymorphism#include <iostream>using namespacestd;classPolygon {protected:    intwidth, height;  Public: Polygon (intAintb): Width (a), height (b) {}Virtual intArea (void) =0; voidPrintArea () {cout<< This->area () <<'\ n'; }};classRectangle: PublicPolygon { Public: Rectangle (intAintb): Polygon (A, b) {}intArea () {returnwidth*height;}};classTriangle: PublicPolygon { Public: Triangle (intAintb): Polygon (A, b) {}intArea () {returnwidth*height/2; }};intMain () {Polygon* Ppoly1 =NewRectangle (4,5); Polygon* Ppoly2 =NewTriangle (4,5); Ppoly1-PrintArea (); Ppoly2-PrintArea (); Deleteppoly1; DeletePpoly2; return 0;}

C + + polymorphic polymorphism

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.