//Person.h:定义Person类
class Person
{
private:
char m_strName[20];
int m_nAge;
int m_nSex;
public:
void Register(char *Name,int Age,char Sex);
void GetName(char *Name);
int GetAge( );
char GetSex( );
};
//Person.cpp:Person类成员函数的定义
#include <string.h>
#include "Person.h"
void Person::Register(const char *name,int age,char sex)
{
strcpy(m_strName,name);
m_nAge=age;
m_nSex=(sex= =´m´?0:1);
}
void Person::GetName(char *name)
{
strcpy(name,m_strName);
}
int Person::GetAge( )
{
return m_nAge;
}
char Person::GetSex( )
{
return (m_nSex= =0? ´m´: ´f´);
}
//main.cpp:人事资料的输入和输出
#include <iostream.h>
#include "Person.h"
void OutPersonData(Person person)
{
char name[20];
person.GetName(name); cout<<name<<´\t´<<person.GetAge()<<´\t´
<<person.GetSex()<<endl;
}
void main( )
{
char name[20],sex;
int age;
Person person1;
cout<<"Enter a person’s name,age and sex: ";
cin>>name>>age>>sex;
person1.Register(name,age,sex);
cout<<"person1: \t";
OutPersonData(person1);
}
输入:
Enter a person’s name,age and sex: Wang2 20 ´f´
输出:
person1: Wang2 20 f
听说这题有个指针悬挂的错误,但我怎么也找不出来