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

编写一个类person 其中包含姓名、性别和年龄的属性,包含构造方法以及显示姓名、性别和年龄的方法,再编写一个学生类Student..他继承person类,其中包含学号属性,包含构造方法以及显示学号的方法,最后编写一个主类Example1.包含main()方法,在main()方法中定义两个学生s1 s2并给他们赋值,最后显示他们的学号,姓名,性别以及年龄

第1个回答  推荐于2017-10-03
//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<<this->m_strName<<endl;
return this->m_strName;
}

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

CString getSex() //取得性别
{
cout<<this->m_strSex<<endl;
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<<this->m_strSno<<endl;
return this->m_strSno;
}
}追问

虽然没有验证你的回答,但是我已经写出来了

还是给你吧

本回答被提问者采纳
相似回答