C++ 编程题目2(期末考试)求大虾解答。谢谢~

建立一个类person,其中有属于私有成员的3个数据成员(name,sex,age)和能够完成输入输出且属于公有成员的6个函数成员。编写C++程序,使用该类person,完成一组数据(name,sex,age)的输入和输出。

回答得好有加分,嘿嘿,谢谢各位高手和大虾~
谢谢各位,我的是期末考试,代码不可能太长。真的感谢各位了,能不能在完善一下~

这个我把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;
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-12-28
#include <iostream.h>
#include <string.h>

class person
{
private:
char name[20];
char sex;
int age;
public:
void SetName(char *n){strcpy(name,n);}
void SetSex(char s){sex=s;}
void SetAge(int a){age=a;}
char *GetName(){return name;}
char GetSex(){return sex;}
char GetAge(){return age;}
};

void main()
{ person p;
char Name[20],Sex;
int Age;
cout<<"请输入姓名、性别、年龄:";
cin>>Name>>Sex>>Age;
p.SetName(Name);
p.SetSex(Sex);
p.SetAge(Age);
cout<<"姓名:"<<p.GetName()<<",性别:"<<p.GetSex()<<",年龄:"<<p.GetAge()<<endl;
}本回答被提问者采纳
第2个回答  2010-12-28
够完成输入输出且属于公有成员的6个函数成员这句话没太看明白,是要用六个函数搞定吗
public Class person
{
/*私有成员
private string name;
private boolen sex;
private int age;

//定义姓名的构造函数
public void person (string n)
{
name=n;
}
public void coutperson
{
cout<<name<<endl; //输出姓名
}
//年龄性别同上
main()
{
person p= new person(); //构造函数实例化
cin<<c.n; //初始化构造函数;
//年龄,性别同理
}
手上没有编辑器,也是手敲的,C++也好久没碰了,呵呵,可能会有写错误,应该也差不多吧

}
第3个回答  2010-12-27
class CPerson
{
public:
void nameFuncton( char *name)
{
cout << strcpy(m_name, name) << endl;
}
private:
char *m_name;
};

int main()
{
CPerson person;
person.nameFunction("yourname");
return 0;
}

性别跟年龄像上面一样类似,通过公有成员函数访问私有成员变量,来到达可以访问私有成员变量的效果,并输出他们的值。本回答被网友采纳
相似回答