【问题标题】:how to implement adding a price to an object without modifying the uml diagram c++如何在不修改uml图c ++的情况下实现向对象添加价格
【发布时间】:2020-06-27 21:39:14
【问题描述】:

我不知道如何实现向菜单添加价格,所以我不更改 uml 图表。

要求说明:

抽象类Menu 有一个纯虚方法description()。
Withlemon 和Withcoffee 包含一个菜单和方法 description() 分别添加文本“With lemon”,“with 咖啡”,包含在菜单的描述中。
包含柠檬的菜单增加 4,包含咖啡的菜单花费 1 附加 5.
Breakfast 类是没有柠檬和 没有咖啡,方法 description() 返回菜单的名称。
在比萨店餐厅里有2个小菜早餐:鸡蛋 和煎蛋卷,价格分别为 10 和 15。

仅创建源自 UML 图的方法和属性 (只需添加特定的东西 C ++ 例如:构造函数)。

这里是 UML 图:

这是我的代码:

#include <iostream>
using namespace std;

class Menu {
private:
    int price = 0;
public:
    virtual string description() = 0;
    int getPrice() {
        return price;
    }
    

};
class WithLemon : public Menu {
private:
    Menu* meniu;
    
public:
    WithLemon() = default;
    WithLemon(Menu* n) :
        meniu{ n } {}
    string description() {
        return meniu->description() + " with lemon ";
    }
};

class WithCoffee : public Menu {
private:
    Menu* meniu;
public:
    WithCoffee(Menu* n) :
        meniu{ n } {
    }
    string description() {
        return meniu->description() + " with coffee ";
    }
};

class Breakfast : public Menu {
private:
      string name;
public:
    Breakfast(string n) :
        name{ n } {
    }
    string description() {
        return name;
    }
};

int main() {
    Breakfast a{"eggs"};
    WithCoffee transformer1{ &a };
    transformer1 = &a;
    cout << transformer1.description() << " " << transformer1.getPrice() << endl;
    return 0;
}

【问题讨论】:

  • 你的问题有点混乱。价格属性已经在 UML 图表中。您是否正在考虑获取方法?顺便说一句,这是考试的一部分吗?

标签: c++ class polymorphism uml abstract-class


【解决方案1】:

图表问题

首先,图表与叙述不符:

  • 已经有一个price 和一个getPrice()。因此,您可以完全按照 description() 进行操作:只需提供您自己的实现即可。
  • 那么,description 在Menu 中并没有显示为纯虚:操作应该以斜体显示。
  • 此外,由于WithLemon、WithCoffee 和Breakfast 提供了它们自己的description() 的具体实现,它也应该在这些类中显示。

提示:WithLemon 和 WithCoffee 是 decorators 的 Menu。

代码问题

在基类Menu 中,由于您有一个虚函数,因此最好给它一个虚析构函数。既然你有一个价格,之后就没有办法改变它了,它应该包含在构造函数中:

class Menu {
private:
    int price;
public:
    Menu(int p=0) :  price{p} {} 
    ...
    virtual ~Menu() {}
};

现在您还应该在Breakfast 中提供price,再次在施工时提供。一个小建议:无论何时从基类重写虚函数,使用关键字override;它将为您节省数小时的 nastry 错误调试时间:

class Breakfast : public Menu {
private:
      string name;
public:
    Breakfast(string n, int p) :
        name{ n }, Menu{ p}  {
    }
    string description() override {
        return name;
    }
};

你已经很好地实现了装饰器。出于同样的原因,我只是建议添加 override 关键字。

最后,为了满足所有要求,在不与类图相矛盾的情况下,您可以将getPrice() 设为虚拟:它不是纯虚函数,因为您在基础Menu 中提供了默认实现。在装饰器中,您只需用更专业的方法覆盖该方法:

class WithCoffee : public Menu {
private:
    Menu* meniu;
public:
    WithCoffee(Menu* n) :
        meniu{ n } {
    }
    string description() override{
        return meniu->description() + " with coffee ";
    }
    int getPrice() override{
        return meniu->getPrice()+5; 
    }
};

在我看来,这种覆盖应该显示在图表中。但是由于您的老师没有展示抽象方法的覆盖(这要重要得多),因此他/她不能抱怨在您的代码中覆盖了提供默认实现的方法。

最后,你可以测试一下:

Breakfast a{"eggs", 10};
WithCoffee breakfast_with_coffee { &a };
cout << breakfast_with_coffee.description() << " " << breakfast_with_coffee.getPrice() << endl;

这里是online demo。

重要提示:这当然仅用于学习目的。在现实世界中,你有办法改变价格。此外,decroators 不会只添加硬编码的文本和金额。最后,您可能会使用智能指针,而不是容易出错的原始指针。

【讨论】:

    猜你喜欢
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 2018-01-16
    • 2020-10-23
    • 2016-10-07
    • 2014-10-12
    • 1970-01-01
    相关资源
    最近更新 更多