C++ Premier Plus丨编程练习答案丨第十章

未完成

第一题
bankaccount.h

#include<iostream>
#ifndef BANKACCOUNT_H_
#define BANKACCOUNT_H_
class CBankAccount
{
private:
	std::string m_name;
	std::string m_account;
	double m_saving;
public:
	CBankAccount(const std::string name, const std::string account, double saving);
	CBankAccount();
	void show() const;
	void deposit(double cash);
	void withdraw(double cash);
};
#endif

bankaccountDoc.cpp

#include<iostream>
#include<string>
#include "bankaccount.h"
CBankAccount::CBankAccount(const std::string name, const std::string account, double saving)
{
	m_name = name;
	m_account = account;
	m_saving = saving;
}

CBankAccount::CBankAccount()
{
	m_name = "";
	m_account = "";
	m_saving = 0.0;
}

void CBankAccount::show() const
{
	using std::cout;
	using std::endl;
	cout<<"Showing Details..."<<endl;
	cout<<"Name = "<<m_name<<endl;
	cout<<"Account = "<<m_account<<endl;
	cout<<"Saving = "<<m_saving<<endl;
}

void CBankAccount::deposit(double cash)
{
	using std::cout;
	if(cash>=0)
	{
		m_saving = m_saving + cash;
		cout<<"Deposit Success\n";
	}
	else
	{
		cout<<"!You can't deposit money less than 0!\n";
	}
}

void CBankAccount::withdraw(double cash)
{
	using std::cout;
	if(cash>=0)
	{
		if(m_saving-cash>=0)
		{
			m_saving = m_saving-cash;
			cout<<"Withdraw Success";
		}
		else
		{
			cout<<"!Your withdraw is higher than deposit!\n";
		}
	}
	else
	{
		cout<<"!You can't withdraw money less than 0!";
	}
	
}

defmain.cpp

//请按需修改主函数
#include<iostream>
#include"bankaccount.h"
using namespace std;
int main()
{
	CBankAccount user ("Daisy", "daisy's account", 1000);
	user.show();
	user.deposit(50);
	user.show();
	user.deposit(-10);
	user.show();
	user.withdraw(100);
	user.show();
	user.withdraw(2000);
	user.show();


	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值