在一定的范围内,支票透支后“好心”的银行也会让你取出来,但之后吗……利息是少不了的
:(
#pragma once #include <iostream> #include <string> using namespace std;class Brass { private : string fullName; long acctNum; double balance; public : Brass (const string& 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 string& s = "Nullbody" , long an = -1 , double bal = 0.0 , double m1 = 500 , double r = 0.011125 ); BrassPlus (const Brass& ba, double m1 = 500 , double r = 0.11125 ); virtual void ViewAcct () const ; virtual void Withdraw (double amt) ; void ResertMax (double m) { maxLoan = m; } void ResertRate (double r) { rate = r; } void ResertOwes () { owesBank = 0 ; } };
#include "Brass.h" typedef ios_base::fmtflags format;typedef streamsize precis;format setFormat () ;void restore (format f, precis p) ;BrassPlus::BrassPlus (const string& s, long an, double bal, double m1, double r) { maxLoan = m1; owesBank = 0.0 ; rate = r; } BrassPlus::BrassPlus (const Brass& ba, double m1, double r) { maxLoan = m1; owesBank = 0.0 ; rate = r; } void BrassPlus::ViewAcct () const { format initialstate = setFormat (); precis prec = cout.precision (2 ); Brass::ViewAcct (); cout << "Maxium load: $" << maxLoan << endl; cout << "Owed to bank: $" << owesBank << endl; cout.precision (3 ); cout << "Loan Rate: " << 100 * rate << "%\n" ; restore (initialstate, prec); } void BrassPlus::Withdraw (double amt) { format initialstate = setFormat (); precis prec = 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); } else cout << "Credit limit exceeded. Transaction cancelled.\n" ; restore (initialstate, prec); } Brass::Brass (const string& s, long an, double bal) { fullName = s; 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) { format initialstate = setFormat (); precis prec = cout.precision (2 ); } double Brass::Balance () const { return balance; } void Brass::ViewAcct () const { format initialstate = setFormat (); precis prec = cout.precision (2 ); cout << "Client: " << fullName << endl; cout << "Account Number: " << acctNum << endl; cout << "Balance: $" << balance << endl; restore (initialstate, prec); } Brass::~Brass () { } format setFormat () { return cout.setf (ios_base::fixed, ios_base::floatfield); } void restore (format f, precis p) { cout.setf (f, ios_base::floatfield); cout.precision (p); }
#include "Brass.h" int main () { Brass Piggy ("porcelot pigg" , 381299 , 4000.00 ) ; BrassPlus Hoggy ("Horation 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; Piggy.Withdraw (4200.00 ); cout << "Piggy account balance: $" << Piggy.Balance () << endl; cout << "Withdrawing $4200 from the Hogg Account:\n" ; Hoggy.Withdraw (4200.00 ); Hoggy.ViewAcct (); system ("pause" ); return 0 ; }
结果:
Client:porcelot pigg Account Number:381299 Balance: $4000.00 Client:Nullbody Account Number:-1 Balance: $0.00 Maxium load: $500.00 Owed to bank: $0.00 Loan Rate: 1.113 % Depositing $1000 into the hogg Account: Nem balance: $1000 Pigg account balance:$4000.00 Withdrawing $4200 from the Hogg Account: Credit limit exceeded.Transaction cancelled. Client:Nullbody Account Number:-1 Balance: $1000.00 Maxium load: $500.00 Owed to bank: $0.00 Loan Rate: 1.113 %
特别鸣谢Github Copilot生成的注释)