C++ Primer Plus第六版 第十章 编程练习答案

//第一题
//count.h
#include <string>
class Count {
private:
	std::string m_name;
	std::string m_id;
	double m_money;
public:
	Count();
	Count(const std::string name, const std::string id, const double money);
	void disp() const;
	void add(const double money);
	void sub(const double money);
};


//count.cpp
#include <iostream>
#include "count.h"

Count::Count()
{
	m_name = "";
	m_id = "";
	m_money = 0;
}

Count::Count(const std::string name, const std::string id, const double money)
{
	m_name = name;
	m_id = id;
	m_money = money;
}

void Count::disp() const
{
	std::cout << "Name: " << m_name << std::endl << "ID: " << m_id << std::endl << "Money: " << m_money << std::endl;
}

void Count::add(const double money)
{
	m_money += money;
}

void Count::sub(const double money)
{
	m_money -= money;
}

//main.cpp
#include "count.h"

int main()
{
	Count a("Tree", "33673504", 66666.66);
	a.disp();
	a.add(666.66);
	a.disp();
	a.sub(666.66);
	a.disp();
	return 0;
}



//第二题
//Person.h
#include <string>

class Person {
private:
	static const int LIMIT = 25;
	std::string lname;
	char fname[LIMIT];
public:
	Person() { lname = ""; fname[0] = '\0'; }
	Person(const std::string &ln, const char *fn = "Heyyou");
	void Show() const;
	void FormalShow() const;
};

//Person.cpp
#include "Person.h"
#include <iostream>
#include <cstring>

Person::Person(const std::string &ln, const char *fn)
{
	lname = ln;
	strcpy_s(fname, fn);
}

void Person::Show() const
{
	std::cout << fname << " " << lname << std::endl;
}

void Person::FormalShow() const
{
	std::cout << lname << ", " << fname << std::endl;
}

//main.cpp
#include "Person.h"

int main()
{
	Person one;
	Person two("Smythecraft");
	Person three("Dimwiddy", "Sam");
	one.Show();
	one.FormalShow();
	two.Show();
	two.
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值