Design simple class BankAccount. If amount to be withdrawn is greater than balance, throw “Insufficient Balance” exception.
#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;
}
Comments
Leave a comment