zoukankan      html  css  js  c++  java
  • 普通类继承

    #ifndef _BRASS_H_
    #define _BRASS_H_
    
    #include <string>
    
    //Brass Account Class
    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(void) const;
        virtual void ViewAcct(void) 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(void) const;
        virtual void Withdraw(double amt);
        void ResetMax(double m)
        {
            maxLoan = m;
        }
        void ResetRate(double r)
        {
            rate = r;
        }
        void ResetOwes(void)
        {
            owesBank = 0;
        }
    };
    
    #endif
    #include <iostream>
    #include "brass.h"
    using std::cout;
    using std::endl;
    using std::string;
    
    //formatting stuff
    typedef std::ios_base::fmtflags format;
    typedef std::streamsize precis;
    format setFormat(void);
    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." << endl;
        }
        else
        {
            balance += amt;
        }
    }
    
    void Brass::Withdraw(double amt)
    {
        //set up ###.## format
        format initialState = setFormat();
        precis prec = cout.precision(2);
    
        if(amt < 0)
        {
            cout << "Withdrawal amount must be positive; " << "withdrawal canceled." << endl;
        }
        else if(amt <= balance)
        {
            balance -= amt;
        }
        else
        {
            cout << "Withdrawal amount of $" << amt << " exceeds your balance." << endl << "Withdrawal canceled." << endl;
        }
    
        restore(initialState, prec);
    }
    
    double Brass::Balance(void) const
    {
        return balance;
    }
    
    void Brass::ViewAcct(void) 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 methods
    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) //uses implicit copy constructor
    {
        maxLoan = ml;
        owesBank = 0.0;
        rate = r;
    }
    
    //redefine how ViewAcct() works
    void BrassPlus::ViewAcct(void) const
    {
        //set up ###.## format
        format initialState = setFormat();
        precis prec = cout.precision(2);
    
        Brass::ViewAcct();  //display base portion
        cout << "Maxinum loan: $" << maxLoan << endl;
        cout << "Owed to bank: $" << owesBank << endl;
        cout .precision(3);
        cout << "Loan Rate : " << 100 * rate << "%" << endl;
    
        restore(initialState, prec);
    }
    
    //redefine how Withdraw() works
    void BrassPlus::Withdraw(double amt)
    {
        //set up ###.## format
        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: $" << advance << endl;
            cout << "Finance charge: $" << advance * rate << endl;
    
            Deposit(advance);
            Brass::Withdraw(amt);
        }
        else
        {
            cout << "Credit limit exceeded . Transaction cancelled." << endl;
        }
    
        restore(initialState, prec);
    }
    
    format setFormat(void)
    {
        //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);
    }
    #include <iostream>
    #include <string>
    #include "brass.h"
    const int CLIENTS = 4;
    
    int main(void)
    {
        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 fraction: ";
                cin >> trate;
                p_clients[i] = new BrassPlus(temp, tempnum, tempbal, tmax, trate);
            }
    
            while(cin.get() != '
    ')
            {
                continue;
            }
        }
        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.
    ";
    
        return 0;
    }
  • 相关阅读:
    MongoDB(01):Windows 平台安装 MongoDB
    MongoDB(99):NoSQL Manager for MongoDB 教程(进阶篇)
    MongoDB(99):NoSQL Manager for MongoDB 教程(基础篇)
    echarts(10):旭日图
    echarts(09):ECharts 事件处理
    echarts(08):数据的视觉映射
    echarts(07):响应式
    echarts(06):数据集(dataset)
    echarts(05):数据集(dataset)
    echarts(04):异步加载数据
  • 原文地址:https://www.cnblogs.com/pingge/p/3363821.html
Copyright © 2011-2022 走看看