针对CH.10的对象和类最后的习题写的银行账户简单系统
back.h
#ifndef __BANK_H__
#define __BANK_H__
#include <string>
using namespace std;
typedef double Money;
class bankAccount
{
private:
static const int MAX = 40;
string name;
string accountNum;
Money deposit;
public:
bankAccount();
bankAccount(string name_, string accountNum_, Money deposit_);
bool save( Money saveMoney);
bool draw( Money drawMoney);
void show();
};
#endif
back.cpp
#include <iostream>
#include "bank.h"
using namespace std;
bankAccount::bankAccount(string name_, string accountNum_, Money deposit_)
{
name = name_;
accountNum = accountNum_;
if (deposit_ < 0)
cout << "Your deposit is below 0!\n";
else
deposit = deposit_;
}
bankAccount::bankAccount()
{
name = "";
accountNum = "";
deposit = 0;
}
bool bankAccount::save(Money saveMoney)
{
this->deposit += saveMoney;
cout << "Your balance is " << this->deposit << "." << endl;
return true;
}
bool bankAccount::draw(Money drawMoney)
{
if (drawMoney <= deposit)
{
this->deposit -= drawMoney;
cout << "Your balance is " << this->deposit << "." << endl;
return true;
}
else
{
cout << "Your balance is insufficient! " << endl;
return false;
}
}
void bankAccount::show()
{
cout << "Your account name: " << this->name << endl
<< "Your account number is: " << this->accountNum << endl
<< "Your deposit is: " << this->deposit << endl
<< endl;
}
use.cpp
#include <iostream>
#include "bank.h"
int main()
{
bankAccount mizuki("Mizuki", "1145141919810", 250000);
bankAccount Mostima("Mostima", "123456789", 140000);
bankAccount a = bankAccount();
mizuki.draw(1000);
Mostima.save(5000);
mizuki.show();
Mostima.show();
Mostima.draw(1111111111111111);
a.show();
return 0;
}