定义一个在校人员Person类包含成员姓名(字符串),性别(,女用字符w或W,男用字符m或M),年龄(大于0的整数)和构造函数及show成员函数;以Person作为基类派生出学生…
- User
定义一个在校人员Person类包含成员姓名(字符串),性别(,女用字符w或W,男用字符m或M),年龄(大于0的整数)和构造函数及show成员函数;以Person作为基类派生出学生Student类扩展成员学号(大于0的整数),成绩(实数)成员和构造函数及show成员函数;请实例化两个类的对象,然后再分别显示出成员信息。 参考主函数: int main() { string name; char sex; int age, num; float score; cin >> num >> name >> sex >> age >> score; Person p(name, sex, age); p.show(); cout << endl; Student s(num, name, sex, age, score); s.show(); return 0;
}C++编译
- Assistant
#include using namespace std;
class Person { private: string name; char sex; int age; public: Person(string n, char s, int a) { name = n; sex = s; age = a; } void show() { cout << "Name: " << name << "\nSex: " << sex << "\nAge: " << age; } };
class Student : public Person { private: int num; float score; public: Student(int n, string name, char sex, int age, float s) : Person(name, sex, age) { num = n; score = s; } void show() { Person::show(); cout << "\nNumber: " << num << "\nScore: " << score; } };
int main() { string name; char sex; int age, num; float score; cin >> num >> name >> sex >> age >> score; Person p(name, sex, age); p.show(); cout << endl; Student s(num, name, sex, age, score); s.show(); return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.