首页 > 代码库 > Windows编程之MDI
Windows编程之MDI
Create All kinds of guns.
设计重点:
1 基类作接口
2 继承类是各种不同的类
3 构建工厂类,然后根据需要创造不同的类,可以传入关键字,或者索引等。
#pragma once #include <string> #include <iostream> using namespace std; //Base class class Gun { public: virtual string description() { return "Generic Gun"; } }; //Derived class class MachineGun :public Gun { public: string description() { return "MachineGun - Gun for fast shoot"; } }; class LaserGun : public Gun { public: string description() { return "LaserGun - Melt everything with heat"; } }; class ShockwaveGun :public Gun { public: string description() { return "ShockwaveGun - shock down everything"; } }; class GofFactory_Gun { public: GofFactory_Gun() { } Gun *createGun(const string &type) { if ("MachineGun" == type) { return (new MachineGun); } else if ("LaserGun" == type) { return (new LaserGun); } else if ("ShockwaveGun" == type) { return (new ShockwaveGun); } else { return NULL; } } }; void GofFactory_Gun_Run() { GofFactory_Gun gofGun; Gun *g = gofGun.createGun("MachineGun"); cout<<g->description()<<endl; delete g; g = gofGun.createGun("LaserGun"); cout<<g->description()<<endl; delete g; g = gofGun.createGun("ShockwaveGun"); cout<<g->description()<<endl; delete g; }
结果:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。