C++ 多态公有继承

brass.h

  1. #ifndef PRO1_BRASS_H
  2. #define PRO1_BRASS_H
  3. class Brass {
  4. private:
  5. enum {MAX = 35};
  6. char fullName[MAX];
  7. long acctNum;
  8. double balance;
  9. public:
  10. Brass(const char* s = "Nullbody", long an = -1, double bal = 0.0);
  11. void Deposit(double amt);
  12. virtual void Withdraw(double amt);
  13. double Balance()const ;
  14. virtual void ViewAcct()const ;
  15. virtual ~Brass(){};
  16. };
  17. class BrassPlus : public Brass{
  18. private:
  19. double maxLoan;
  20. double rate;
  21. double owesBank;
  22. public:
  23. BrassPlus(const char* s = "Nullbody", long an = -1, double bal = 0.0, double ml = 500, double r = 0.10);
  24. BrassPlus(const Brass & ba, double ml = 500, double r = 0.1);
  25. virtual void ViewAcct()const ;
  26. virtual void Withdraw(double amt);
  27. void ResetMax(double m){ maxLoan = m; }
  28. void ResetRate(double r){ rate = r; }
  29. void ResetOwes(){ owesBank = 0; }
  30. };
  31. #endif //PRO1_BRASS_H

brass.cpp

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

main.cpp

  1. #include <iostream>
  2. #include "module7_polymorphism/brass.h"
  3. using namespace std;
  4. // 多态公有继承
  5. void useBrass(){
  6. Brass Piggy("Procelot Pigg", 381299, 4000.00);
  7. BrassPlus Hoggy("Horatio Hogg", 382288, 3000.00);
  8. Piggy.ViewAcct();
  9. cout << endl;
  10. Hoggy.ViewAcct();
  11. cout << endl;
  12. cout << "Depositing $1000 into the Hogg Account: \n";
  13. Hoggy.Deposit(1000.00);
  14. cout << "New balance: $ " << Hoggy.Balance() << endl;
  15. cout << "Withdrawing $4200 from the Pigg Account:\n";
  16. Piggy.Withdraw(4200.00);
  17. cout << "Pigg account balance: $ " << Piggy.Balance() << endl;
  18. cout << "Withdrawing $4200 from the Hogg Account:\n";
  19. Hoggy.Withdraw(4200.00);
  20. Hoggy.ViewAcct();
  21. }
  22. int main() {
  23. useBrass();
  24. return 0;
  25. }