标签:
class BrassPlus : public Brass
BrassPlus::BrassPlus(const std::string & 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;
}
Brass * p_clients[2];
p_clients[0] = new Brass("Tim", 112233, 1500);
p_clients[1] = new BrassPlus("Tim", 112233, 1500, 500, 0.11125);
p_clients[0]->ViewAcct();//use Brass::ViewAcct()
p_clients[1]->ViewAcct();//use BrassPlus::ViewAcct() because Brass::ViewAcct() is a virtual function
format setFormat() {
//set ###.## format
return cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
}
void restore(format f, precis p) {
//reset the format and precision
cout.setf(f, std::ios_base::floatfield);
cout.precision(p);
}
#pragma once
#ifndef BRASS_H_
#define BRASS_H_
#include <string>
class Brass
{
private:
std::string fullName;
long accNum;
double balance;
public:
Brass(const std::string & s = "Nullbody", long an = -1, double bal = 0.0);
void Deposit(double amt);
virtual void Withdraw(double amt); //virtual function
double Balance() const;
virtual void ViewAcct() const; //virtual function
virtual ~Brass() {};//virtual function
};
class BrassPlus : public Brass
{
private:
double maxLoan;
double rate;
double owesBank;
public:
BrassPlus(const std::string & s = "Nullbody", long an = -1, double bal = 0.0, double ml = 500, double r = 0.11125);
BrassPlus(const Brass &ba, double ml = 500, double r = 0.11125);
virtual void ViewAcct() const;
virtual void Withdraw(double amt);
void ResetMax(double m) { maxLoan = m; };
void ResetRate(double r) { rate = r; };
void ReserOwes() { owesBank = 0; };
};
#endif
#include"brass.h"
#include<iostream>
using std::cout;
using std::endl;
using std::string;
typedef std::ios_base::fmtflags format;
typedef std::streamsize precis;
format setFormat();
void restore(format f, precis p);
Brass::Brass(const std::string & s, long an, double bal) {
fullName = s;
accNum = 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);
if (amt < 0) {
cout << "Withdrawal amount must be positive; " << "withdrawal cancelled.\n";
}
else if (amt <= balance)
balance -= amt;
else
cout << "Withdrawal amount of $" << amt
<< " exceeds your balance.\n"
<< "Withdrawal cancelled.\n";
restore(initialState, prec);
}
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: " << accNum << endl;
cout << "Balance: $" << balance << endl;
restore(initialState, prec);
}
BrassPlus::BrassPlus(const std::string & 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 {
format initialState = setFormat();
precis prec = cout.precision(2);
Brass::ViewAcct();
cout << "MaxLoan: $" << maxLoan << endl;
cout << "Owed to Bank $" << owesBank << endl;
cout.precision(3);
cout << "Loan Rate: " << 100*rate <<"%"<< endl;
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: $" << endl;
cout << "Finance Charge: $" << advance*rate << endl;
Deposit(advance);
Brass::Withdraw(amt);
}
else
cout << "Credit limit exceedsed, Transaction cancelled.\n";
restore(initialState, prec);
}
format setFormat() {
//set ###.## format
return cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
}
void restore(format f, precis p) {
//reset the format and precision
cout.setf(f, std::ios_base::floatfield);
cout.precision(p);
}
#include<iostream>
#include"brass.h"
#include<string>
#pragma warning(disable:4996)
const int CLIENTS = 4;
int main(void) {
// freopen("Text.txt", "r", stdin);
using std::cin;
using std::cout;
using std::endl;
Brass * p_clients[CLIENTS];
std::string temp;
long tempnum;
double tempbal;
char kind;
for (int i = 0; i < CLIENTS; i++) {
cout << "Enter client`s name: ";
getline(cin, temp);
cout << "Enter client`s account number: ";
cin >> tempnum;
cout << "Enter opening balance: $";
cin >> tempbal;
cout << "Enter 1 for Brass Account or 2 for BrassPlus Account: ";
while (cin>>kind&&(kind!=‘1‘&&kind!=‘2‘))
{
cout << "Enter either 1 or 2: ";
}
if (kind == ‘1‘)
p_clients[i] = new Brass(temp, tempnum, tempbal);
else {
double tmax, trate;
cout << "Enter the overdraft limit: $";
cin >> tmax;
cout << "Enter the interest rate as a decimal function: ";
cin >> trate;
p_clients[i] = new BrassPlus(temp, tempnum, tempbal, tmax, trate);
}
while (cin.get()!=‘\n‘)
{
continue;
}
}
for (int i = 0; i < CLIENTS; i++) {
cout << endl;
p_clients[i]->ViewAcct();
}
for (int i = 0; i < CLIENTS; i++) {
delete p_clients[i];
}
cout << "Done!!";
return 0;
}
标签:
原文地址:http://www.cnblogs.com/zzandliz/p/5055436.html