In: Computer Science
create a class in C++ having
the following 4 private members:
one double variable representing the balance of a bank account
on string variable representing the bank account number
a function to deposit money from the bank account
a function to withdraw money from the bank account
the following 2 public members:
-two wrapper functions.
- one parameterless constructor
- one constructor initializing the account balance and account number
- sample:
#include <iostream> using namespace std; class account { public: account(); account(string s, double m); void setMoney(double b); void getMoney(double b); private: double money; string accountNo; void withdraw(double b); void deposit(double b); }; account::account() { money=0; accountNo="12345"; } account::account(string s, double m) { money=m; accountNo=s; } void account::withdraw(double b) { if(money<b) { cout<<"not enough"<<endl; } else { money = money-b; } cout<<"withdraw your new balance is "<<money<<endl; } void account::setMoney(double b) { deposit(b); } void account::getMoney(double b) { withdraw(b); } void account::deposit(double b) { money = money+b; cout<<"deposit: your new balance is "<<money<<endl; } int main() { account a; a.setMoney(1000); a.getMoney(500); account b("23456", 1234); b.setMoney(2000); b.getMoney(600); return 0; }
#include <iostream>
using namespace std;
class account
{
public:
account();
account(string s, double m);
void setMoney(double b);
void getMoney(double b);
private:
double money;
string accountNo;
void withdraw(double b);
void deposit(double b);
};
account::account()
{
money=0;
accountNo="12345";
}
account::account(string s, double m)
{
money=m;
accountNo=s;
}
void account::withdraw(double b)
{
if(money<b)
{
cout<<"not enough"<<endl;
}
else
{
money = money-b;
}
cout<<"withdraw your new balance is
"<<money<<endl;
}
void account::setMoney(double b)
{
deposit(b);
}
void account::getMoney(double b)
{
withdraw(b);
}
void account::deposit(double b)
{
money = money+b;
cout<<"deposit: your new balance is
"<<money<<endl;
}
int main()
{
account a;
a.setMoney(1000);
a.getMoney(500);
account b("23456", 1234);
b.setMoney(2000);
b.getMoney(600);
return 0;
}
Expected output: