实现多态共有继承的两种方法
1 在派生类中重新定义基类的方法
2 使用虚方法
P493程序清单13.7使用的方法为在派生类中重新定义基类的方法
brass.h
#ifndef BRASS_H
#define BRASS_H
#include <string>
class Brass{
private:
std::string fullName; //客户姓名
long acctNum; //账号
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); //取款
double Balance() const; //显示余额
virtual void ViewAcct() const;
virtual ~Brass() {}
};
//Brass Plus Account Class
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 ResetOwes() {owesBank=0;};
};
#endif // BRASS_H
main.cpp
#include <iostream>
#include "brass.h"
using namespace std;
//formatting stuff
typedef std::ios_base::fmtflags format;
typedef std::streamsize precis;
format setFormat();
void restore(format f,precis p);
//Brass methods
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
";
}
else
balance=balance+amt;
}
void Brass::withdraw(double amt)
{
//set up ###.##format
format initialState=setFormat();
precis prec=cout.precision(2);
if(amt<0)
{
cout<<"withdrawal amouut must be positive;"
<<"withdrawal canceled
";
}
else if(amt<=balance)
{
balance=balance-amt;
}
else
{
cout<<"withrdrawl amout of $"<<amt
<<" exceeds your balance
"
<<"withdrawal canceled
";
}
restore(initialState,prec);
}
double Brass::Balance() const
{
return balance;
}
void Brass::ViewAcct() const
{
//set up ###.## format
format initialState=setFormat();
precis prec=cout.precision(2);
cout<<"Client:"<<fullName<<endl;
cout<<"Account Number:"<<acctNum<<endl;
cout<<"Balance:$"<<balance<<endl;
restore(initialState,prec); //存储原始的格式
}
//BrassPlus的方法
BrassPlus::BrassPlus(const 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
{
//set up ###.##format
format initialState=setFormat();
precis prec=cout.precision(2);
Brass::ViewAcct(); //显示基类的部分
cout<<"Maximum loan:$"<<maxLoan<<endl;
cout<<"Owed to back:$"<<owesBank<<endl;
cout.precision(3);
cout<<"Loan Rate"<<100*rate<<"%
";
restore(initialState,prec);
}
//重定义
//如果用户提取的金额超过了结余,该方法将安排贷款
void BrassPlus::withdraw(double amt)
{
format initialState=setFormat();
precis prec=cout.precision(2);
//使用Balance()函数来确定结余
double bal=Balance();
if(amt<=bal)
{
//提款少于存款,提款成功
Brass::withdraw(amt); //继承类中调用基类的方法
}
else if(amt<=bal+maxLoan-owesBank)
{
double advance=amt-bal;
owesBank+=advance*(1.0+rate); //owesBank当前的透支额度
cout<<"Bank advance:$"<<advance<<endl;
cout<<"Finance charge:$"<<advance*rate<<endl;
Deposit(advance); //放贷
//放贷成功后可以今天提款
Brass::withdraw(amt);
}
else
{
cout<<"Credit limit exceeded.Transaction cancelled.
";
}
restore(initialState,prec);
}
format setFormat()
{
//set up ###.## format
return cout.setf(std::ios_base::fixed,
std::ios_base::floatfield);
}
void restore(format f, precis p)
{
cout.setf(f,std::ios_base::floatfield);
cout.precision(p);
}
int main(int argc, char *argv[])
{
cout << "Hello World!" << endl;
using std::cout;
using std::endl;
Brass Piggy("Procelot Pigg",381299,4000.00);
BrassPlus Hoggy("Horatio Hogg",382288,3000.00);
Piggy.ViewAcct();
cout<<endl;
Hoggy.ViewAcct();
cout<<"Depositing $1000 into the Hogg Account:
";
Hoggy.Deposit(1000.00); //储蓄1000元到Hoggy的账户
cout<<"New Balance:$"<<Hoggy.Balance()<<endl;
cout<<"Withdrawing $4200 from the Pigg Account:
";
//取款失败,且不能借贷,因为Piggy是基类的对象,没有实现借贷的方法
Piggy.withdraw(4200.00);
cout<<"Pigg account balance:$"<<Piggy.Balance()<<endl;
cout<<"Withdrwaing $42000 from the Hogg Account:
";
Hoggy.withdraw(4200.00);
Hoggy.ViewAcct();
return 0;
}
运行结果如下
IDE为Qt Creator 4.0.3 (Community)