C++ Primer Plus第六版第十章对象和类编程练习答案

1.

#include<iostream>
#include<cstring>
class BankAccount
{
   
private:
	char name[40];
	char acctnum[25];
	double balance;
public:
	BankAccount(const char * client, const char * num, double bal = 0.0);
	void show()const;
	void deposit(double cash);
	void withdraw(double cash);
};
int main()
{
   
	BankAccount Account("jack", "123", 1000);
	Account.show();
	Account.deposit(100);
	Account.show();
	Account.withdraw(100);
	Account.show();
	return 0;
}
BankAccount::BankAccount(const char * client, const char * num, double bal)
{
   
	strcpy_s(name, client);
	strcpy_s(acctnum, num);
	balance = bal;
}
void BankAccount::show() const
{
   
	using std::cout;
	using std::endl;
	cout << "name: " << name << endl;
	cout << "acctnum: " << acctnum << endl;
	cout << "balance: " << balance << endl;

}
void BankAccount::deposit(double cash)
{
   
	using std::cout;
	if (cash >= 0)
		balance += cash;
	else
		cout << "请输入一个正数\n";
}
void BankAccount::withdraw(double cash)
{
   
	using std::cout;
	if (balance == 0)
		cout << "The deposit is 0\n";
	else if ((balance - cash) < 0)
		cout << "You don't have enough savings\n";
	else
		balance -= cash;
}

2.

#include<iostream>
#include<cstring>
#include<string>
using std::string;
class Person 
{
   
private:
	static const int LIMIT = 25;
	string lname;
	char fname[25];
public:
	Person() {
    lname = ""; fname[0] = '\0'; }
	Person(const string & ln, const char * fn = "Heyyou");
	void Show() const;
	void FormalShow() const;
};
int main()
{
   
	using namespace std;
	Person one;
	Person two("Smythecraft");
	Person three("Dimwiddy", "Sam");
	one.Show();
	cout << endl;
	one.FormalShow();
	two.Show();
	cout << endl;
	two.FormalShow();
	three.Show();
	cout << endl
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值