Write a program that defines the abstract base class shape, derived from it by 3 derived classes, Circle (circle), Rectangle (Rectangle), Triangle (triangle). Use the following main () function to find the area and the number of geometry defined.
int main () { Circle C1 (12.6), C2 (4.9),//Create Circle Class object C1,c2, parameters are circle radius Rectangle R1 (4.5,8.4), R2 (5.0,2.5);// Establish the Rectangle class object R1,R2, the parameters are rectangle length, width Triangle T1 (4.5,8.4), T2 (3.4,2.8),//Triangle class object T1,t2, the parameters are triangular bottom length and high Shape *pt [6]= {&c1,&c2,&r1,&r2,&t1,&t2}; Defines the base class pointer array pt, which points each element to a derived class object double areas=0.0;//areas for the total area for (int i=0; i<6; i++) { Areas=areas + Pt[i]->area (); } cout<< "Totol of all areas=" <<areas<<endl; Output total area return 0;}
<pre name= "code" class= "CPP" > #include <iostream>using namespace std;//define abstract base class Shapeclass Shape{public: Virtual double area () const = 0; Pure virtual function, write const is because this function only value does not change the data member, so with a const protection, and virtual function independent};//definition Circle class Circle:public shape{public:circle (Double R): Radius (r) {}//constructor virtual double area () const//base class with the same name pure virtual function with const, here also must write, to show the same function, otherwise think not to implement pure virtual function, is still abstract class, can not define object--Instance of class {return 3.14159*radius*radius; }; Defines the virtual function protected:double radius; Radius};//definition Rectangle class Rectangle:public Shape{public:rectangle (Double w,double h): Width (w), height (h) {} struct function Virtual double area () const//base class with the same name pure virtual function with const, here also must write, to show the same function, otherwise think not to implement pure virtual function, is still abstract class, cannot define object--instance of class {re Turn width*height; Define virtual function}protected:double width,height; Width and height};class triangle:public shape{public:triangle (double w,double h): Width (w), height (h) {}//Structural function VI Rtual Double area () const//Previous class {return 0.5*width*height; Define virtual function}protected:double width,height; Width and height};int main () {Circle C1 (12.6), C2 (4.9);//Create Circle Class object C1,c2, parameters are circle radius Rectangle R1 (4.5,8.4), R2 (5.0,2.5);//Build Rectangl Class E Object R1,R2, the parameters are rectangle length, width Triangle T1 (4.5,8.4), T2 (3.4,2.8); Establishes the Triangle class object T1,t2, with the parameters of the triangle bottom length and the high Shape *pt[6]= {&c1,&c2,&r1,&r2,&t1,&t2}; Defines the base class pointer array pt, which points each element to a derived class object double areas=0.0; Areas for the total area for (int i=0; i<6; i++) {areas=areas + Pt[i]->area (); } cout<< "Totol of all areas=" <<areas<<endl; Output total area return 0;}Operation Result:
13th Week item Two-pure virtual functions in the Shape class family