C++继承与多态简易练习

简易银行账户管理系统

题目描述:
设计一个简单的银行账户管理系统。系统中有一个基类 BankAccount,包含纯虚函数 deposit() 和 withdraw()。要求你从 BankAccount 类派生出两个具体的账户类 SavingsAccount 和 CheckingAccount,分别实现不同的存取款方式。SavingsAccount 类可能会有存款利息计算功能,而 CheckingAccount 类可能会有支票手续费的计算功能。

#include <iostream>
#include <windows.h>
using namespace std;

//基类
class BankAccount  
{
    public:
    BankAccount():balance(0){}
    virtual void Withdraw() = 0;     //纯虚取款函数,实现多态性
    virtual void Deposit() = 0;   //纯虚存款函数
    virtual ~BankAccount(){}    //虚析构函数

    void dispalyAccount(){
        cout<<"账户余额:"<<balance<<endl;
    }
    protected:
    float balance;  //账户余额
};

//派生类 savingAccount
class savingAccount : public BankAccount    
{
    public:
    savingAccount(float h=0.0,float a=0.0,float b=0.0):interestRate(h),depositamount(a),withdrawamount(b) {} //构造函数参数初始化表
    void Withdraw() override{   //重写取款函数
        if(balance >= withdrawamount){
            balance -= withdrawamount;
        }
        else{
            cout<<"取款失败,余额不足"<<endl;
        }
    }
    void Deposit() override{    //重写存款函数
        balance += depositamount;
        balance += balance*interestRate;    //利息计算
        cout<<"存款成功"<<endl;
    }
    private:
    float interestRate;     //利息
    float depositamount;   //存款数目
    float withdrawamount;   //取款数目
};

//派生类 checkingAccount
class checkingAccount :public BankAccount
{
    public:
    checkingAccount(float fee=2.0,float h=0.0,float b=0.0):transactionFee(fee),depositamount(h),withdrawamount(b){}
    void Withdraw() override{   //取款
        if(balance >= (withdrawamount+transactionFee)){
            balance -= (withdrawamount+transactionFee);
        }
        else{
            cout<<"取款失败,余额不足"<<endl;
        }
    }
    void Deposit() override{    //存款
        balance += (depositamount-transactionFee);
        cout<<"存款成功"<<endl;
    }
    private:
    float depositamount;
    float withdrawamount;
    float transactionFee;   //手续费
};

void Function(BankAccount* ptr)     //父类指针指向派生类对象
{
    ptr->dispalyAccount();
    ptr->Deposit();
    ptr->Withdraw();
    ptr->dispalyAccount();
}

int main(int argc,const char* argv[])
{
    SetConsoleOutputCP(CP_UTF8);

//测试代码
    savingAccount s1(0.53,500,300);     //利息0.53,存款500,取款300
    checkingAccount c1(4,750,400);  //手续费4,存款750,取款400
    Function(&s1);
    cout<<endl<<endl;
    Function(&c1);
    system("pause");
    return 0;
}

运行结果

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值