由Person类派生出大学生CollegeStu类。设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,编写成员函数:构造函数Person、Display函数(显示数据成员信息)和析构函数;由Person类派生出大学生类CollegeStu,其属性有专业major(指针类型),C++程序设计课程成绩score(double型),编写构造函数(实现数据初始化)、输出函数Display(包括name,id,major,score)。
代码修改后运行效果
参考如下:
#include <iostream>
#include <cstring>
using namespace std;
class Person{
private:
const char *name; // 字符型指针
int id;
public:
Person(const char *name, int id) {
this -> name = name;
this -> id = id;
}
void Display()
{
cout << "Person name: " << name << " , id:" << id << endl;
}
~Person() {}
};
class CollegeStu:public Person{
private:
const char *major; // 字符型指针
double score;
public:
CollegeStu(const char *name, int id, const char *nMajor, double nScore):Person(name, id),major(nMajor),score(nScore)
{
}
void Display()
{
cout << "College Student Info:" << endl;
Person::Display();
cout << "major: " << major << endl;
cout << "C++ Score:" << score << endl;
}
~CollegeStu(){}
};
//以下不许改动
int main(){
Person pdemo("John",123);
pdemo.Display();
CollegeStu csDemo("Rose",456,"Software",96);
csDemo.Display();
}
如有帮助,欢迎采纳哈!