编程题A. 对象数组(类和对象)B. 存折类定义(类与对象)C. 点和圆 (类与对象)D. 最胖的加菲(类与对象+数组)E. 身体评估(类与对象)【id:156】【20分】B. 存折类定义(类与对象)

题目描述

定义一个存折类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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值