定义并实现一个银行类(Bank).cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <iostream>
  2. using namespace std;
  3. class Bank {
  4. private:
  5. long ID;
  6. double Balance;
  7. static double Rational;
  8. public:
  9. Bank() : ID(10000), Balance(0) {} // 默认构造函数
  10. Bank(long id, double balance) : ID(id), Balance(balance) {} // 带参数构造函数
  11. void setID(long id) {
  12. ID = id;
  13. }
  14. void setBalance(double d) {
  15. Balance = d;
  16. }
  17. double getBalance() {
  18. return Balance;
  19. }
  20. static void setRational(double r) {
  21. Rational = r;
  22. }
  23. static double getRational() {
  24. return Rational;
  25. }
  26. void display() {
  27. cout << "账号:" << ID << "; 存款:" << Balance << "; 本息:" << Balance * ( Rational / 100) << endl;
  28. }
  29. };
  30. double Bank::Rational = 0; // 初始化静态数据成员
  31. int main() {
  32. double rate;
  33. cout << "请输入利率:";
  34. cin >> rate;
  35. Bank::setRational(rate); // 设置利率
  36. long id;
  37. double balance;
  38. cout << "请输入3个账号及对应的存款:" << endl;
  39. c.display(); // 输出系统缺省账户信息
  40. double totalInterest = 0;
  41. for (int i = 0; i < 3; ++i) {
  42. cin >> id >> balance;
  43. Bank account(id, balance);
  44. account.display(); // 输出新建账户信息
  45. totalInterest += balance * (1 + Bank::getRational() / 100);
  46. }
  47. cout << "3个账户的本息:" << totalInterest << endl;
  48. return 0;
  49. }