C++代码
#pragma once #include <string> using std::string; class Beverage { public: Beverage() { } virtual ~Beverage() { } virtual string getDescription() const { return description; } virtual double cost() const = 0;//纯虚函数可以有函数体 protected: string description = "Unknown Beverage"; }; //-------------------------------------- #pragma once #include "Beverage.h" class DarkRoast : public Beverage { public: DarkRoast() { this->description = "Dark Roast Coffee"; } virtual ~DarkRoast() { } virtual double cost() const { return 0.99; } }; #pragma once #include "Beverage.h" class Condiment : public Beverage { public: Condiment() { } virtual ~Condiment() { } virtual string getDescription() const = 0; //纯虚函数可以有函数体 }; //-------------------------------------- #pragma once #include "Beverage.h" class Espresso : public Beverage { public: Espresso() { this->description = "Espresso Coffee"; } virtual ~Espresso() { } virtual double cost() const { return 1.99; } }; //-------------------------------------- #pragma once #include "Beverage.h" class HouseBlend : public Beverage { public: HouseBlend() { this->description = "House Blend Coffee"; } virtual ~HouseBlend() { } virtual double cost() const { return 0.89; } }; //-------------------------------------- #pragma once #include "Condiment.h" class Mocha : public Condiment { private: Beverage* beverage; public: Mocha(Beverage* beverage) { this->beverage = beverage; } virtual ~Mocha() { } virtual string getDescription() const { return beverage->getDescription() + ",Mocha"; } virtual double cost() const { return 0.20 + beverage->cost(); } private: Mocha(const Mocha& m){ } }; //-------------------------------------- #pragma once #include "Condiment.h" //奶泡 class Whip : public Condiment { private: Beverage* beverage; public: Whip(Beverage* beverage) { this->beverage = beverage; } virtual ~Whip() { } virtual string getDescription() const { return beverage->getDescription() + ",Whip"; } virtual double cost() const { return 0.10 + beverage->cost(); } }; //-------------------------------------- #pragma once #include "Condiment.h" class Soy : public Condiment { private: Beverage* beverage; public: Soy(Beverage* beverage) { this->beverage = beverage; } virtual ~Soy() { } virtual string getDescription() const { return beverage->getDescription() + ",Soy"; } virtual double cost() const { return 0.15 + beverage->cost(); } }; //-------------------------------------- #pragma once #include "Condiment.h" class Milk : public Condiment { private: Beverage* beverage; public: Milk(Beverage* beverage) { this->beverage = beverage; } virtual ~Milk() { } virtual string getDescription() const { return beverage->getDescription() + ",Milk"; } virtual double cost() const { return 0.10 + beverage->cost(); } }; //-------------------------------------- #include <iostream> #include "Condiment.h" #include "DarkRoast.h" #include "HouseBlend.h" #include "Espresso.h" #include "Mocha.h" #include "Whip.h" #include "Soy.h" using namespace std; void display(const Beverage& b){ cout <<"name:" << b.getDescription() << "\ncost:" << b.cost() << endl; } void display2(const Beverage* b){ cout << "name:" << b->getDescription() << "\ncost:" << b->cost() << endl; } int main(){ //浓缩咖啡 Espresso e; display(e); //深烘焙 + 2份摩卡 + 1份奶泡 DarkRoast dr; Mocha mo(&dr); Mocha mo2(&mo); Whip w(&mo2); display(w); //混合 + 豆浆 + 摩卡 + 奶泡 HouseBlend hb; Soy t1(&hb); Mocha t2(&t1); Whip t3(&t2); Beverage* b = &t3; display2(b); } /* name:Espresso Coffee cost:1.99 name:Dark Roast Coffee,Mocha,Mocha,Whip cost:1.49 name:House Blend Coffee,Soy,Mocha,Whip cost:1.34 */ //--------------------------------------View Code
--------------------
标签:HeadFirst,include,const,virtual,public,Beverage,设计模式,装饰,beverage From: https://www.cnblogs.com/htj10/p/16874835.html