鱼C论坛

 找回密码
 立即注册
查看: 1442|回复: 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
  1. #include<iostream>
  2. using namespace std;

  3. class Shape
  4. {
  5. public:
  6.         virtual float Area(void) = 0;                                                //求面积
  7.         virtual void Setdata(float, float = 0) = 0;                //设置图形数据
  8. };

  9. class Triangle :public Shape
  10. {
  11.         float W, H;                                                                                        //三角形边长为W,高为H
  12. public:
  13.         Triangle(float w = 0, float h = 0){ W = w;   H = h; }
  14.         float Area(void){ return  W*H / 2; }
  15.         void Setdata(float w, float h = 0){ W = w;  H = h; }
  16. };

  17. class Rectangle :public Shape
  18. {
  19.         float W, H;
  20. public:
  21.         Rectangle(float w = 0, float h = 0){ W = w; H = h; }
  22.         float Area(void){ return W*H; }
  23.         void Setdata(float w, float h){ W = w; H = h; }
  24. };

  25. class Square :public Shape
  26. {
  27.         float S;
  28. public:
  29.         float Area(void){ return S*S; }
  30.         void Setdata(float s,float){ S = s; }
  31. };

  32. class Circle :public Shape
  33. {
  34.         float R;
  35. public:
  36.         float Area(void){ return 3.1415926*R*R; }
  37.         void Setdata(float r, float){ R = r; }
  38. };

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

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

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

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

  64.         return 0;
  65. }
复制代码



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

使用道具 举报

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

  3. class Shape
  4. {
  5. public:
  6.         virtual float Area(void) = 0;                                                //求面积
  7.         virtual void Setdata(float, float = 0) = 0;                //设置图形数据
  8. };

  9. class Triangle :public Shape
  10. {
  11.         float W, H;                                                                                        //三角形边长为W,高为H
  12. public:
  13.         Triangle(float w = 0, float h = 0){ W = w;   H = h; }
  14.         float Area(void){ return  W*H / 2; }
  15.         void Setdata(float w, float h = 0){ W = w;  H = h; }
  16. };

  17. class Rectangle :public Shape
  18. {
  19.         float W, H;
  20. public:
  21.         Rectangle(float w = 0, float h = 0){ W = w; H = h; }
  22.         float Area(void){ return W*H; }
  23.         void Setdata(float w, float h){ W = w; H = h; }
  24. };

  25. class Square :public Shape
  26. {
  27.         float S;
  28. public:
  29.         float Area(void){ return S*S; }
  30.         void Setdata(float s,float){ S = s; }
  31. };

  32. class Circle :public Shape
  33. {
  34.         float R;
  35. public:
  36.         float Area(void){ return 3.1415926*R*R; }
  37.         void Setdata(float r, float){ R = r; }
  38. };

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

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

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

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

  64.         return 0;
  65. }
复制代码



建议看一下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-5-1 04:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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