【C++ Primer Plus习题】10.1

大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←

问题:

这里是引用
在这里插入图片描述

解答:
main.cpp

#include <iostream>
#include "BankAccount.h"
using namespace std;

int main()
{
	BankAccount BA1("韩立","韩跑跑",1);
	BA1.get_info();
	BankAccount BA;
	BA.init_account("姚国林", "amdin", 10000);
	BA.get_info();
	BA.deposit(1000);
	BA.get_info();
	BA.withdraw(2000);
	BA.get_info();

	return 0;
}

BankAccount.h

#pragma once
#include <iostream>
using namespace std;
class BankAccount
{
private:
	string fullname;
	string accid;
	double balance;
public:
	BankAccount();
	BankAccount(const string name, const string id, double bal);
	~BankAccount();
	void init_account(const string name, const string id, double bal);
	void get_info()const;
	void deposit(double cash);
	void withdraw(double cash);
};


BankAccount.cpp

#include "BankAccount.h"

BankAccount::BankAccount()
{
	this->fullname = "";
	this->accid = "";
	this->balance = 0.0;
}
BankAccount::BankAccount(const string name, const string id, double bal)
{
	this->fullname = name;
	this->accid = id;
	this->balance = bal;
}
BankAccount::~BankAccount()
{

}
void BankAccount::init_account(const string name, const string id, double bal)
{
	this->fullname = name;
	this->accid = id;
	this->balance = bal;
}
void BankAccount::get_info()const
{
	cout << "姓名为:" << this->fullname << endl;
	cout << "账号为:" << this->accid << endl;
	cout << "存款为:" << this->balance << endl;
}
void BankAccount::deposit(double cash)
{
	this->balance += cash;
}
void BankAccount::withdraw(double cash)
{
	this->balance -= cash;
}

运行结果:
在这里插入图片描述

考查点:

  • 类和对象
  • 分文件

2024年9月3日19:52:54

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值