- #pragma once
- #include
- // 电信运营商
- class ITelco
- {
- public:
- virtual ~ITelco() {}
- virtual void Recharge(int money) = 0; // 充值
- };
- // 中国移动
- class CMCC : public ITelco
- {
- public:
- void Recharge(int money) override {
- std::cout << "Recharge " << money;
- }
- };
- // 代理点
- class Proxy : public ITelco
- {
- public:
- Proxy() : m_pCMCC(NULL) {}
- ~Proxy() { delete m_pCMCC; }
- // 低于 50 不充
- void Recharge(int money) override {
- if (money >= 50) {
- if (m_pCMCC == NULL)
- m_pCMCC = new CMCC();
- m_pCMCC->Recharge(money);
- }
- else {
- std::cout << "Sorry, too little money" << std::endl;
- }
- }
- private:
- CMCC* m_pCMCC;
- };
-
- #include "subject.h"
- #ifndef SAFE_DELETE
- #define SAFE_DELETE(p) { if(p){delete(p); (p)=NULL;} }
- #endif
- int main()
- {
- Proxy* proxy = new Proxy();
- proxy->Recharge(20);
- proxy->Recharge(100);
- SAFE_DELETE(proxy);
- getchar();
- return 0;
- }