C++Primer Plus对象和类的练习,练习10.10类和对象 练习2默认参数和重载

2.下面是一个非常简单的类定义:
class Person
{
private:
static const LIMIT=25;
string lname; //Person’s last name
char fname[LIMIT]; //Person’s first namepublic :
Person()(lname=“”;fname[0]=0’;}//#1
Person(const string&ln,constchar*fn=“Heyyou”);
//#2the following methods display lname and fname
void Show()consti //firstname lastname formatvoid Formal
Show()const; //lastname, firstname format
};
它使用了一个 string 对象和一个字符数组,让您能够比较它们的用法。请提供未定义的方法的代码以完成这个类的实现。再编写一个使用这个类的程序,它使用了三种可能的构造函数调用(没有参数、个参数和两个参数)以及两种显示方法。下面是一个使用这些构造函数和方法的例子:
Person one ; //use default constructor
Person two(“Smythecraft”);//use #2 with one default argument
Person three(“Dimwiddy”,“Sam”);//use #2,no defaults
one.Show();
cout <<endl;
one.FormalShow();
//etc.for two and three

#include <iostream>
using std::string;
using std::cout;
using std::endl;
class Person
{
public:
	Person() { lname = "zhangyongjiang";fname[0] = '\0'; };
	Person(const string& ln, const char* fn = "Heyyou");
	~Person();
	void show()const;
	void formalshow()const;

private:
	static const unsigned LIMIT = 25;
	string lname;
	char fname[LIMIT];
};

Person::Person(const string& ln, const char* fn)
{
	lname = ln;
	unsigned cntstr= strlen(fn) + 1;
	strcpy_s(fname, cntstr, fn);
	
}

Person::~Person()
{
}
void Person::show() const
{
	cout << lname;
}
void Person::formalshow() const
{
	cout << fname;
}

int main()
{
	Person one;
	Person two("Smythecratft");
	Person three("Dimwiddy", "Sam");
	one.show();
	cout << endl;
	three.show();
	cout << endl;

	three.formalshow();
	one.formalshow();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值