B. 存折类定义(类与对象)
题目描述
定义一个存折类CAccount,存折类具有帐号(account, long)、姓名(name,char[10])、余额(balance,float)等数据成员,可以实现存款(deposit,操作成功提示“saving ok!”)、取款(withdraw,操作成功提示“withdraw ok!”)和查询余额(check)的操作,取款金额必须在余额范围内,否则提示“sorry! over limit!”。编写主函数,建立这个类的对象并测试,输入账号、姓名、余额后,按照查询余额、存款、查询余额、取款、查询余额的顺序调用类方法并输出。
输入
第一个存折的账号、姓名、余额
存款金额
取款金额
第二个存折的账号、姓名、余额
存款金额
取款金额
输出
第一个存折的账户余额
存款操作结果
账户余额
取款操作结果
账户余额
第二个存折的账户余额
存款操作结果
账户余额
取款操作结果
账户余额
输入样例1
9111 Tom 1000
500
1000
92220 John 500
500
1500
输出样例1
Tom's balance is 1000
saving ok!
Tom's balance is 1500
withdraw ok!
Tom's balance is 500
John's balance is 500
saving ok!
John's balance is 1000
sorry! over limit!
John's balance is 1000
该题主要考察对C++类的定义及其应用
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include <iomanip>
#include<cmath>
#include<cstring>
#include<cctype>
#include<queue>
#include<set>
using namespace std;
class CAccount
{
public:
long account;
char name[10];
float balance;
void check(CAccount acc);
void save(int money);
void withdraw(int output);
};
void CAccount::check(CAccount acc)
{
cout << acc.name << "'s balance is " << acc.balance << endl;
}
void CAccount::save(int money)
{
balance += money;
cout << "saving ok!" << endl;
}
void CAccount::withdraw(int output)
{
if (balance >= output)
{
balance -= output;
cout << "withdraw ok!" << endl;
}
else
{
cout << "sorry! over limit!" << endl;
}
}
void deal(CAccount& acc1)
{
int savemoney, money, output;
cin >> acc1.account >> acc1.name >> acc1.balance;
acc1.check(acc1);
cin >> money;
acc1.save(money);
acc1.check(acc1);
cin >> output;
acc1.withdraw(output);
acc1.check(acc1);
}
int main()
{
CAccount acc1, acc2;
deal(acc1);
deal(acc2);
return 0;
}
|