这个我把name分成姓和名了,你可以稍微改一下:
(对了,这是三个文件,放在一个工程里就可以了)
//PERSON_H
#ifndef PERSON_H
#define PERSON_H
#include <iostream.h>
class person
{
friend ostream &operator<<(ostream &,const person &);
friend istream &operator>>(istream &,person &);
public:
person(char *fna="Bob" , char *lna="Smith" , char *se="Male", int ag=20);
person(const person &);
~person();
const char *getfirstName() const;
const char *getlastName() const;
const char *getsex() const;
int getage() const;
static int getPerCount();
const person &operator=(const person &);
private:
char *firstName;
char *lastName;
char *sex;
int age;
static int PerCount;
};
#endif
//PERSON_CPP
#include <iostream.h>
#include <string.h>
#include <assert.h>
#include "person.h"
int person::PerCount=0;
int person::getPerCount()
{
return PerCount;
}
person::person(char *fna, char *lna, char *se,int ag)
{
++PerCount;
firstName=new char[strlen(fna)+1];
assert(firstName!=0);
strcpy(firstName,fna);
lastName=new char[strlen(lna)+1];
assert(lastName!=0);
strcpy(lastName,lna);
sex=new char[strlen(se)+1];
assert(sex!=0);
strcpy(sex,se);
age=ag;
cout<<"Person默认构造 当前实例化对象个数:"<<PerCount<<endl;
}
person::person(const person &pe)
{
++PerCount;
firstName=new char[strlen(pe.firstName)+1];
assert(firstName!=0);
strcpy(firstName,pe.firstName);
lastName=new char[strlen(pe.lastName)+1];
assert(lastName!=0);
strcpy(lastName,pe.lastName);
sex=new char[strlen(pe.sex)];
assert(sex!=0);
strcpy(sex,pe.sex);
age=pe.age;
cout<<"Person拷贝构造 当前实例化对象个数:"<<PerCount<<endl;
}
person::~person()
{
--PerCount;
delete [] firstName;
delete [] lastName;
delete [] sex;
cout<<"Person析构释放 当前实例化对象个数:"<<PerCount<<endl;
}
const char *person::getfirstName() const
{
return firstName;
}
const char *person::getlastName() const
{
return lastName;
}
const char *person::getsex() const
{
return sex;
}
int person::getage() const
{
return age;
}
const person &person::operator=(const person &pe)
{
if(&pe!=this)
{
delete [] firstName;
delete [] lastName;
delete [] sex;
firstName=new char[strlen(pe.firstName)+1];
assert(firstName!=0);
strcpy(firstName,pe.firstName);
lastName=new char[strlen(pe.lastName)+1];
assert(lastName!=0);
strcpy(lastName,pe.lastName);
sex=new char[strlen(pe.sex)+1];
assert(sex!=0);
strcpy(sex,pe.sex);
age=pe.age;
}
return *this;
}
ostream &operator<<(ostream &output,const person &pe)
{
cout<<"姓 名:"<<pe.firstName<<' '<<pe.lastName<<"\t\t性 别:"<<pe.sex
<<"\t\t\t年 龄:"<<pe.age;
return output;
}
istream &operator>>(istream &input,person &pe)
{
char *fna,*lna,*se;
fna=new char[20];
assert(fna!=0);
lna=new char[20];
assert(lna!=0);
se=new char[10];
assert(se!=0);
na=new char[30];
assert(na!=0);
cin>>fna>>lna>>se>>pe.age;
pe.firstName=new char[strlen(fna)+1];
assert(pe.firstName!=0);
strcpy(pe.firstName,fna);
pe.lastName=new char[strlen(lna)+1];
assert(pe.lastName!=0);
strcpy(pe.lastName,lna);
pe.sex=new char[strlen(se)+1];
assert(pe.sex!=0);
strcpy(pe.sex,se);
return input;
}
//TEST_CPP 测试程序
#include <iostream.h>
#include "person.h"
int main()
{
person p1("鲍伯","史密斯","男",21);
cout<<p1;
person p1;
cout<<"请输入人员信息如:\n鲍伯 史密斯 男 21"<<endl;
cin>>p1;
cout<<p1;
return 0;
}
温馨提示:内容为网友见解,仅供参考