#pragma once #include "student.h" #include "Teacher.h" typedef enum _EPersonType{ ePersonUndefin = 0, ePersonStudent, ePersonTeacher }EPersonType; class Factory { public: Factory(void); ~Factory(void); Person *CreatPerson(EPersonType personType); }; Person* Factory::CreatPerson(EPersonType personType){ Person *pPerson = 0; switch (personType) { case ePersonStudent: { pPerson = new Student(); break; } case ePersonTeacher: { pPerson = new Teacher(); break; } } return pPerson; } #pragma once #include <iostream> using namespace std; class Person { public: Person(void); virtual ~Person(void); }; #include "Person.h" Person::Person(void) { } Person::~Person(void) { } #pragma once #include "person.h" class Student : public Person { public: Student(void); ~Student(void); }; #include "Student.h" Student::Student(void) { cout<<"Student::Student()"<<endl; } #pragma once #include "person.h" class Teacher : public Person { public: Teacher(void); ~Teacher(void); }; #include "Teacher.h" Teacher::Teacher(void) { cout<<"Teacher::Teacher()"<<endl; } #include "Factory.h" int _tmain(int argc, _TCHAR* argv[]) { Factory factory; factory.CreatPerson(ePersonStudent);//ePersonTeacher return 0; }
原文地址:http://www.cnblogs.com/zhidao-chen/p/3838157.html