CPP学习笔记(自用)

针对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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值