//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 = ▭ 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 = ▭ 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 = ▭ 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