c++练习--hotdog

这是一个关于使用C++编程实现Hotdog类的练习,根据顾客选择的配料计算总价。类图按照UML规范设计,代码中通过Hotdog类、WheatBread、Beef、Cheese、Mustard和Onion等类来构建hotdog,并提供了打印详情和总价的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要求:
1.根据顾客要求定义hotdog,计算总价
2.按uml类图编写
在这里插入图片描述
在这里插入图片描述
UML类图

测试样例:
int main() {
auto h = new Hotdog(new WheatBread());
h->addFood(new Beef());
h->addFood(new Cheese());
h->addFood(new Mustard());
h->addFood(new Onion());
h->addFood(new Onion());
h->addFood(new Tomato());
h->printDetails();
}

#include<iostream>
#include<vector>
using namespace std;

class FoodIngredients{
    int m_price;
public:
    FoodIngredients(int price){m_price=price;}
    int getPrice()const{return m_price;}
    virtual void printDetail()=0;
};

class Bread:public FoodIngredients{
public:
    Bread(int price):FoodIngredients(price){}
    void printDetail(){cout<<"Bread"<<" "<<getPrice()<<endl;}
};
class Wheat_bread:virtual public Bread{
public:
    Wheat_bread(int price=7):Bread(price){}
    void printDetail(){cout<<"Wheat_bread"<<" "<<getPrice()<<endl;}
};
class Honey_break:virtual  public Bread{
public:
    Honey_break(int price=8):Bread(price){}
    void printDetail(){cout<<"Honey_break"<<" "<<getPrice()<<endl;}
};

class Bacon:virtual public FoodIngredients{
public:
    Bacon(int price=8):FoodIngredients(price){}
    void printDetail(){cout<<"Bacon"<<" "<<getPrice()<<endl;}
};
class Beef:virtual public FoodIngredients{
public:
    Beef(int price=12):FoodIngredients(price){}
    void printDetail(){cout<<"Beef"<<" "<<getPrice()<<endl;}
};
class Onion:virtual public FoodIngredients{
public:
    Onion(int price=1):FoodIngredients(price){}
    void printDetail(){cout<<"Onion"<<" "<<getPrice()<<endl;}
};
class Tomoto:virtual public FoodIngredients{
public:
    Tomoto(int price=2):FoodIngredients(price){}
    void printDetail(){cout<<"Tomoto"<<" "<<getPrice()<<endl;}
};
class Lettuce:virtual public FoodIngredients{
public:
    Lettuce(int price=1):FoodIngredients(price){}
    void printDetail(){cout<<"Lettuce"<<" "<<getPrice()<<endl;}
};
class Cheese:virtual public FoodIngredients{
public:
    Cheese(int price=2):FoodIngredients(price){}
    void printDetail(){cout<<"Chess"<<" "<<getPrice()<<endl;}
};
class Mustard:virtual public FoodIngredients{
public:
    Mustard(int price=1):FoodIngredients(price){}
    void printDetail(){cout<<"Mustard"<<" "<<getPrice()<<endl;}
};

class Hotdog{
    Bread *bread;
    vector<FoodIngredients *> v;//虚基类无法有对象
public:
    Hotdog(Bread *b){bread=b;}
    bool addFood(FoodIngredients *f){v.push_back(f);}
    bool setbreak(Bread *b){}

    void printDetails(){
        cout<<"消费表单:"<<endl;
        bread->printDetail();
        for(vector<FoodIngredients *>::iterator it=v.begin();it!=v.end();it++){
            FoodIngredients *f=*it;
            f->printDetail();
        }
    }

     int getTotalPrice(){
        int total=0;
        total+=bread->getPrice();
        for(vector<FoodIngredients *>::iterator it=v.begin();it!=v.end();it++){
            FoodIngredients *f=*it;
            total+=f->getPrice();
        }
        return total;
    }
};

int main(){
    auto h = new Hotdog(new Wheat_bread());
	h->addFood(new Beef());
	h->addFood(new Cheese());
	h->addFood(new Mustard());
	h->addFood(new Onion());
	h->addFood(new Onion());
	h->addFood(new Tomoto());
	h->printDetails();
	cout <<"总计:"<< h->getTotalPrice() << endl;

    return 0;
}

结果:

消费表单:
Wheat_bread 7
Beef 12
Chess 2
Mustard 1
Onion 1
Onion 1
Tomoto 2
总计:26
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值