Pure virtual function classes in cube groups

Source: Internet
Author: User
/** Copyright (c) 2013, School of Computer Science, Yantai University * All Rights Reserved. * file name: test. CPP * Author: Qiu xuewei * Completion Date: July 15, June 2, 2013 * version: V1.0 * input Description: none * Problem description: * program output: * Problem Analysis: * Algorithm Design: * // * design an abstract class csolid, which 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. */# Include <iostream> using namespace STD; Class csolid // base class {public: Virtual double area () const = 0; // pure virtual function, evaluate the area virtual double volume () const = 0; // pure virtual function, evaluate the volume}; Class ccube: Public csolid // cube {public: ccube (double W ): width (w) {} virtual double area () const // cube surface area {return 6 * width;} virtual double volume () const // cube volume {return width * width;} PRIVATE: double width;}; Class cball: Public csolid // sphere {public: cball (double R0 ): R (R0) {} virtual double area () const // sphere surface area {return 4*3.14 * r;} virtual double volume () const // volume of the sphere {return (4*3.14 * r)/3;} PRIVATE: Double R ;}; class ccylinder: Public csolid {public: ccylinder (double r0, double H): R (R0), height (H) {} virtual double area () const {return 2*(3.14 * r) + height * 3.14 * r * 2;} virtual double volume () const {return height * 3.14 * r;} PRIVATE: Double R, height;}; int main () {csolid * P; double S, V; ccube C1 (30); P = & C1; S = p-> area (); V = p-> volume (); cout <"the cube surface area with a side length of 30 is:" The size of the cube with a side length of 30 <S <Endl; cout <"side length of 30 is: "<v <Endl; cball C2 (5); P = & C2; S = p-> area (); V = p-> volume (); cout <"the surface area of the ball with a radius of 5 is:" The size of the ball with a radius of 5 is: "<v <Endl; ccylinder C3 (5, 4); P = & C3; S = p-> area (); V = p-> volume (); cout <"the surface area of a cylindrical with a radius of 5 and a height of 4 is:" <S <Endl; cout <"the volume of a cylindrical with a radius of 5 and a height of 4 is: "<v <Endl ;}

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.