1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| #include <iostream> #include <string>
using namespace std;
#define male 0 #define female 1
class Mediator;
class Person { public: Person(string name, int sex, int condition, Mediator* mediator) { m_name = name; m_sex = sex; m_condition = condition; m_mediator = mediator; } string getName() { return m_name; } int getSex() { return m_sex; } int getCondition() { return m_condition; } virtual void getParter(Person* p) = 0; protected: string m_name; int m_sex; int m_condition; Mediator* m_mediator; private: };
class Mediator { public: void getParter() { if(m_pMan->getSex() == m_pWoman->getSex()) { cout << "不是同性恋!" << endl; } if(m_pMan->getCondition() == m_pWoman->getCondition()) { cout << m_pMan->getName() << "和" << m_pWoman->getName() << "匹配" << endl; } else { cout << m_pMan->getName() << "和" << m_pWoman->getName() << "不匹配" << endl; } } void setMan(Person* man) { m_pMan = man; } void setWoman(Person* woman) { m_pWoman = woman; } private: Person* m_pMan; Person* m_pWoman; };
class Woman : public Person { public: Woman(string name, int sex, int condition, Mediator* mediator):Person(name, sex, condition, mediator) {
} virtual void getParter(Person* p) {
m_mediator->setMan(p); m_mediator->setWoman(this); m_mediator->getParter(); } protected: private: };
class Man : public Person { public: Man(string name, int sex, int condition, Mediator* mediator):Person(name, sex, condition, mediator) {
} virtual void getParter(Person* p) {
m_mediator->setMan(this); m_mediator->setWoman(p); m_mediator->getParter(); } protected: private: };
int main() { Mediator* mediator; Person* xiaofang = new Woman("小芳", female, 5, mediator); Person* zhangsan = new Man("张三", male, 3, mediator); Person* lisi = new Man("李四", male, 5, mediator);
xiaofang->getParter(zhangsan); xiaofang->getParter(lisi);
delete xiaofang; delete zhangsan; delete lisi;
cout << "Hello world!" << endl; return 0; }
|