ltw222

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、运行截图

分类:

技术点:

相关文章:

  • 2021-09-22
  • 2021-05-19
  • 2021-10-03
  • 2021-11-29
  • 2021-11-02
  • 2021-12-02
猜你喜欢
  • 2021-04-29
  • 2021-12-02
  • 2021-04-13
  • 2021-08-18
  • 2022-01-12
  • 2021-09-23
  • 2021-08-22
相关资源
相似解决方案