编写一个学生和职工数据输入和输出程序,学生数据要有学号、姓名、性别、专业、年级等,职工数据要有职工号、姓名、性别、年龄、部门、职称、工资等。要求将姓名、性别和数据输出设计成一个类person,学生类student和职工类employee,由类person来派生。要求程序完整(包含主函数)。
时间: 2023-06-13 20:04:47 浏览: 34
以下是一个简单的实现,具体可以根据实际需要进行修改:
```C++
#include <iostream>
#include <string>
using namespace std;
class Person{
public:
string name;
string gender;
virtual void display(){
cout<<"Name: "<<name<<endl;
cout<<"Gender: "<<gender<<endl;
}
};
class Student:public Person{
public:
string studentID;
string major;
string grade;
void display(){
Person::display();
cout<<"Student ID: "<<studentID<<endl;
cout<<"Major: "<<major<<endl;
cout<<"Grade: "<<grade<<endl;
}
};
class Employee:public Person{
public:
string employeeID;
int age;
string department;
string position;
float salary;
void display(){
Person::display();
cout<<"Employee ID: "<<employeeID<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Department: "<<department<<endl;
cout<<"Position: "<<position<<endl;
cout<<"Salary: "<<salary<<endl;
}
};
int main(){
Student s;
s.name = "Tom";
s.gender = "Male";
s.studentID = "1001";
s.major = "Computer Science";
s.grade = "Sophomore";
s.display();
cout<<endl;
Employee e;
e.name = "Mary";
e.gender = "Female";
e.employeeID = "2001";
e.age = 30;
e.department = "Human Resources";
e.position = "Manager";
e.salary = 5000;
e.display();
return 0;
}
```
输出结果为:
```
Name: Tom
Gender: Male
Student ID: 1001
Major: Computer Science
Grade: Sophomore
Name: Mary
Gender: Female
Employee ID: 2001
Age: 30
Department: Human Resources
Position: Manager
Salary: 5000
```