• 实验九


    #include <bits/stdc++.h>
    using namespace std;
    class Account
    {
    private:
        double balance;
    public:
        Account(double balance = 0) {balance=balance;} 
        virtual double credit(double creFee = 0)   {
            balance += creFee;
            return balance;
        }
    
        virtual double debit(double deFee = 0){
            if (balance >= deFee){
                balance -= deFee;
            }
            else{
                cout<<"Debit amount exceeded account balance."<<endl;
            }
            return balance;
        };
    
        double setBalance(double balance = 0){
            this -> balance = balance;
            return balance;
        }
    
        double getBalance(){
            return balance;
        }
    };
    
    class SavingsAccount : public Account
    {   
        public:
            SavingsAccount(double balance = 0 ,double interestRate = 0){
                this -> interestRate = interestRate;
                setBalance(balance);
            }
            double calculateInterest() {
                return getBalance() * interestRate * 0.01;
            }
        private:
            double interestRate;
    };
    
    class CheckingAccount : public Account
    {
        public:
        CheckingAccount(double balance = 0, double fee = 0){
            this -> fee = fee;
            setBalance(balance);
        }
        double credit(double creFee = 0) {
            if(getBalance() + creFee - fee >= 0)
                setBalance(getBalance() + creFee - fee);
            else 
                cout << "Transaction fee exceeded account balance while debiting." << endl;
            return 0;
        }
        double debit(double deFee = 0)
        {
            if(getBalance() - deFee - fee >= 0){
                setBalance(getBalance() - deFee - fee);
            }
            else{
                cout<<"Debit amount exceeded account balance."<<endl;
            } 
            return 0;
        }
        private:
            double fee; 
    };
    
    int main() {
        Account *accounts[3];
        accounts[0] = new SavingsAccount(100, 3);   //余额100元,利息3%
        accounts[1] = new CheckingAccount(100, 5);  //余额100元,交易费5元
        accounts[2] = new CheckingAccount(50, 5);   //余额50元,交易费5元
        for (int i = 0; i < 3; i++) {
            cout << "第" << i + 1 << "次循环的结果:" << endl;
            accounts[i]->debit(200);   //借款200元
            accounts[i]->debit(40);
            accounts[i]->credit(50);   //存款50元
            accounts[i]->debit(49);
            accounts[i]->debit(43);
            accounts[i]->credit(1);
            //将Account指针强制转换为SavingAccount指针
            SavingsAccount *derivedPtr =
            dynamic_cast<SavingsAccount *>(accounts[i]);
            if(derivedPtr != NULL)   //如果类型兼容,转换成功
            derivedPtr->credit(derivedPtr->calculateInterest());
            cout << fixed << setprecision(2);   //使用定点数格式,2位小数部分
            cout << "账户的余额为:" << accounts[i]->getBalance() << endl;
        }
    }
    
    作者:LightAc
    出处:https://www.cnblogs.com/lightac/
    联系:
    Email: dzz@stu.ouc.edu.cn
    QQ: 1171613053
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    Algs4-1.4DoublingRatio
    Algs4-1.4TwoSumFast
    Algs4-1.4ThreeSumFast
    Algs4-1.4ThreeSum
    Algs4-1.4TwoSum
    Algs4-1.3.50快速出错的迭代器
    *Algs4-1.3.49栈与队列-(未解决)
    Algs4-1.3.4X栈与队列-两个栈实现一个队列均摊O(1)
    Algs4-1.3.47可连接的队列、栈或steque
    Java的垃圾回机机制(见过讲得最清楚的)
  • 原文地址:https://www.cnblogs.com/lightac/p/10943576.html
Copyright © 2020-2023  润新知