brass.h #ifndef _BRASS_H_ #define _BRASS_H_ class Brass { private: enum {MAX = 35}; char fullName[MAX]; // 客户姓名 long accNum; //账号 double balance; //当前结余 public: Brass (const char * s = "Nullbody", long an = -1, double bal = 0.0 ); // 构造 void Depost(double amt); // 存款 virtual void WithDraw(double amt); // 取款 double Balance() const; // 返回balance 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 brass.cpp #include <iostream> #include "brass.h" #include <cstring> using std::cout; using std::ios_base; using std::endl; Brass::Brass(const char * s, long an, double bal) { strncpy_s(fullName, s, MAX-1); fullName[ MAX - 1 ] = '/0'; accNum = an; balance = bal; } void Brass::Depost(double amt) // 存款 { if (amt < 0) { cout << "负数的存款是不允许的:" << "存款取消!" << endl; } else { balance += amt; } } void Brass::WithDraw(double amt) // 取款 { if ( amt < 0 ) { cout << "取款的数额不能为负:" << "取款取消!" << endl; } else if( amt <= balance ) { balance -= amt; } else { cout << "你想取" << amt << "块?/n但是你的卡里没有这么多钱!取款取消!" << endl; } } 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 << "用户名:" << fullName << endl; cout << "帐号:" << accNum << endl; cout << "金额:" << 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 << "最大借款额:" << maxLoan << endl; cout << "欠款:" << owesBank << endl; cout << "欠款利息:" << 100 * rate << "%"<< endl; 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 << "银行预支额:" << advance << endl; cout << "预支利息:" << advance * rate << endl; Depost(advance);// 银行把不够的钱存进你的卡里 Brass::WithDraw(amt);// 让你取出来 } else { cout << "你取的钱已经超出了最大预支额!" << endl; } cout.setf(initialState); } useBrass1.cpp #include "brass.h" #include <iostream> int main() { using std::cout; using std::endl; Brass kelso("kelso zhao", 198827, 4000.00); BrassPlus susan("susan su", 19871103, 3000.00); kelso.ViewAcct(); cout << endl; susan.ViewAcct(); cout << endl; cout << "存1000块到susan的帐户里:/n"; susan.Depost(1000); cout << "现在susan帐户里有:" << susan.Balance() << endl<< endl; cout << "从kelso的卡里取4200块:" << endl; kelso.WithDraw(4200.00); cout << endl; cout << "kelso帐户里还有:" ; cout << kelso.Balance(); cout << endl<< endl; cout << "从susan卡里取4200块:" << endl; susan.WithDraw(4200.00); susan.ViewAcct(); return 0; } usebrass2.cpp #include "brass.h" #include <iostream> const int CLIENTS = 4; //客户 const int LEN = 40; // 数组长度 int main() { using std::cin; using std::cout ; using std::endl; Brass * p_clients[CLIENTS]; int i; for (i = 0; i < CLIENTS; i++) { char temp[LEN]; long tempnum; double tempbal; char kind; cout << "输入客户姓名:"; cin.getline(temp, LEN); cout << "输入客户的帐号:"; cin >> tempnum; cout << "输入客户的存款额:"; cin >> tempbal; cout << "标准用户输入1, 信用卡用户输入2:"; while(cin >> kind && ('1' != kind && '2' != kind)) cout << "请输入1或者2: " ; if ( '1' == kind) { p_clients[i] = new Brass(temp, tempnum, tempbal); } else { double tmax, trate; cout << "输入额度:" ; cin >> tmax; cout << "输入利息:"; cin >> trate; p_clients[i] = new BrassPlus(temp, tempnum, tempbal, tmax, trate); } while(cin.get() != '/n') continue; } cout << endl; for (i = 0; i < CLIENTS; i++ ) { p_clients[i]->ViewAcct(); cout << endl; } for (i = 0; i < CLIENTS; i++) { delete p_clients[i]; } cout << "完成!" << endl; return 0; }