Abstract classes in the 14-3 stereo class family

Source: Internet
Author: User
/* 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:

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.