B. 存折类定义(类与对象)

时间限制1s  内存限制128MB

题目描述:

定义一个存折类CAccount,存折类具有帐号(account, long)、姓名(name,char[10])、余额(balance,float)等数据成员,可以实现存款(deposit,操作成功提示“saving ok!”)、取款(withdraw,操作成功提示“withdraw ok!”)和查询余额(check)的操作,取款金额必须在余额范围内,否则提示“sorry! over limit!”。编写主函数,建立这个类的对象并测试,输入账号、姓名、余额后,按照查询余额、存款、查询余额、取款、查询余额的顺序调用类方法并输出。

输入:

第一个存折的账号、姓名、余额

存款金额

取款金额

第二个存折的账号、姓名、余额

存款金额

取款金额

输出:

第一个存折的账户余额

存款操作结果

账户余额

取款操作结果

账户余额

第二个存折的账户余额

存款操作结果

账户余额

取款操作结果

账户余额

输入样例:

9111 Tom 1000
500
1000
92220 John 500
500
1500

输出样例:

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

#include <iostream>
using namespace std;
class CAccount {
private:
	long account;
	char name[10];
	float balance;
public:
	void set() {
		cin >> account >> name >> balance;
	}
	void deposit() {
		int deposit1;
		cin >> deposit1;
		balance += deposit1;
		cout << "saving ok!" << endl;
	}
	void withdraw() {
		int withdraw1;
		cin >> withdraw1;
		if (balance >= withdraw1) {
			balance -= withdraw1;
			cout << "withdraw ok!" << endl;
		}
		else {
			cout << "sorry! over limit!" << endl;
		}
	}
	void check() {
		cout << name << "'s balance is " << balance << endl;
	}
};
int main()
{
	CAccount a, b;
	a.set();
	a.check();
	a.deposit();
	a.check();
	a.withdraw();
	a.check();
	b.set();
	b.check();
	b.deposit();
	b.check();
	b.withdraw();
	b.check();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值