编写一个类person 其中包含姓名、性别和年龄的属性,包含构造方法以及显示姓名、性别和年龄的方法

2025-02-06 09:57:26
推荐回答(1个)
回答1:

//CPerson类
class CPerson
{
protect:
CString m_strName;
UINT m_nAge;
CString m_strSex;
public:
CPerson() //默认构造函数
{
m_strName.Empty();
m_nAge = 22;
m_strSex.Empty();
}

CPerson(CString strName="",UINT nAge=22,CString strSex="") //一般构建函数
{
this->m_strName = strName;
this->m_nAge = nAge;
this->m_strSex = strSex;
}

CPerson(const CPerson& perObj) //拷贝构造函数
{
this->m_strName = perObj.m_strName;
this->m_nAge = perObj.m_nAge;
this->m_strSex = perObj.m_strSex;
}
~CPerson(){} //析构函数
CString getName() //取得名字
{
cout<m_strName< return this->m_strName;
}

UINT getAge() //取得年龄
{
cout<m_nAge< return this->m_nAge;
}

CString getSex() //取得性别
{
cout<m_strSex< return this->m_strSex;
}
};

//学生类Student
class CStudent :public CPerson
{
private:
CString m_strSno;
public:
CStudent()
{
m_strSno.Empty();
}

CStudent(CString strSno)
{
this->m->strSno = strSno;
}

~Student(){}

CString getSno()
{
cout<m_strSno< return this->m_strSno;
}
}