鱼C论坛

 找回密码
 立即注册
查看: 1519|回复: 2

[已解决]新手求助!如何使用虚函数

[复制链接]
发表于 2017-4-20 10:47:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
利用虚函数实现的多态性来求四种几何图形的面积之和。这四种几何图形是:三角形、矩形、正方形和圆。几何图形的类型可以通过构造函数或通过成员函数来设置。
计算这四种几何图的面积公式分别是:
三角形的边长为W,高为H时,则三角形的面积为W* H/2;矩形的边长为W,宽为H时,则其面积为W* H;正方形的边长为S,则正方形的面积为S*S;圆的半径为R,其面积为 3.1415926 *R *R。
最佳答案
2017-4-20 11:12:58
#include<iostream>
using namespace std;

class Shape
{
public:
        virtual float Area(void) = 0;                                                //求面积
        virtual void Setdata(float, float = 0) = 0;                //设置图形数据
};

class Triangle :public Shape
{
        float W, H;                                                                                        //三角形边长为W,高为H
public:
        Triangle(float w = 0, float h = 0){ W = w;   H = h; }
        float Area(void){ return  W*H / 2; }
        void Setdata(float w, float h = 0){ W = w;  H = h; }
};

class Rectangle :public Shape
{
        float W, H;
public:
        Rectangle(float w = 0, float h = 0){ W = w; H = h; }
        float Area(void){ return W*H; }
        void Setdata(float w, float h){ W = w; H = h; }
};

class Square :public Shape
{
        float S;
public:
        float Area(void){ return S*S; }
        void Setdata(float s,float){ S = s; }
};

class Circle :public Shape
{
        float R;
public:
        float Area(void){ return 3.1415926*R*R; }
        void Setdata(float r, float){ R = r; }
};

int main()
{
        Triangle tri;
        int w, h;
        cout << "请输入三角形的底和高:";
        cin >> w >> h;
        tri.Setdata(w, h);
        cout << "三角形的面积为:" << tri.Area() << endl;

        Rectangle rec;
        cout << "请输入矩形的长和宽:";
        cin >> w >> h;
        rec.Setdata(w, h);
        cout << "矩形的面积为:" << rec.Area() << endl;

        Square squ;
        int s;
        cout << "请输入正方形的边长:";
        cin >> s;
        squ.Setdata(s,s);
        cout << "正方形的面积为:" << squ.Area() << endl;

        Circle cir;
        int r;
        cout << "请输入圆的半径:";
        cin >> r;
        cir.Setdata(r, r);
        cout << "圆的面积为:" << cir.Area() << endl;

        return 0;
}


建议看一下http://bbs.fishc.com/forum.php?mod=viewthread&tid=31380
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-20 11:12:58 | 显示全部楼层    本楼为最佳答案   
#include<iostream>
using namespace std;

class Shape
{
public:
        virtual float Area(void) = 0;                                                //求面积
        virtual void Setdata(float, float = 0) = 0;                //设置图形数据
};

class Triangle :public Shape
{
        float W, H;                                                                                        //三角形边长为W,高为H
public:
        Triangle(float w = 0, float h = 0){ W = w;   H = h; }
        float Area(void){ return  W*H / 2; }
        void Setdata(float w, float h = 0){ W = w;  H = h; }
};

class Rectangle :public Shape
{
        float W, H;
public:
        Rectangle(float w = 0, float h = 0){ W = w; H = h; }
        float Area(void){ return W*H; }
        void Setdata(float w, float h){ W = w; H = h; }
};

class Square :public Shape
{
        float S;
public:
        float Area(void){ return S*S; }
        void Setdata(float s,float){ S = s; }
};

class Circle :public Shape
{
        float R;
public:
        float Area(void){ return 3.1415926*R*R; }
        void Setdata(float r, float){ R = r; }
};

int main()
{
        Triangle tri;
        int w, h;
        cout << "请输入三角形的底和高:";
        cin >> w >> h;
        tri.Setdata(w, h);
        cout << "三角形的面积为:" << tri.Area() << endl;

        Rectangle rec;
        cout << "请输入矩形的长和宽:";
        cin >> w >> h;
        rec.Setdata(w, h);
        cout << "矩形的面积为:" << rec.Area() << endl;

        Square squ;
        int s;
        cout << "请输入正方形的边长:";
        cin >> s;
        squ.Setdata(s,s);
        cout << "正方形的面积为:" << squ.Area() << endl;

        Circle cir;
        int r;
        cout << "请输入圆的半径:";
        cin >> r;
        cir.Setdata(r, r);
        cout << "圆的面积为:" << cir.Area() << endl;

        return 0;
}


建议看一下http://bbs.fishc.com/forum.php?mod=viewthread&tid=31380
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-4-20 11:15:41 | 显示全部楼层
轩~ 发表于 2017-4-20 11:12
建议看一下http://bbs.fishc.com/forum.php?mod=viewthread&tid=31380

非常感谢!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-28 05:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表