zoukankan      html  css  js  c++  java
  • 简单工厂模式(C++)

    C++源代码:

    #include<iostream>
    #include<vector>
    using namespace std;
    
    typedef enum PersonTypeTag
    {
    	M,
    	W,
    	R
    }PersonType;
    
    class Person
    {
    public:
    	virtual void make() = 0;
    };
    
    class Man : public Person
    {
    public:
    	void make()
    	{
    		cout << "生产男人" << endl;
    	}
    };
    
    class Woman : public Person
    {
    public:
    	void make()
    	{
    		cout << "生产女人" << endl;
    	}
    };
    
    class Robot : public Person
    {
    public:
    	void make()
    	{
    		cout << "生产机器人" << endl;
    	}
    };
    
    class Nvwa
    {
    public:
    	Person*Personjudge(PersonType type) {
    		switch (type) {
    		case M:
    			return new Man();
    		case W:
    			return new Woman();
    		case R:
    			return new Robot();
    		default:
    			return NULL;
    		}
    	}
    };
    
    int main(int argc, char *argv[])
    {
    	Nvwa *nvwa = new Nvwa();
    	char t = 0;
    
    	Person * man = nvwa->Personjudge(M);
    	Person * woman = nvwa->Personjudge(W);
    	Person * robot = nvwa->Personjudge(R);
    
    	while (t != -1) {
    		cout << "请输入标识符:";
    		cin >> t;
    		if (t == 'M')
    			man->make();
    		else if (t == 'W')
    			woman->make();
    		else if (t == 'R')
    			robot->make();
    		else {
    			cout << "输入的标识符有误,请重新输入!" << endl;
    			cout << "请输入标识符:";
    			cin >> t;
    		}
    		cout << endl;
    	}
    	delete nvwa;
    	nvwa = NULL;
    
    	delete man;
    	man = NULL;
    
    	delete woman;
    	woman = NULL;
    
    	delete robot;
    	robot = NULL;
    
    	return 0;
    }
    

      实现截图:

  • 相关阅读:
    艰苦的RAW格式数据恢复之旅
    smarty小技巧之6个标签
    smarty制作留言板的学习和思考
    ul 下的li 添加背景
    php.ini 配置详细选项
    fck与smarty的结合使用
    excel VBA 公式计算结果引用问题
    excel 统计字数公式
    bcp命令详解转载
    CTRL+C和CTRL+V不能使用的解决办法
  • 原文地址:https://www.cnblogs.com/marr/p/15403132.html
Copyright © 2011-2022 走看看