标签:
#include <iostream> using namespace std; class Fruit { public : virtual void getFruit() = 0; }; class Banana :public Fruit { public : virtual void getFruit() { printf("我是香蕉"); } }; class Apple :public Fruit { public: virtual void getFruit() { printf("我是苹果"); } }; class Factory { public: Fruit *create(char *p) { if (strcmp(p, "banana") == 0) { return new Banana; } else if (strcmp(p, "Apple") == 0) { return new Apple; } else { return NULL; } } }; void main() { Factory *fact=new Factory; Fruit *fru; fru=fact->create("banana"); fru->getFruit(); fru = fact->create("Apple"); fru->getFruit(); }
标签:
原文地址:http://www.cnblogs.com/GIScore/p/5322420.html