zoukankan      html  css  js  c++  java
  • 软件设计——简单工厂模式之女娲造人C++

    1、类图

    2、源程序代码

    #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;
    }
    
    

    3、运行截图

  • 相关阅读:
    SVN 使用学习记录
    jQuery 获取 URL信息
    JS扩展方法
    .Net 加密 哈希
    SQL Serverf 索引
    SQL Server索引
    SQL Server索引
    insert into select 多个表
    Mysql数据库自带四个数据库的解析
    转: MySQL5.7 ERROR 1142 (42000)问题
  • 原文地址:https://www.cnblogs.com/ltw222/p/15333388.html
Copyright © 2011-2022 走看看