Answer to Question #186178 in C++ for Ajith

Question #186178

Design simple class BankAccount. If amount to be withdrawn is greater than balance, throw “Insufficient Balance” exception.


1
Expert's answer
2021-05-01T17:50:55-0400
#include <iostream>
using namespace std;
class BankAccount{
    int amount = 0;
    public:
        BankAccount(){}
        void deposit(int x){
            this->amount += x;
        }
        void withdraw(int x){
            try{
                string s = "Insufficient Balance";
                if(this->amount < x) throw(s);
                else{
                    this->amount -= x;
                    cout<<"Withdrawal successful";
                }
            }
            catch(string s){
                cout<<s;
            }
        }
};
int main(){
    BankAccount Alvin;
    int x;
    cout<<"Enter amount to deposit: ";
    cin>>x;
    Alvin.deposit(x);
    cout<<"Enter amount to withdraw: ";
    cin>>x;
    Alvin.withdraw(x);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS