1. 要求如下:
1) 实现描述银行的类Bank,记录系统中现有哪些储户(可用数组或vector实现),定义了生成储户的函数append,按照账户删除储户的函数的delete,按账号查询储户的函数query,并显示结果。
2) 定义储户基类Account,具有属性账号,存款人姓名和余额,操作saving、withdraw和showme。虚函数saving 存储业务,虚函数withdraw处理取款业务,虚showme函数显示储户所有信息。
3) 定义储户派生类普通储户NormalAccount,实现操作saving、withdraw和showme,函数withdraw处理取款业务时余额不足不予取并提示信息,函数showme显示普通储户的所有信息。
4) 定义储户派生类高级储户VIPAccount,包含普通账户的所有信息,同时包含透支上限,透支总额,函数withdraw处理取款业务时超过透支上限不予取并提示信息,函数showme显示高级储户的所有信息。
5) 编写main函数,测试上述所要求的各种功能,即可以根据菜单命令增加,删除,和查询储户,以及储户存款和取款操作:
ž 增加账户时可选择增加普通账户和高级账户,普通账户帐号格式为“N001”,高级账户帐号格式为“V001”;
ž 根据输入的帐号删除和查询账户;
ž 储户取款和存款时要求输入帐号,根据帐号来操作账户。
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 class Account 7 { 8 public: 9 Account(string a,string b,double bal); 10 virtual void saving(double a); 11 virtual void withdrow(double a); 12 virtual void showme(); 13 string getId(); 14 15 16 private: 17 string Account_name; 18 string name; 19 double balance ; 20 21 22 }; 23 24 class NormalAccount:public Account 25 { 26 public: 27 NormalAccount(string a,string b,double bal):Account(a,b,bal) 28 { 29 30 } 31 private: 32 string Account_name; 33 string name; 34 double balance ; 35 }; 36 37 class VIPAccount:public Account 38 { 39 public: 40 VIPAccount(string a,string b,double bal,double d,double e):Account(a,b,bal),tzsx(d),tzze(e) 41 { 42 43 } 44 void withdrow(double a); 45 void showme(); 46 47 48 private: 49 string Account_name; 50 string name; 51 double balance ; 52 double tzsx; 53 double tzze; 54 55 56 }; 57 58 Account::Account(string a,string b,double bal) 59 { 60 Account_name = a; 61 name = b; 62 balance = bal; 63 } 64 65 void Account::saving(double a) 66 { 67 balance = balance + a; 68 } 69 70 void Account::withdrow(double a) 71 { 72 if(a > balance) 73 cout<<"余额不足"<<endl; 74 else 75 { 76 balance = balance - a; 77 cout << "已取出" << a << "元" <<endl; 78 } 79 } 80 81 void Account::showme() 82 { 83 cout << "用户账号为" << Account_name << endl; 84 cout << "开户人姓名" << name <<endl; 85 cout << "账户余额为" << balance <<endl; 86 } 87 88 string Account::getId() 89 { 90 return Account_name; 91 } 92 93 void VIPAccount::withdrow(double a) 94 { 95 if(a >balance + tzsx -tzze) 96 cout<<"不可透支"<<endl; 97 else 98 balance = balance - a; 99 100 } 101 102 void VIPAccount::showme() 103 { 104 cout << "用户账号为" << Account_name << endl; 105 cout << "开户人姓名" << name <<endl; 106 cout << "账户余额为" << balance <<endl; 107 cout << "透支上限为" << tzsx <<endl; 108 cout << "透支总额为" << tzze <<endl; 109 110 } 111 112 class Bank 113 { 114 public: 115 Bank(); 116 void append1(); 117 void append2(); 118 void del(); 119 void query(); 120 Account *account[100]; 121 private: 122 123 int accNum; 124 double Balance; 125 126 }; 127 128 Bank::Bank() 129 { 130 for(int i = 0;i < 100 ; i++ ) 131 { 132 account[i] = NULL; 133 } 134 135 accNum = 0; 136 } 137 138 void Bank::append1() 139 { 140 string str1,str2; 141 142 cout << "请输入普通用户账号" << endl; 143 cin>>str1; 144 cout << "请输入开户人姓名" << endl; 145 cin>>str2; 146 Account *acc = new NormalAccount(str1,str2,0) ; 147 cout<<"增加普通账户成功"<<endl; 148 149 account[accNum] = acc; 150 accNum++; 151 } 152 153 void Bank::append2() 154 { 155 string str1,str2; 156 157 cout << "请输入高级用户账号" << endl; 158 cin>>str1; 159 cout << "请输入开户人姓名" << endl; 160 cin>>str2; 161 Account *acc = new VIPAccount(str1,str2,0,5000,0) ; 162 cout<<"增加高级账户成功"<<endl; 163 164 account[accNum] = acc; 165 accNum++; 166 } 167 168 void Bank::del() 169 { 170 string n; 171 cout << "请输入要注销的用户账号" << endl; 172 cin>>n; 173 174 for(int i=0;i<100;i++) 175 { 176 if(account[i]->getId() != n) 177 cout<<"没有这个账号"<<endl; 178 if(account[i]->getId() == n) 179 { 180 delete account[i]; 181 accNum--; 182 183 } 184 185 } 186 } 187 188 void Bank::query() 189 { 190 string n; 191 cout << "请输入您要查询的用户账号" << endl; 192 cin>>n; 193 194 for(int i=0;i<100;i++) 195 { 196 if(account[i]->getId() != n) 197 cout<<"没有这个账号"<<endl; 198 if(account[i]->getId() == n) 199 account[i]->showme(); 200 } 201 202 } 203 204 int main() 205 { 206 Bank bank; 207 208 while(1) 209 { 210 cout << "1.增加账户" << endl; 211 cout << "2.删除账户" <<endl; 212 cout << "3.查询账户" <<endl; 213 cout << "4.取款和存款" <<endl; 214 cout << "5.退出系统" <<endl; 215 216 cout << "请选择" <<endl; 217 218 int n; 219 220 cin>>n; 221 222 if(n == 1) 223 { 224 int n; 225 cout << "1.增加普通账户" << endl; 226 cout << "2.增加高级账户" << endl; 227 cout << "请选择" << endl; 228 cin>>n; 229 230 if(n == 1) 231 bank.append1(); 232 233 if(n == 2) 234 bank.append2(); 235 236 } 237 238 if (n == 2) 239 { 240 bank.del(); 241 } 242 243 if (n == 3) 244 { 245 bank.query(); 246 } 247 248 if (n == 4) 249 { 250 251 string n; 252 cout<<"请输入您要存取款的账号"<<endl; 253 cin>>n; 254 for(int i=0;i<100;i++) 255 { 256 if(bank.account[i]->getId() != n) 257 cout<<"账号输入错误"<<endl; 258 if(bank.account[i]->getId() == n) 259 { 260 int choice; 261 262 cout<<"1.取款"<<endl; 263 cout<<"2.存款"<<endl; 264 cout<<"请选择"<<endl; 265 266 cin>>choice; 267 if(choice == 1) 268 { 269 double jine; 270 cout<<"请输入取款金额"<<endl; 271 cin >> jine; 272 bank.account[i]->withdrow(jine); 273 bank.account[i]->showme(); 274 275 } 276 if(choice == 2) 277 { 278 double qkuan; 279 cout<<"请输入存款金额"<<endl; 280 cin>>qkuan; 281 bank.account[i]->saving(qkuan); 282 bank.account[i]->showme(); 283 } 284 } 285 286 } 287 288 289 } 290 291 if (n == 5) 292 return 0; 293 } 294 295 //return 0; 296 }