标签:
特点:
// Decorator.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <STRING> using namespace std; class Cake{ public: Cake():name("cake"){} virtual ~Cake(){} virtual string getname(){ return name; } public: string name; }; class MikeCake:public Cake{ public: MikeCake(){ this->name = "Milkcake"; } MikeCake(string str){ this->name = str; } }; class Decorate:public Cake{ public: Decorate(Cake * p):pcake(p){} virtual ~Decorate(){ }; protected: Cake *pcake; }; class FlowerDecorate:public Decorate{ public: FlowerDecorate(Cake * p):Decorate(p){ name = (string("flowered --> ")+=p->getname()); } }; class FruitDecorate:public Decorate{ public: FruitDecorate(Cake * p):Decorate(p){ name = (string("fruited --> ")+=p->getname()); } }; int main(int argc, char* argv[]) { Cake * pcake = new MikeCake("cake1"); cout<<pcake->getname()<<endl; cout<<FlowerDecorate(pcake).getname()<<endl; cout<<FruitDecorate(&FlowerDecorate(pcake)).getname()<<endl; delete pcake; return 0; }
标签:
原文地址:http://www.cnblogs.com/xiumukediao/p/4639434.html