Use inheritance and derivation to write a student and teacher data information input/output program. Define a Person class and its subclasses Teacher and Student.The detailed requirements are: (1) Define a Person class that has data member named id and name.Member functions: a) Define the input() function to input the information, and output() function to output the infor mation. b) Both the input() and output() functions are required to be virtual. (2) The Student class is a subclass of the Person class with data member id, name, grade, and score. Please implement Student class. (3) The Teacher class is a subclass of the Person class with data member id, name, position, and de partment.Please implement Teacher class. (4) Complete the call in the main() function, including the input and output of student and teacher information
时间: 2024-03-06 08:46:28 浏览: 114
Here is an implementation in C++:
```
#include<iostream>
#include<string>
using namespace std;
class Person {
protected:
string id;
string name;
public:
virtual void input() {
cout << "Enter ID and name: ";
cin >> id >> name;
}
virtual void output() {
cout << "ID: " << id << endl;
cout << "Name: " << name << endl;
}
};
class Student : public Person {
protected:
string grade;
int score;
public:
void input() {
Person::input();
cout << "Enter grade and score: ";
cin >> grade >> score;
}
void output() {
Person::output();
cout << "Grade: " << grade << endl;
cout << "Score: " << score << endl;
}
};
class Teacher : public Person {
protected:
string position;
string department;
public:
void input() {
Person::input();
cout << "Enter position and department: ";
cin >> position >> department;
}
void output() {
Person::output();
cout << "Position: " << position << endl;
cout << "Department: " << department << endl;
}
};
int main() {
Student s;
s.input();
s.output();
Teacher t;
t.input();
t.output();
return 0;
}
```
In this implementation, we define a `Person` class and its subclasses `Student` and `Teacher`. The `Person` class has data members `id` and `name`, and virtual member functions `input()` and `output()`. The `Student` class is a subclass of `Person` and has additional data members `grade` and `score`. The `Teacher` class is also a subclass of `Person` and has additional data members `position` and `department`.
In the `main()` function, we create objects of `Student` and `Teacher`, call their `input()` and `output()` functions to get and display their information.
阅读全文