c++ 类 银行取钱问题代码
**// 类**
// version 00
#ifndef BANK_H_
#define BANK_H_
#include <string>
class Bank // class declaration
{
private:
std::string name;
std::string number;
double balance;
public:
Bank(const std::string & co, const std::string & n, double cou=0.0);
void show(void)const;
void sell(double cash);
void update(double cash);
}; // note semicolon at the end
#endif
**// 成员函数**
// version 00
#include <iostream>
#include "stock00.h"
Bank::Bank(const std::string & co, const std::string & n, double cou)
{
name = co;
number = n;
balance= cou;
}
void Bank::show(void) const
{
std::cout << "your count is " << name << "\n"
<< " the count number is :" << number << name << "\n"
<< ";now,its blance $ is " << balance << std::endl;
}
void Bank::sell( double cash)
{
if (cash < 0)
std::cout << "input fail !!";
else
{
balance += cash;
std::cout << "$ add" << cash << std::endl;
}
}
void Bank::update(double cash)
{
if (cash < 0)
std::cout << "input fail !!";
else
{
balance -= cash;
std::cout << "count remain:" <<balance << std::endl;
}
}
#include <iostream>
#include "stock00.h"
#include <string>
int main()
{
using namespace std;
cout << "请输入你的信息\n";
string a;
string b;
double c;
cout << "输入你的账户名: ";
getline(cin, a);
cout << "输入你的账号: ";
cin >> b;
cout << "输入你的存款: ";
cin >> c;
Bank myself(a, b, c);
myself.show();
//三个功能,存款 取款 退出
cout << "请选择业务: d 存款 w 取款, q 退出\n";
char d;
double cash;
cin >> d;
while (d != 'q')
{
if (d == 'd')
{
cout << "存款 ";
cin >> cash;
myself.sell(cash);
myself.show();
}
else if (d == 'w')
{
cout << "取款 ";
cin >> cash;
myself.update(cash);
myself.show();
}
else
break;
cout << "Please choose what you want";
cin >> d;
}
cout << "欢迎下次光临";
return 0;
}
运行结果