C++ 多态公有继承
#ifndef PRO1_BRASS_H#define PRO1_BRASS_Hclass Brass {private: enum {MAX = 35}; char fullName[MAX]; long acctNum; double balance;public: Brass(const char* s = "Nullbody", long an = -1, double bal = 0.0); void Deposit(double amt); virtual void Withdraw(double amt); double Balance()const ; virtual void ViewAcct()const ; virtual ~Brass(){};};class BrassPlus : public Brass{private: double maxLoan; double rate; double owesBank;public: BrassPlus(const char* s = "Nullbody", long an = -1, double bal = 0.0, double ml = 500, double r = 0.10); BrassPlus(const Brass & ba, double ml = 500, double r = 0.1); virtual void ViewAcct()const ; virtual void Withdraw(double amt); void ResetMax(double m){ maxLoan = m; } void ResetRate(double r){ rate = r; } void ResetOwes(){ owesBank = 0; }};#endif //PRO1_BRASS_H
#include "brass.h"#include <iostream>#include <cstring>using namespace std;Brass::Brass(const char *s, long an, double bal) { strncpy(fullName, s, MAX - 1); fullName[MAX - 1] = '\0'; acctNum = an; balance = bal;}void Brass::Deposit(double amt) { if (amt < 0) cout << "Negative deposit not allowed: " << "deposit is cancelled.\n"; else balance += amt;}void Brass::Withdraw(double amt) { if (amt < 0) cout << "Withdrawal amount must be positive: " << "withdrawal canceled.\n"; else if (amt <- balance) balance -= amt; else cout << "Withdrawal amount of $" << amt << " exceeds your balance.\n" << "Withdrawal canceled.\n";}double Brass::Balance() const { return balance;}void Brass::ViewAcct() const { ios_base::fmtflags initialState = cout.setf(ios_base::fixed, ios_base::floatfield); cout.setf(ios_base::showpoint); cout.precision(2); cout << "Client: " << fullName << endl; cout << "Account Number: " << acctNum << endl; cout << "Balance: $" << balance << endl; cout.setf(initialState);}BrassPlus::BrassPlus(const char *s, long an, double bal, double ml, double r):Brass(s, an, bal) { maxLoan = ml; owesBank = 0.0; rate = r;}BrassPlus::BrassPlus(const Brass &ba, double ml, double r):Brass(ba) { maxLoan = ml; owesBank = 0.0; rate = r;}void BrassPlus::ViewAcct() const { ios_base::fmtflags initialState = cout.setf(ios_base::fixed, ios_base::floatfield); cout.setf(ios_base::showpoint); cout.precision(2); Brass::ViewAcct(); cout << "Maximum loan: " << maxLoan << endl; cout << "Owed to bank: " << owesBank << endl; cout << "Loan Rate: " << 100 * rate << "%\n"; cout.setf(initialState);}void BrassPlus::Withdraw(double amt) { ios_base::fmtflags initialState = cout.setf(ios_base::fixed, ios_base::floatfield); cout.setf(ios_base::showpoint); cout.precision(2); double bal = Balance(); if (amt <= bal) Brass::Withdraw(amt); else if (amt <= bal + maxLoan - owesBank){ double advance = amt - bal; owesBank += advance*(1.0 + rate); cout << "Bank advance: $ " << advance << endl; cout << "Finance charge: $ " << advance * rate << endl; Deposit(advance); Brass::Withdraw(amt); } else cout << "Credit limit exceeded. Transaction cancelled.\n"; cout.setf(initialState);}
#include <iostream>#include "module7_polymorphism/brass.h"using namespace std;// 多态公有继承void useBrass(){ Brass Piggy("Procelot Pigg", 381299, 4000.00); BrassPlus Hoggy("Horatio Hogg", 382288, 3000.00); Piggy.ViewAcct(); cout << endl; Hoggy.ViewAcct(); cout << endl; cout << "Depositing $1000 into the Hogg Account: \n"; Hoggy.Deposit(1000.00); cout << "New balance: $ " << Hoggy.Balance() << endl; cout << "Withdrawing $4200 from the Pigg Account:\n"; Piggy.Withdraw(4200.00); cout << "Pigg account balance: $ " << Piggy.Balance() << endl; cout << "Withdrawing $4200 from the Hogg Account:\n"; Hoggy.Withdraw(4200.00); Hoggy.ViewAcct();}int main() { useBrass(); return 0;}