由Person类派生出大学生CollegeStu类。设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,编写成员函数:构造函数Person、Display函数(显示数据成员信息)和析构函数;由Person类派生出大学生类CollegeStu,其属性有专业major(指针类型),C++程序设计课程成绩score(double型),编写构造函数(实现数据初始化)、输出函数Display(包括name,id,major,score)。
代码修改后运行效果
Person::Person(const char *name, int id)
{
int n = strlen(name);
this->name = new char[n+1];
strcpy(this->name,name);
this->id = id;
}
void Person::Display()
{
cout<<"Person name:"<<name<<",id:"<<id<<endl;
}
CollegeStu::CollegeStu(const char *name,int id,const char *major,int score) : Person(name,id)
{
int n = strlen(major);
this->major = new char[n+1];
strcpy(this->major,major);
this->score = score;
}
void CollegeStu::Display()
{
Person::Display();
cout<<"major:"<<major<<endl;
cout<<"score:"<<score<<endl;
}