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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <iostream>
  2. using namespace std;
  3. //Bank的定义
  4. class Bank{
  5. private:
  6. long ID;
  7. double Balance;
  8. static double Rational; //存款利率
  9. public:
  10. Bank() : ID(10000), Balance(0) {}
  11. Bank(long id, double balance) : ID(id), Balance(balance) {}
  12. void setID(long id) {
  13. ID = id;
  14. }
  15. void setBalance(double d) {
  16. Balance = d;
  17. }
  18. double getBalance() {
  19. return Balance;
  20. }
  21. static void setRational(double r) {
  22. Rational = r / 100.0; // 将百分比转换为小数
  23. }
  24. static double getRational() {
  25. return Rational;
  26. }
  27. void display() {
  28. double interest = Balance * Rational;
  29. cout << "账号:" << ID << "; 存款:" << Balance << "; 利息:" << interest << endl;
  30. }
  31. };
  32. double Bank::Rational = 0;
  33. int main()
  34. {
  35. Bank c;//系统缺省账户
  36. double rate;
  37. cout << "请输入利率:";
  38. cin >> rate;
  39. Bank::setRational(rate);
  40. Bank defaultAccount;
  41. Bank accounts[3];
  42. long id;
  43. double balance;
  44. cout << "请输入3个账号及对应的存款:" << endl;
  45. for (int i = 0; i < 3; ++i) {
  46. cin >> id >> balance;
  47. accounts[i].setID(id);
  48. accounts[i].setBalance(balance);
  49. }
  50. defaultAccount.display();
  51. for (int i = 0; i < 3; ++i) {
  52. accounts[i].display();
  53. }
  54. double totalInterest = defaultAccount.getBalance() * Bank::getRational();
  55. for (int i = 0; i < 3; ++i) {
  56. totalInterest += accounts[i].getBalance() * Bank::getRational();
  57. }
  58. double totalPrincipal = defaultAccount.getBalance();
  59. for (int i = 0; i < 3; ++i) {
  60. totalPrincipal += accounts[i].getBalance();
  61. }
  62. cout << "3个账户的本息:" << totalPrincipal + totalInterest << endl;
  63. return 0;
  64. }