An abstract class Csolid is designed to contain two pure virtual functions for surface area and volume. Design derived classes Ccube, Cball, Ccylinder, respectively, representing cubes, spheres, and cylinders. In the main () function, define Csolid *p; (P is a pointer to the base class, and the base class is an abstract class). This P-pointer is required to be able to find the surface area and volume of a cube, sphere, and cylinder object.
The main () function uses the old on the.
/** Copyright (c) 2015, Yantai University School of Computer * All right reserved.* Shao * file: demo.cpp* finish: June 07, 2015 * version number: v1.0*/#include <iostream > #include <string>using namespace std;const double pi=3.14;class csolib{public:virtual double area () = 0; Virtual double volume () = 0;}; Class Ccube:public csolib{private:double Length;public:ccube (double L): Length (l) {} double area (); Double volume ();}; Double Ccube::area () {return (6*length*length);} Double Ccube::volume () {return (length*length*length);} Class Cball:public csolib{private:double Radius;public:cball (Double R): Radius (r) {} double area (); Double volume ();}; Double Cball::area () {return (4*pi*radius*radius);} Double Cball::volume () {return (PI*RADIUS*RADIUS*RADIUS*4/3);} Class Ccylinder:public csolib{private:double radius; Double Height;public:ccylinder (double r,double h): Radius (R), height (h) {} double area (); Double volume ();}; Double Ccylinder::area () {return (2*pi*radius*radius+2*pi*radius*height);} Double Ccylinder::volume () {return (pi*radius*radius*height);} int main () {csolib *p; Double s,v; Ccube x (30); cout<< "cube side Length" <<endl; p=&x; S=p->area (); V=p->volume (); cout<< "surface area:" <<s<<endl; cout<< "Volume:" <<v<<endl; cout<<endl; Cball y (4.5); cout<< "Sphere radius is 4.5" <<endl; p=&y; S=p->area (); V=p->volume (); cout<< "surface area:" <<s<<endl; cout<< "Volume:" <<v<<endl; cout<<endl; Ccylinder Z (10,20); cout<< "Cylinder bottom radius, height is ten," <<endl; p=&z; S=p->area (); V=p->volume (); cout<< "surface area:" <<s<<endl; cout<< "Volume:" <<v<<endl; cout<<endl; return 0;}
Operation Result:
@ Mayuko
Item 3-Abstract classes common to stereo classes