/* Copyright (c) 2012, School of Computer Science, Yantai University * All Rights Reserved. * file name: text. CPP * Author: Hu Ying * Completion Date: July 15, June 4, 2013 * version number: V1.0 ** input Description: none * Problem description: design an abstract class csolid, contains two pure virtual functions to evaluate the surface area and volume. The ccube, cball, and ccylinder classes are designed to represent cubes, sphere, and cylinder. In the main () function, define the base class pointer P (csolid * P;) and use the P pointer to output the surface area and volume of the cube, sphere, and cylindrical objects. * Program Output: the output defines several three-dimensional volume and surface area * Problem Analysis: none * Algorithm DESIGN: omitted */# include <iostream> using namespace STD; // abstract class csolidclass csolid {public: Virtual double surfacearea () const = 0; // pure virtual function of surface area virtual double volume () const = 0; // pure virtual function of volume}; // derived ccube (cube) Class ccube: public csolid {public: ccube (double Len): length (LEN) {} virtual double surfacearea () const {return 6 * length;} virtual double volume () const {return length * length;} PRIVATE: double length;}; // derived cball (sphere) Class cball: Public csolid {public: cball (Double R ): radius (r) {} virtual double surfacearea () const {return 4*3.1415926 * radius;} virtual double volume () const {return (4/3) * 3.1415926 * radius;} PRIVATE: Double Radius;}; // derived ccylinder (cylinder) Class ccylinder: Public csolid {public: ccylinder (Double R, double H): radius (R), height (H) {} virtual double surfacearea () const {return 2*3.1415926 * radius + 2*3.1415926 * radius * height ;} virtual double volume () const {return 3.1415926 * radius * height;} PRIVATE: Double Radius; double height ;}; int main () {csolid * P; double S, v; // output cube surface area and volume ccube X (30); cout <"cube side length: 30:" <Endl; P = & X; S = p-> surfacearea (); V = p-> volume (); cout <"surface area:" <S <Endl; cout <"volume: "<v <Endl; cout <Endl; // output sphere surface area and size cball y (4.5); cout <" sphere radius: 4.5: "<Endl; P = & Y; S = p-> surfacearea (); V = p-> volume (); cout <"surface area:" <S <Endl; cout <"volume:" <v <Endl; cout <Endl; // the surface area and volume of the output cylinder Z ); cout <"the cylinder radius is 10 and the height is 20:" <Endl; P = & Z; S = p-> surfacearea (); V = p-> volume (); cout <"surface area:" <S <Endl; cout <"volume:" <v <Endl; cout <Endl; return 0 ;}
running result: