01./* 02 .* Program Copyright and version description section 03. * copyright (c) 2013, Yantai University Computer college student 04. * All rightsreserved. 05. * file name: shape. CPP 06. * Author: Zhao guanzhe 07. * Completion Date: April 8, May 31, 2013. * version: V1.0 09. * input Description: 10. * Problem description: 11. */# include <iostream> using namespace STD; # define PI 3.1415926 class shape // declare the abstract base class shape {public: Virtual double area () = 0 ;}; class circle: public shape // declare the circle class {public: Circle (Double R): radius (r) {} virtual double area () {return pI * radius;} PRIVATE: double Radius ;}; class rectangle: Public shape // declare the rectangle class {public: rectangle (double L, double W): length (L), width (W) {} virtual double area () {return length * width;} PRIVATE: double length, width ;}; class triangle: Public shape // declare the triangle class {public: triangle (double L, double H): length (L), heigth (h) {} virtual double area () {return 0.5 * length * heigth;} PRIVATE: double length, heigth ;}; int main () {circle C1 (12.6), C2 (4.9); // create a circle Class Object C1, C2. The parameter is the circle radius rectangle r1 (4.5, 8.4), R2 (5.0, 2.5); // create rectangle class objects R1 and R2. The parameters are rectangular length, width, triangle T1 (4.5, 8.4), T2 (3.4, 2.8); // create a triangle Class Object T1 and T2. The parameter is the triangle bottom side length and height shape * PT [6] = {& C1, & C2, & R1, & R2, & T1, & T2}; // defines the base class pointer array PT, so that each element points to a derived class Object double areas = 0.0; // areas is the total area for (INT I = 0; I <6; I ++) {areas = areas + Pt [I]-> area ();} cout <"totol of all areas =" <areas <Endl; // return 0 ;}
Running result: