闫立新

第13章 类继承 --抽象基类

包括贷款,虚函数,抽象基类。两个版本的最终版本。

1.acctabc.h

#ifndef ACCTABC_H_
#include<iostream>
#include<string>
using namespace std;
class AcctABC
{
private:
    string fullname;
    long acctNum;
    double balance;
protected:
    string &Fullname()
    {return fullname;}
    long AcctNum()
    {return acctNum;}
public:
    AcctABC(const string &s="none",long an=-1,double bal=0.0);
    void Deposit(double amt);
    virtual void Withdraw(double amt)=0;
    double Balance()
    {return balance;}
    virtual void ViewAcct()=0;
    virtual ~AcctABC(){};
};
class Brass:public AcctABC
{
public:
    Brass(const string &s="none",long an=-1,double bal=0.0);//??
    virtual void Withdraw(double amt);
    virtual void ViewAcct();
    virtual ~Brass(){};
};
class BrassPlus:public Brass
{
private:
    double maxLoan;
    double rate;
    double owesBank;
public:
    BrassPlus(const string &s="none",long an=-1,double bal=0.0,double ml=500,double r=0.1);
    BrassPlus(const Brass &ba,double ml=500,double r=0.1);
    virtual void Withdraw(double amt);
    virtual void ViewAcct();
    void RestMax(double m)
    {maxLoan=m;}
    void RestRate(double r)
    {rate=r;}
    void RestOwes()
    {owesBank=0;}
};
#endif

2.acctABC.cpp

 

#include<iostream>
#include"acctabc.h"
using namespace std;
AcctABC::AcctABC(const string &s,long an,double bal)   //在类的实现部分 不能有默认的参数值 否则会导致重定义
{
    fullname=s;
    acctNum=an;
    balance=bal;
}
void AcctABC::Deposit(double amt)
{
    balance=balance+amt;
}
void AcctABC::Withdraw(double amt)
{
    balance-=amt;
}


Brass::Brass(const string &s,long an,double bal):AcctABC(s,an,bal) //在类的实现部分 不能有默认的参数值 否则会导致重定义
{
}
void  Brass::Withdraw(double amt)
{
    if(amt<=Balance())
        AcctABC::Withdraw(amt);
    else
        cout<<"exceeds your balance!"<<endl;
}
void Brass::ViewAcct()
{
    cout<<"Brass Client:"<<Fullname()<<endl;
    cout<<"Account Number:"<<AcctNum()<<endl;
    cout<<"Balance :$"<<Balance()<<endl;
}


BrassPlus::BrassPlus(const string &s,long an,double bal,double ml,double r):Brass(s,an,bal) //在类的实现部分 不能有默认的参数值 否则会导致重定义
{
    maxLoan=ml;
    rate=r;
    owesBank=0.0;
}
BrassPlus::BrassPlus(const Brass &ba,double ml,double r):Brass(ba)     //在类的实现部分 不能有默认的参数值 否则会导致重定义
{
    maxLoan=ml;
    rate=r;
    owesBank=0.0;
}
void BrassPlus::Withdraw(double amt)
{
    double bal=Balance();
    if(amt<=bal)
        AcctABC::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);
        AcctABC::Withdraw(amt);
    }
    else 
        cout<<"Credit limit exceeded!"<<endl;
}
void BrassPlus::ViewAcct()
{
    Brass::ViewAcct();
    cout<<"<Max loan:$"<<maxLoan<<endl;
    cout<<"Owed to bank:$"<<owesBank<<endl;
    cout<<"Loan rate:"<<rate<<endl;
}

 

3.usebrass.cpp

#include<iostream>
#include<string>
#include"acctabc.h"
using namespace std;
const int N=4;
int main()
{
    AcctABC *p[N];
    string tname;
    long tnum;
    double tbal;
    int kind;
    for(int i=0;i<N;i++)
    {
        cout<<"Enter name of #"<<i+1<<":";
        //getline(cin,tname);
        cin>>tname;
        cout<<"Enter number :";
        cin>>tnum;
        cout<<"Enter balance:";
        cin>>tbal;
        cout<<"Enter the type of client:";
        cin>>kind;
        while(kind!=1&&kind!=2)
        {    cout<<"type error!";
            cout<<"Enter the type of client:";
            cin>>kind;
        }
        if(kind==1)
        {
            p[i]= new Brass(tname,tnum,tbal); //用new 
        }
        else 
        {
            double trate;
            double tmax;
            cout<<"Enter the rate:";
            cin>>trate;
            cout<<"Enter the MaxLoan:";
            cin>>tmax;
            p[i]=new BrassPlus(tname,tnum,tbal,tmax,trate);
        }
    //    while (cin.get()!='\n')
        //  continue;  后面的不合法输入在缓冲中的给清理掉,为下一次输入扫清障碍。还要明白一点cin.get()是不会忽略空格和回车的,
        //每次输入的最后你总是敲击enter以表明你的输入结束从而为上述语句总能处理成功提供保障。
    }
        for(int i=0;i<N;i++)
        {
            p[i]->ViewAcct();
            cout<<endl;
        }
        p[0]->Withdraw(1200);
        p[0]->ViewAcct();
        for(int i=0;i<N;i++)
        {
            delete p[i];
        }
        cout<<"Done!\n";
        return 0;
}

 

posted on 2015-10-22 10:45  闫立新  阅读(124)  评论(0编辑  收藏  举报

导航