定义一个在校人员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 #include using namespace std;
class Person { protected: 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 << endl; cout << "Sex: " << sex << endl; cout << "Age: " << age << endl; } };
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() { cout << "Name: " << name << endl; cout << "Sex: " << sex << endl; cout << "Age: " << age << endl; cout << "Number: " << num << endl; cout << "Score: " << score << endl; } };
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; }