• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
p-boost-q
博客园    首页    新随笔    联系   管理    订阅  订阅
多继承和单继承
#include <iostream>
 
using namespace std;
 
class shape
{
public:
    void setHigh(double _high)
    {
        high = _high;
    }

    void setWidth(double _width)
    {
        width = _width;
    }

public:
    double high;
    double width;
};

class cost
{
public:
    int getCost(double area)
    {

        return area * 2;
    }

};

class rectangle : public shape
{
public:
    double getarea()
    {
        return high * width;
    }
};

class rectangleCost : public shape, public cost
{
public:
    double getarea()
    {
        return high * width;
    }
};

int main ()
{
    rectangle rect;
    rect.setHigh(2);
    rect.setWidth(4);

    double area = rect.getarea();
    cout << "面积:" << area << endl;

    rectangleCost rectCost;
    rectCost.setHigh(2);
    rectCost.setWidth(5);

    double area_c = rectCost.getarea();
    double cost = rectCost.getCost(area_c);
     cout << "面积 :" << area_c << endl;
    cout << "花费 :" << cost << endl;


   return 0;
}

输出结果:

面积:8
面积 :10
花费 :20

 

posted on 2022-05-08 20:43  p-boost-q  阅读(32)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3