C++语言继承与多态练习--计算图形面积
小标 2018-07-25 来源 : 阅读 1484 评论 0

摘要:本文主要向大家介绍了C++语言继承与多态练习--计算图形面积,通过具体的内容向大家展示,希望对大家学习C++语言有所帮助。

本文主要向大家介绍了C++语言继承与多态练习--计算图形面积,通过具体的内容向大家展示,希望对大家学习C++语言有所帮助。

1.目的:

/*设计一个计算图形面积的类库。

类库的顶层是一个抽象类,并且提供三个纯虚函数;显示数据成员、返回面积和返回体积。

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 hereclass 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 reArea() {

        return PI*pow(x, 3)*4.0 / 3.0;

    }

    double reVolume() {

        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;

    }

};//RectangularParallelepipedclass 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.关于多态性

 在这个例子中我用了基类指针指向基类,这个不难理解,在类型兼容规则下,

 指向基类的指针可以隐式的转换成派生类的指针。

 这是最常见的关于多态的用法,利用该指针指向任意一个子类对象,

 就可以调用相应的虚函数,指向的子类的不同,实现的方法也就不同。

以上就介绍了C/C+的相关知识,希望对C/C+有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程