current position:Home>How should this program be completed

How should this program be completed

2022-02-02 19:26:18 CSDN Q & A

Create an abstract base class Account( Account ) class , Indicates the customer account of the bank . Customers can deposit money from their bank accounts ( That is to credit )、 Withdraw money ( That is, debit ).Account Class includes :
(1) A data member representing the account balance Balance( type :double), And provide... For the data member getBalance and setBalance Two member functions , among ,setBalance The function guarantees that the balance is nonnegative , If it's negative , Set the balance to 0, And display the error message “Balance is invalid”;getBalance Function returns the value of the data member representing the current balance ;
(2) An advantage of setBalance Function to initialize the constructor and a virtual Destructor ;
(3) A with deposit amount as the parameter virtual function credit, Realize the function of saving money in bank account ;
(4) A with withdrawal amount as the parameter virtual function debit, To realize the withdrawal function of bank account , And guarantee that the account will not overdraw . If overdraft , Then the current balance remains unchanged , And print the information “Insufficient balance.”.




Refer to the answer 1:
#include<iostream>#include<string>using namespace std;class Account{public:    Account(double Balance){        setBalance(Balance);    }    virtual ~Account(){}     void setBalance(double Balance){        if(Balance >= 0)            this->Balance = Balance;        else{            this->Balance = 0;            cout<<"Balance is invalid"<<endl;        }    }    double getBalance(){        return Balance;    }    virtual void credit(double money) = 0;    virtual void debit(double money) = 0;private:    double Balance;};class Bank:public Account{public:    Bank(double ba):Account(ba){}    void credit(double money){        setBalance(money + getBalance());    }    void debit(double money){        if(money > getBalance())            cout<<"Insufficient balance."<<endl;        else            setBalance(getBalance()-money);    }};int main(){    Bank b(-900);    b.credit(1200);    b.debit(4000);    b.debit(3000);    cout<<" The remaining amount is :"<<b.getBalance()<<endl;    return 0;}



Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202021926167572.html

Random recommended