写出Person类派生大学生Collegestu类(1)。设计一个Person类,其属性包括姓名name和身份证号id,其中name 为string类型,记d世.为string型,编写成员函数:构造西数Person、Display西数(显示数据成员信息)和析构函数;由Person类派生出大学生类Collegestu,其属性有专北imajor (string类型),C+程序设计课程成绩score (double型),编写构造西数(实现数据初始化)、输出西数Display(包括name, id, major, score) 。 main的代码如下: (不允许改动) int main0 { string name, major; string id; double score; cin>>name>>id>>major>>score; CollegeStu cs(name,id,major,score); cs.DisplayO; return 0; Input Output Sample Input Zhangsan 210302198909120938 Software 99.5
时间: 2023-04-01 21:04:21 浏览: 215
设计一个学生类Student(学生学号、姓名、数学、英语、计算机成绩;)
5星 · 资源好评率100%
Person类派生大学生Collegestu类的代码如下:
```c++
#include <iostream>
#include <string>
using namespace std;
class Person {
protected:
string name;
string id;
public:
Person(string n, string i) : name(n), id(i) {}
virtual ~Person() {}
virtual void Display() {
cout << "Name: " << name << endl;
cout << "ID: " << id << endl;
}
};
class CollegeStu : public Person {
private:
string major;
double score;
public:
CollegeStu(string n, string i, string m, double s) : Person(n, i), major(m), score(s) {}
void Display() {
Person::Display();
cout << "Major: " << major << endl;
cout << "Score: " << score << endl;
}
};
int main() {
string name, major, id;
double score;
cin >> name >> id >> major >> score;
CollegeStu cs(name, id, major, score);
cs.Display();
return ;
}
```
输入样例:
```
Zhangsan 210302198909120938 Software 99.5
```
输出样例:
```
Name: Zhangsan
ID: 210302198909120938
Major: Software
Score: 99.5
```
阅读全文