C++ Primer Plus第五版 第10章 编程练习答案

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/19 
From : C++ Primer Plus第五版第10章编程练习 第1题   
*******************************************************************************************************************/
//头文件
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
using namespace std;
class Account
{
private:
	string name;
	string account;
	double savings;
public:
	Account();
	Account(const string &n,const string &a ,const double money);
	void show()const;
	void save(const double money);
	void withdraw(const double money);
	~Account();
};
#endif
//头文件对应的cpp文件
#include<iostream>
#include <string>
#include "Account.h"
Account::Account()
{
	name = " ";
	account = " ";
	savings =0;
}
Account::Account(const string  &n,const string &a ,const double money)
{
	name = n;
	account = a;
	savings = money;
}
Account::~Account()
{
	cout << "Bye.\n";
}

void Account::show()const
{
	cout << "The name is : " << name << endl;
	cout << "The account is : "<< account <<endl;
	cout << "The saving is : " << savings <<endl;
}
void Account::save(const double money)
{
	if (money <0)
	{
		cerr << "The number of saving can't be negative.\n";
	}
	else
		savings+=money;
}

void Account::withdraw(const double money)
{
	if (money > savings)
	{
		cerr << "The number of withdrawing can't be bigger than savings.\n";
	}
	else
		savings-=money;
}
//演示主程序
#include<iostream>
#include "Account.h"
int main()
{
	Account account1("Bob Smith","452164146413541641",7800);
	account1.show();
	account1.save(1200);
	account1.show();
	account1.save(-1200);
	account1.show();
	account1.withdraw(2000);
	account1.show();
	account1.withdraw(20000);
	account1.show();
	system("pause");
	return 0;
}


/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/11/17 
From : C++ Primer Plus第五版第10章编程练习 第2题   
*******************************************************************************************************************/
//头文件
#ifndef PERSON_H_
#define PERSON_H_
using namespace std;
#include<string>
class Person
{
private:
	static const int LIMIT =25;
	string lname;
	char fname[LIMIT];
public:
	Person(){lname="";fname[0]='\0';}
	Person(const string & ln,const char *fn= "Heyyou");
	void show() const;
	void FormalShow()const;
};
#endif
//头文件对应的cpp文件
#include<iostream>
#include<cstring>
#include"Person.h"
using namespace std;
Person::Person(const string & ln,const char *fn)
{
	lname = ln;
	strncpy(fname,fn,LIMIT-1);
	fname[LIMIT-1]='\0';
}

void Person::show() const
{
	cout << "The name is : " << fname << " " << lname <<endl;
}
void Person::FormalShow()const
{
	cout << "The name is : " << lname << " " << fname <<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值