有两种方式可以实现多态公有继承
1) 在派生类中重新定义基类的方法
2) 使用虚方法
如下是使用虚方法
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\n";
}
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\n";
}
else if(amt<=balance)
{
balance=balance-amt;
}
else
{
cout<<"withrdrawl amout of $"<<amt
<<" exceeds your balance\n"
<<"withdrawal canceled\n";
}
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<<"%\n";
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.\n";
}
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);
}
const int CLIENTS=4;
int main()
{
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 //输入的kind是2
{
double tmax,trate;
cout<<"Enter the overdraft limit:$"; //overdraft 透支
cin>>tmax;
cout<<"Enter the interest rate"
<<"as a decimal fraction:";
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;
for(int i=0;i<CLIENTS;i++)
{
p_clients[i]->ViewAcct();
cout<<endl;
}
for(int i=0;i<CLIENTS;i++)
{
delete p_clients[i]; //释放内存
}
cout<<"Done.\n";
return 0;
}
运行效果如下
开发环境Qt Creator 4.0.3 (Community)