C++ 抽象类

acctabc.h

  1. #ifndef PRO1_ACCTABC_H
  2. #define PRO1_ACCTABC_H
  3. #include <iostream>
  4. #include <cstring>
  5. using namespace std;
  6. // Abstract Base Class
  7. class AcctABC {
  8. private:
  9. enum {MAX = 35};
  10. char fullName[MAX];
  11. long acctNum;
  12. double balance;
  13. protected:
  14. const char * FullName()const { return fullName; }
  15. long AcctNum()const { return acctNum; }
  16. ios_base::fmtflags SetFormat()const ;
  17. public:
  18. AcctABC(const char *s = "Nullbody", long an = -1, double bal = 0.0);
  19. void Deposit(double amt);
  20. virtual void Withdraw(double amt) = 0;
  21. double Balance()const { return balance; };
  22. virtual void ViewAcct()const = 0;
  23. virtual ~AcctABC(){};
  24. };
  25. class Brass: public AcctABC{
  26. public:
  27. Brass(const char*s = "Nullbody", long an = -1, double bal = 0.0);
  28. virtual void Withdraw(double amt);
  29. virtual void ViewAcct()const ;
  30. virtual ~Brass();
  31. };
  32. class BrassPlus: public AcctABC{
  33. private:
  34. double maxLoan;
  35. double rate;
  36. double owesBank;
  37. public:
  38. BrassPlus(const char *s = "Nullbody", long an = -1, double bal = 0.0, double ml = 500, double r = 0.10);
  39. BrassPlus(const Brass & ba, double ml = 500, double r = 0.1);
  40. virtual void ViewAcct()const ;
  41. virtual void Withdraw(double amt);
  42. void ResetMax(double m){ maxLoan = m; }
  43. void ResetRate(double r){ rate = r; }
  44. void ResetOwes(){ owesBank = 0; }
  45. };
  46. #endif //PRO1_ACCTABC_H

acctabc.cpp

  1. #include "acctabc.h"
  2. #include <iostream>
  3. #include <cstring>
  4. using namespace std;
  5. AcctABC::AcctABC(const char *s, long an, double bal) {
  6. strncpy(fullName, s, MAX - 1);
  7. fullName[MAX - 1] = '\0';
  8. acctNum = an;
  9. balance = bal;
  10. }
  11. void AcctABC::Deposit(double amt) {
  12. if (amt < 0)
  13. cout << "Negative deposit not allowed: " << "deposit is cancelled.\n";
  14. else
  15. balance -=amt;
  16. }
  17. void AcctABC::Withdraw(double amt) {
  18. balance -= amt;
  19. }
  20. ios_base::fmtflags AcctABC::SetFormat() const {
  21. ios_base::fmtflags initialState = cout.setf(ios_base::fixed, ios_base::floatfield);
  22. cout.setf(ios_base::showpoint);
  23. cout.precision(2);
  24. return initialState;
  25. }
  26. void Brass::Withdraw(double amt) {
  27. if (amt < 0)
  28. cout << "Withdrawal; amount must be positive;" << "withdrawal canceled.\n";
  29. else if (amt <= Balance())
  30. AcctABC::Withdraw(amt);
  31. else
  32. cout << "Withdrawal amount of $" << amt << " exceeds your balance.\n" << "Withdrawal canceled.\n";
  33. }
  34. void Brass::ViewAcct() const {
  35. ios_base::fmtflags initialState = SetFormat();
  36. cout << "Brass Client: " << FullName() << endl;
  37. cout << "Account Number: " << AcctNum() << endl;
  38. cout << "Balance: $ " << Balance() << endl;
  39. cout.setf(initialState);
  40. }
  41. BrassPlus::BrassPlus(const char *s, long an, double bal, double ml, double r): AcctABC(s, an, bal) {
  42. maxLoan = ml;
  43. owesBank = 0.0;
  44. rate = r;
  45. }
  46. BrassPlus::BrassPlus(const Brass &ba, double ml, double r):AcctABC(ba) {
  47. maxLoan = ml;
  48. owesBank = 0.0;
  49. rate = r;
  50. }
  51. void BrassPlus::ViewAcct() const {
  52. ios_base::fmtflags initialState = SetFormat();
  53. cout << "BrassPlus Client: " << FullName() << endl;
  54. cout << "Account Number: " << AcctNum() << endl;
  55. cout << "Balance: $ " << Balance() << endl;
  56. cout << "Maximum loan: $ " << maxLoan << endl;
  57. cout << "Owed to Bank: $ " << owesBank << endl;
  58. cout << "Loan Rate: " << 100 * rate << endl;
  59. cout.setf(initialState);
  60. }
  61. void BrassPlus::Withdraw(double amt) {
  62. ios_base::fmtflags initialState = SetFormat();
  63. double bal = Balance();
  64. if (amt <= bal)
  65. AcctABC::Withdraw(amt);
  66. else if (amt <= bal + maxLoan - owesBank){
  67. double advance = amt - bal;
  68. owesBank += advance * (1.0 + rate);
  69. cout << "Bank advance: $ " << advance << endl;
  70. cout << "Finance charge: $ " << advance * rate << endl;
  71. Deposit(advance);
  72. AcctABC::Withdraw(amt);
  73. } else
  74. cout << "Credit limit exceeded. Transaction cancelled.\n";
  75. cout.setf(initialState);
  76. }