首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++继承与多态练习--计算图形面积

C++继承与多态练习--计算图形面积

作者头像
Enterprise_
发布2019-02-21 17:23:31
6360
发布2019-02-21 17:23:31
举报
文章被收录于专栏:小L的魔法馆小L的魔法馆

1.目的:

/*//修改--5-16-关于球体表面积和体积公式错误*/
/*设计一个计算图形面积的类库。
类库的顶层是一个抽象类,并且提供三个纯虚函数;显示数据成员、返回面积和返回体积。
Class Shape
{
virtual void showData()=0;
virtual double reArea()=0;
virtual double reVolume()=0;
};
第二层由Shape类派生TwoDimShape(二维图形)和ThreeShape(三维图形),
它们增加了有关的数据成员,但没有成员函数的实现。
第三层派生具体的图形类。TwoDimShape类派生Circle(圆)、Elipse(椭圆)、
Rectangle(矩形)和Triangle(三角形)等类。
ThreeShape类派生Ball(球体)、Cylinder(圆柱体)、
RectangularParallelepiped(长方体)等类。
在主函数测试中使用多态方式调用不同对象的求值函数。
*/

2.代码如下:

/*
*This file contains code for C++ 6th experiment
*By LZH
*/
#include<iostream>
#include<string>
using namespace std;
const double PI = acos(-1.0);
// Definition of Shape class and related functions goes here
class Shape
{
    virtual void showData() = 0;
    virtual double reArea() = 0;
    virtual double reVolume() = 0;
};
class TwoDimShape :virtual public Shape {
protected:
    double x, y;
public:
    virtual void showData() {
        return ;
    }
    virtual double reArea() {
        return 0;
    }
    virtual double reVolume() {
        return 0;
    }
};
class ThreeShape :virtual public Shape {
protected:
    double x, y, z;
public:
    virtual void showData() {
        return;
    }
    virtual double reArea() {
        return 0;
    }
    virtual double reVolume() {
        return 0;
    }
};
class Circle :virtual public TwoDimShape {
public:
    Circle(double tp) {
        x = tp;
    }
    Circle(Circle &asp) {
        x = asp.x;
    }
    ~Circle()
    {

    }
    void showData() {
        cout << "This is a Circle:" << endl
            << "The radiation:" << x << endl
            << "The area:" << reArea() << endl;
    }
    double reArea() {
        return PI*x*x;
    }
};
class Elipse :virtual public TwoDimShape {
public:
    Elipse(double ta, double tb) {
        x = ta, y = tb;
    }
    Elipse(Elipse &asp) {
        x = asp.x, y = asp.y;
    }
    ~Elipse() {

    }
    void showData() {
        cout << "This is a Elipse:" << endl
            << "The long axis:" << x << endl
            << "The short axis:" << y << endl
            << "The area:" << reArea() << endl;
    }
    double reArea() {
        return PI*x*y;
    }
};
class Rectangle :virtual public TwoDimShape {
public:
    Rectangle(double ta, double tb) {
        x = ta, y = tb;
    }
    Rectangle(Rectangle &asp) {
        x = asp.x, y = asp.y;
    }
    ~Rectangle() {

    }
    void showData() {
        cout << "This is a Rectangle:" << endl
            << "The long axis:" << x << endl
            << "The short axis:" << y << endl
            << "The area:" << reArea() << endl;
    }
    double reArea() {
        return x*y;
    }
};
class Triangle :virtual public TwoDimShape {
public:
    Triangle(double ta, double tb) {
        x = ta, y = tb;
    }
    Triangle(Triangle &asp) {
        x = asp.x, y = asp.y;
    }
    ~Triangle() {

    }
    void showData() {
        cout << "This is a Triangle:" << endl
            << "The base length:" << x << endl
            << "The height  :" << y << endl
            << "The area:" << reArea() << endl;
    }
    double reArea() {
        return x*y / 2.0;
    }
};
class Ball :virtual public ThreeShape {
public:
    Ball(double ta) {
        x = ta;
    }
    Ball(Ball &asp) {
        x = asp.x;
    }
    ~Ball() {

    }
    void showData() {
        cout << "This is a Ball:" << endl
            << "The radiation:" << x << endl
            << "The surface area:" << reArea() << endl;
    }
    double reVolume() {
        return PI*pow(x, 3)*4.0 / 3.0;
    }
    double reArea() {
        return PI*x*x;
    }
};
class Cylinder :virtual public ThreeShape {
public:
    /*
    V=PI*r*r*h S=2*PI*r+r*h
    */
    Cylinder(double ta, double tb) {
        x = ta, y = tb;
    }
    Cylinder(Cylinder &asp) {
        x = asp.x, y = asp.y;
    }
    ~Cylinder() {

    }
    void showData() {
        cout << "This is a Cylinder:" << endl
            << "The radiation:" << x << endl
            << "The height:" << y << endl
            << "The surface area:" << reArea() << endl;
    }
    double reArea() {
        return 2 * PI*x + x*y;
    }
    double reVolume() {
        return PI*x*x*y;
    }
};
//RectangularParallelepiped
class cuboid :virtual public ThreeShape {
public:
    cuboid(double ta, double tb, double tc) {
        x = ta, y = tb, z = tc;
    }
    cuboid(cuboid &asp) {
        x = asp.x, y = asp.y, z = asp.z;
    }
    void showData() {
        cout << "This is a cuboid:" << endl
            << "The length:" << x << endl
            << "The width:" << y << endl
            << "The height" << z << endl
            << "The surface area:" << reArea() << endl;

    }
    double reArea() {
        return 2 * (x*y + x*z + y*z);
    }
    double reVolume() {
        return x*y*z;
    }
};
int main(void) {
    TwoDimShape a;
    ThreeShape b;
    TwoDimShape *p = &a;
    ThreeShape *w = &b;
    Circle t1(1.0);
    Elipse t2(1.0, 2.0);
    Rectangle t3(10.0,2.3);
    Triangle t4(4.0, 5.0);
    Ball t5(2.33333);
    Cylinder t6(4.5, 65.0);
    cuboid t7(132, 5,156);
    p = &t1;
    p->showData();
    p = &t2;
    p->showData();
    p = &t3;
    p->showData();
    p = &t4;
    p->showData();
    w = &t5;
    w->showData();
    w = &t6;
    w->showData();
    w = &t7;
    w->showData();
    return 0;
}

3 . 测试截图

这里写图片描述
这里写图片描述

4.关于多态性

 在这个例子中我用了基类指针指向基类,这个不难理解,在类型兼容规则下,
 指向基类的指针可以隐式的转换成派生类的指针。
 这是最常见的关于多态的用法,利用该指针指向任意一个子类对象,
 就可以调用相应的虚函数,指向的子类的不同,实现的方法也就不同。

我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=30tgazvyzm4i

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年05月08日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档