13th Week (abstract class shared by the three-dimensional group)

Source: Internet
Author: User

/*
*copyright (c) 2015, College of Computer Science, Yantai University
*all rights reserved.
* File name: Week 13th (abstract class shared by the stereoscopic group)

* Wangzhong
* Completion Date: 2015.6.02

* Version: v1.0
*
* Description of the problem: Design an abstract class Csolid. Contains two pure virtual functions which are useful for surface area and volume. Design derived classes Ccube, Cball, Ccylinder. represent cubes, spheres, and cylinders, respectively.

In the main () function, define Csolid *p; (P is a pointer to the base class, and the base class is an abstract class).

Requires the use of this P-pointer. The surface area and volume of the object of cube, sphere and cylinder can be obtained.


* Input Descriptive narrative:

* Program output:

#include <iostream>using namespace Std;class csolid{public:virtual Double volume () = 0; Virtual double area () = 0;};    Class Ccube:public Csolid{public:ccube (Double A): B (a) {} double volume () {return b*b*b;    } double Area () {return 6*b*b; }protected:double b;};    Class Cball:public Csolid{public:cball (Double A): R1 (a) {} double volume () {return (4*3.14*R1*R1*R1)/3;    } double Area () {return 4*3.14*r1*r1; }protected:double R1;}; Class Ccylinder:public Csolid{public:ccylinder (double a,double b): R2 (a), H (b) {} double volume () {return 3    .14*r2*r2*h;    } double Area () {return (6.28*R2*H+6.28*R2*R2);    }protected:double R2; Double h;};    int main () {csolid *p;    Ccube C1 (1);    Cball C2 (1);    Ccylinder C3 (a);    p=&c1;    Cout<<p->volume () << "";    Cout<<p->area () <<endl;    p=&c2;    Cout<<p->volume () << ""; Cout&lT;<p->area () <<endl;    p=&c3;    Cout<<p->volume () << "";    Cout<<p->area () <<endl; return 0;}


Write your own, one-time success, don't mention how cool!!

13th Week (abstract class shared by the three-dimensional group)

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.