题目描述
定义一个存折类CAccount,存折类具有帐号(account, long)、姓名(name,char[10])、余额(balance,float)等数据成员,可以实现存款(deposit,操作成功提示“saving ok!”)、取款(withdraw,操作成功提示“withdraw ok!”)和查询余额(check)的操作,取款金额必须在余额范围内,否则提示“sorry! over limit!”。编写主函数,建立这个类的对象并测试,输入账号、姓名、余额后,按照查询余额、存款、查询余额、取款、查询余额的顺序调用类方法并输出。
输入
第一个存折的账号、姓名、余额
存款金额
取款金额
第二个存折的账号、姓名、余额
存款金额
取款金额
输出
第一个存折的账户余额
存款操作结果
账户余额
取款操作结果
账户余额
第二个存折的账户余额
存款操作结果
账户余额
取款操作结果
账户余额
样例查看模式
正常显示查看格式
输入样例1 <-复制
输出样例1
#include<iostream>
using namespace std;
class CAccount
{
private:
long account;
char name[10];
float balance;
public:
CAccount() { balance = 0; }
CAccount(long account, char *name, float balance)
{
this->account = account;
int len = strlen(name);
for (int i = 0; i < len; i++)
{
this->name[i] = name[i];
}
this->name[len] = '\0';
this->balance = balance;
}
//deposit,操作成功提示“saving ok!”
void deposit(int num)
{
balance += num;
cout << "saving ok!" << endl;
}
//withdraw,操作成功提示“withdraw ok!”
void withdraw(int num)
{
if (num > balance)
{
cout << "sorry! over limit!" << endl;
}
else
{
balance -= num;
cout << "withdraw ok!" << endl;
}
}
void check()
{
int len = strlen(name);
for (int i = 0; i < len; i++)
{
cout << name[i];
}
cout << "'s balance is " << balance << endl;
}
};
int main()
{
long account;
char name[10];
int balance;
int num;
cin >> account;
cin >> name;
cin >> balance;
CAccount c1(account,name,balance);
c1.check();
cin >> num;
c1.deposit(num);
c1.check();
cin >> num;
c1.withdraw(num);
c1.check();
cin >> account;
cin >> name;
cin >> balance;
CAccount c2(account, name, balance);
c2.check();
cin >> num;
c2.deposit(num);
c2.check();
cin >> num;
c2.withdraw(num);
c2.check();
return 0;
}
该博客要求用C++定义存折类CAccount,包含帐号、姓名、余额等数据成员,实现存款、取款和查询余额操作。取款需在余额范围内,否则给出提示。主函数创建对象测试,按特定顺序调用类方法并输出结果,还给出了输入输出样例。
201

被折叠的 条评论
为什么被折叠?



