1 #include<iostream> 2 #include<string> 3 using namespace std; 4 class Bank 5 { 6 public: 7 Bank(string _name) 8 { 9 name = _name; 10 } 11 string getName() const 12 { 13 return name; 14 } 15 virtual void printMessage() const = 0; 16 virtual void deposit(double m) = 0; 17 virtual void withdraw(double m) = 0; 18 virtual double getMoney() const = 0; 19 protected: 20 string name; 21 }; 22 23 24 25 class ICBC : public Bank 26 { 27 public: 28 ICBC(double _money = 0):Bank("ICBC") 29 { 30 money = _money; 31 } 32 void printMessage() const 33 { 34 cout << "Welcome to Industrial and Commercial Bank of China" << endl; 35 } 36 void deposit(double m) 37 { 38 money += m; 39 } 40 void withdraw(double m) 41 { 42 if (money > m) money -= m; 43 else money = 0; 44 } 45 double getMoney() const 46 { 47 return money; 48 } 49 private: 50 double money; 51 }; 52 53 54 class ABC : public Bank 55 { 56 public: 57 ABC(double _money = 0):Bank("ABC") 58 { 59 money = _money; 60 } 61 void printMessage() const 62 { 63 cout << "Welcome to Agricultural Bank of China" << endl; 64 } 65 void deposit(double m) 66 { 67 money += m; 68 } 69 void withdraw(double m) 70 { 71 if (money > m) money -= m; 72 else money = 0; 73 } 74 double getMoney() const 75 { 76 return money; 77 } 78 private: 79 double money; 80 }; 81 82 83 84 class AccountManager 85 { 86 public: 87 AccountManager(int _size); 88 ~AccountManager(); 89 void setBank(int i, Bank *bank); 90 Bank* getBank(int i); 91 void deposit(Bank *bank, double money); 92 void withdraw(Bank *bank, double money); 93 void transfer(Bank *bank1, Bank *bank2, double money); 94 void printAccount() const; //print the welcome message, bank name and money for each array element 95 private: 96 Bank* *bankList; //bank array, where each element is a (Bank*) type 97 int size; //the length of the bank array 98 }; 99 100 101 AccountManager::AccountManager(int _size){ 102 size = _size; 103 bankList = new Bank*[size]; 104 } 105 AccountManager::~AccountManager(){ 106 delete [] bankList; 107 } 108 void AccountManager::setBank(int i, Bank *bank){ 109 bankList[i] = bank; 110 } 111 Bank* AccountManager::getBank(int i){ 112 return bankList[i]; 113 } 114 void AccountManager::deposit(Bank *bank, double money){ 115 bank->deposit(money); 116 } 117 void AccountManager::withdraw(Bank *bank, double money){ 118 bank->withdraw(money); 119 } 120 void AccountManager::transfer(Bank *bank1, Bank *bank2, double money){ 121 if(bank1->getMoney()>=money){ 122 bank1->withdraw(money); 123 bank2->deposit(money); 124 } 125 else{ 126 bank2->deposit(bank1->getMoney()); 127 bank1->withdraw(bank1->getMoney()); 128 } 129 130 } 131 void AccountManager::printAccount() const{ 132 for(int i=0;i<size;i++){ 133 bankList[i]->printMessage(); 134 cout<<bankList[i]->getName()<<" "; 135 cout<<bankList[i]->getMoney()<<endl; 136 } 137 } 138 139 140 141 142 143 144 145 146 147 148 149 int main() 150 { 151 AccountManager accountManager(2); 152 153 Bank *bank1 = new ICBC (100); 154 accountManager.setBank(0, bank1); 155 Bank *bank2 = new ABC; 156 accountManager.setBank(1, bank2); 157 158 accountManager.printAccount(); 159 160 accountManager.deposit(accountManager.getBank(0), 100); 161 accountManager.deposit(accountManager.getBank(1), 100); 162 accountManager.printAccount(); 163 164 accountManager.withdraw(accountManager.getBank(0), 50); 165 accountManager.withdraw(accountManager.getBank(1), 50); 166 accountManager.printAccount(); 167 168 accountManager.transfer(accountManager.getBank(0), accountManager.getBank(1), 500); 169 accountManager.printAccount(); 170 171 return 0; 172 } 173 174 175 176 177 178 179 180 181 182 183 184 185 186
本知识属于个人知识成果,如需转载,请邮件请求