Week 3 Project 3: Define the abstract class csolid, a pure virtual number containing the surface area and volume, and derive ccube, cball, and ccylinder, representing the cube, sphere, and cylinder respectively.

Source: Internet
Author: User
/** Copyright and version declaration section of the Program * Copyright (c) 2013, student * All rightsreserved from computer College of Yantai University. * Prepared by: Li Yang * completed on: July 15, May 31, 2013 * version: v1.0 * input Description: none * Problem description: Output surface area and volume */# include <iostream> using namespace std; # define PI 3.1415926 class CSolid // declare the abstract base class shape {public: virtual double area () = 0; // The pure virtual function virtual double volume () = 0; // pure virtual function}; class CCube: public CSolid // declare CCube class {public: CCube (double l): length (l) {} virtual double area () // area {return 6 * length;} virtual double volume () // volume {return length * length;} private: double length ;}; class CBall: public CSolid // declare the CBall class {public: CBall (double r): radius (r) {} virtual double area () // area {return 4 * PI * radius;} virtual double volume () // volume {return PI * radius * 4/3;} private: double radius ;}; class CCylinder: public CSolid // declare CCylinder class {public: CCylinder (double r, double h): radius (r), heigth (h) {} virtual double area () // area {return 2*2 * PI * radius + 2 * PI * radius * heigth;} virtual double volume () // volume {return PI * radius * heigth;} private: double radius, heigth;}; int main () {CSolid * p; CCube a (3 ); cout <"cube length: 3" <endl; p = & a; cout <"surface area of the cube:" <p-> area () <endl; cout <"volume of the cube:" <p-> volume () <endl; cout <endl; CBall B (4 ); cout <"sphere radius is 4" <endl; p = & B; cout <"surface area of the sphere:" <p-> area () <endl; cout <"volume of the sphere:" <p-> volume () <endl; cout <endl; CCylinder c (1.5, 4 ); cout <"the bottom radius and height of the cylinder are 1.5, 4 respectively." <endl; p = & c; cout <"surface area of the cylinder: "<p-> area () <endl; cout <" volume of the cylinder: "<p-> volume () <endl; return 0 ;}

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.