#include <iostream> #include <vector> using namespace std; class relatives { public: relatives(){} virtual ~relatives(){} virtual bool request(int)=0; }; class brother : public relatives { public: brother(){} virtual ~brother(){} bool request(int num) { if (num<100) { cout<<"哥哥给你买"<<endl; return true; } else { cout<<"哥哥不给买,让妈妈买吧"<<endl; return false; } } }; class mother : public relatives { public: mother(){} virtual ~mother(){} bool request(int num) { if (num<500) { cout<<"妈妈给你买"<<endl; return true; } else { cout<<"妈妈不给买,让爸爸买吧"<<endl; return false; } } }; class father : public relatives { public: father(){} virtual ~father(){} bool request(int num) { if (num<1000) { cout<<"爸爸给你买"<<endl; return true; } else { cout<<"爸爸不给买,...."<<endl; return false; } } }; class buysomething { private: vector<relatives*> p_vbuy; public: buysomething() { p_vbuy.push_back(new brother); p_vbuy.push_back(new mother); p_vbuy.push_back(new father); } virtual ~buysomething() { p_vbuy.clear(); } void request(int num) { bool flag=false; for (vector<relatives*>::iterator it=p_vbuy.begin();it!=p_vbuy.end();it++) { flag=(*it)->request(num); if (flag==true) break; } } }; int main() { buysomething *buy=new buysomething; buy->request(6000); delete buy; system("pause"); return 0; }
转载于:https://www.cnblogs.com/tiandsp/archive/2012/06/27/2565629.html