C++primer plus第六版课后编程题答案10.2

Person.h

#ifndef Person_H_
#define Person_H_
#include <string>
using namespace std;
class Person{
private:
	static const int LIMIT=25;
	string lname;
	char fname[LIMIT];
public:
	Person(){lname="default";fname[0]='\0';}
	Person(const string &ln,const char *fn="Heyyou");
	~Person();
	void show()const;
	void FormalShow()const;




};
#endif

Person.cpp

#include <iostream>
#include "Person.h"
#include <string>
#include <cctype>
using namespace std;
/*
Person::Person()
{	
}*/
Person::Person(const string &ln,const char*fn)//不要写成const char*fn="Heyyou"
{				//会发生重定义参数错误
		cout<<"\n\nln address:"<<&ln<<endl;
		lname=ln;
		cout<<"lname address:"<<&lname<<endl;
		int i=0;
		for(;i<LIMIT&&fn[i]!='\0';i++)
			fname[i]=fn[i];
		fname[i]='\0';
}
Person::~Person()
{}

void Person::show()const
{
	cout<<"\nlname1:"<<lname<<endl;
	cout<<"lname address:"<<&lname<<endl;
	cout<<"fname1:"<<fname<<endl;
	//showChar(fname);
}
void Person::FormalShow()const
{
	
	cout<<"\nlname address:"<<&lname<<endl;
	cout<<"fname2:"<<fname<<endl;
	cout<<"lname2:"<<lname<<endl;
}

void showChar(const char fn[])
{
	for(int i=0;fn[i]!='\0';i++)
		cout<<fn[i];

}

main102.cpp

#include <iostream>
#include "Person.h"
using namespace std;
void main102()
{
	Person p;
	p.show();
	p.FormalShow();
	
	Person p1("Smythe");
	p1.show();
	p1.FormalShow();
	Person p2("Dimwiddy","Sam");
	p2.show();
	p2.FormalShow();
	cin.get();



}


posted @ 2014-04-13 20:25  天下纵横C++  阅读(182)  评论(0编辑  收藏  举报