C++定义个人信息类Person,其数据成员有姓名、性别、出生年月。并以Person为基类定义一个学生的派生类Student,增加描述学生的信息:班级、学号、专业、英语成绩和数学成绩。再由基类Person定义一个职工的派生类Employee,增加描述职工的信息:部门、职 务、工资。编程实现学生与职工信息的输入与输出
时间: 2023-06-04 07:08:32 浏览: 496
:好的,我能够理解您的问题。以下是对应的代码实现:
```c++
#include<iostream>
#include<string>
using namespace std;
class Person{
protected:
string name;
char sex;
int birthYear, birthMonth;
public:
Person(string name, char sex, int birthYear, int birthMonth){
this->name = name;
this->sex = sex;
this->birthYear = birthYear;
this->birthMonth = birthMonth;
}
void display(){
cout << "姓名:" << name << endl;
cout << "性别:" << sex << endl;
cout << "出生日期:" << birthYear << "年" << birthMonth << "月" << endl;
}
};
class Student : public Person{
protected:
string className;
string studentID;
string major;
double englishGrade;
double mathGrade;
public:
Student(string className, string studentID, string major, double englishGrade, double mathGrade, string name, char sex, int birthYear, int birthMonth): Person(name, sex, birthYear, birthMonth){
this->className = className;
this->studentID = studentID;
this->major = major;
this->englishGrade = englishGrade;
this->mathGrade = mathGrade;
}
void display(){
Person::display();
cout << "班级:" << className << endl;
cout << "学号:" << studentID << endl;
cout << "专业:" << major << endl;
cout << "英语成绩:" << englishGrade << endl;
cout << "数学成绩:" << mathGrade << endl;
}
};
class Employee : public Person{
protected:
string department;
string position;
double salary;
public:
Employee(string department, string position, double salary, string name, char sex, int birthYear, int birthMonth): Person(name, sex, birthYear, birthMonth){
this->department = department;
this->position = position;
this->salary = salary;
}
void display(){
Person::display();
cout << "部门:" << department << endl;
cout << "职务:" << position << endl;
cout << "工资:" << salary << endl;
}
};
int main(){
string name;
char sex;
int birthYear, birthMonth;
string className, studentID, major;
double englishGrade, mathGrade;
string department, position;
double salary;
cout << "请输入学生信息:" << endl;
cout << "姓名:";
cin >> name;
cout << "性别:";
cin >> sex;
cout << "出生年份:";
cin >> birthYear;
cout << "出生月份:";
cin >> birthMonth;
cout << "班级:";
cin >> className;
cout << "学号:";
cin >> studentID;
cout << "专业:";
cin >> major;
cout << "英语成绩:";
cin >> englishGrade;
cout << "数学成绩:";
cin >> mathGrade;
Student stu(className, studentID, major, englishGrade, mathGrade, name, sex, birthYear, birthMonth);
cout << endl << "学生信息如下:" << endl;
stu.display();
cout << endl << "请输入职工信息:" << endl;
cout << "姓名:";
cin >> name;
cout << "性别:";
cin >> sex;
cout << "出生年份:";
cin >> birthYear;
cout << "出生月份:";
cin >> birthMonth;
cout << "部门:";
cin >> department;
cout << "职务:";
cin >> position;
cout << "工资:";
cin >> salary;
Employee emp(department, position, salary, name, sex, birthYear, birthMonth);
cout << endl << "职工信息如下:" << endl;
emp.display();
return 0;
}
```
希望上述代码能够满足您的需求。
阅读全文