Item 3-Abstract classes common to stereo classes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.