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