C++ STL 第五次实验

来源:互联网 发布:java数字转化为字符串 编辑:程序博客网 时间:2024/06/06 03:19
//各种头文件#include <iostream>#include <functional>#include <algorithm>#include <vector>#include <string.h>using namespace std;class FoodIngredients{    int price;public:    FoodIngredients(int price):price(price){}    int getPrice(){return this->price;}    //纯虚函数    virtual void printDetail()=0;};class Beef:public FoodIngredients{public:    Beef(int price = 12):FoodIngredients(price){}    void printDetail(){cout << "Beef:" << "12 " << endl;}};class Cheese:public FoodIngredients{public:    Cheese(int price = 2):FoodIngredients(price){}    void printDetail(){cout << "Cheese:" << "2 " << endl;}};class Mustard:public FoodIngredients{public:    Mustard(int price = 1):FoodIngredients(price){}    void printDetail(){cout << "Mustard: " << "1 " << endl;}};class Onion:public FoodIngredients{public:    Onion(int price = 1):FoodIngredients(price){}    void printDetail(){cout << "Onion: " << "1 " << endl;}};class Tomoto:public FoodIngredients{public:    Tomoto(int price = 2):FoodIngredients(price){}    void printDetail(){cout << "Tomoto: " << "2 " << endl;}};class Bread:public FoodIngredients{public:    Bread(int price):FoodIngredients(price){}    void printDetail(){cout << "Bread: " << this->getPrice() << endl;}};class Wheat_bread:public Bread{public:    Wheat_bread(int price = 7):Bread(price){}    void printDetail(){cout << "Wheat_bread: " << "7 " << endl;}};class Honey_bread:public Bread{public:    Honey_bread(int price = 8):Bread(price){}    void printDetail(){cout << "Honey_bread: " << "8 " << endl;}};static int sum = 0;class Hotdog{private:    vector<FoodIngredients *> v;    Bread *m_b;public:    Hotdog(Bread *b):m_b(b){}    int GetTotalPrice(){        for(int i=0;i<v.size();i++){            sum += v[i]->getPrice();        }        return sum;    }    void printDetails(){        for_each(v.begin(),v.end(),mem_fun(&FoodIngredients::printDetail));    }    bool addFood(FoodIngredients *pFoodIngredients){        v.push_back(pFoodIngredients);        return true;    }};int main() {     Hotdog *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();}
原创粉丝点击