这里写自定义目录标题
设计一个单基继承的类层次程序,利用Person类派生出Student类,增加属性xh(学号),Person类中至少有姓名、年龄等数据成员,成员函数中构造函数对其初始化,析构函数释放相应存储单元,输出函数输出其数据成员的值,其它成员函数根据需要添加,在主函数中进行测试
#include
#include
using namespace std;
class Person{
protected:
int m_Age;
string m_Name;
public:
Person(string name,int age){
this->m_Age=age;
this->m_Name=name;
}
~Person(){
}
void print(){
cout<<“姓名:”<m_Name<<“年龄:”<m_Age<<endl;
}
};
class Student:public Person
{
protected:
string xh;
public:
Student(string name,int age,string id):Person(name,age)
{
this->xh=id;
}
~Student(){
}
void print1(){
cout<<"姓名:"<<this->m_Name
<<"年龄:"<<this->m_Age
<<"学号:"<<this->xh<<endl;
}
};
int main(){
Student s(“小明”,23,“203677”);
s.print1();
}