编写一个程序实现教师和学生数据输入和输出,学生数据有学号、姓名和性别,教师数据有编号、姓名、性别、职称和部门,教师类Teacher继承学生类Student,分别用三种不同的继承方式实现
时间: 2024-03-22 19:40:50 浏览: 35
好的,我可以帮你实现这个程序。下面分别使用三种不同的继承方式实现:
1. 公有继承
```
#include <iostream>
#include <string>
using namespace std;
// 学生类
class Student{
public:
string number; // 学号
string name; // 姓名
string gender; // 性别
void input(){
cout << "请输入学号:";
cin >> number;
cout << "请输入姓名:";
cin >> name;
cout << "请输入性别:";
cin >> gender;
}
void output(){
cout << "学号:" << number << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
}
};
// 教师类
class Teacher: public Student{
public:
string id; // 编号
string title; // 职称
string department; // 部门
void input(){
Student::input();
cout << "请输入编号:";
cin >> id;
cout << "请输入职称:";
cin >> title;
cout << "请输入部门:";
cin >> department;
}
void output(){
Student::output();
cout << "编号:" << id << endl;
cout << "职称:" << title << endl;
cout << "部门:" << department << endl;
}
};
int main(){
Teacher t;
t.input();
t.output();
return 0;
}
```
2. 私有继承
```
#include <iostream>
#include <string>
using namespace std;
// 学生类
class Student{
public:
string number; // 学号
string name; // 姓名
string gender; // 性别
void input(){
cout << "请输入学号:";
cin >> number;
cout << "请输入姓名:";
cin >> name;
cout << "请输入性别:";
cin >> gender;
}
void output(){
cout << "学号:" << number << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
}
};
// 教师类
class Teacher: private Student{
public:
string id; // 编号
string title; // 职称
string department; // 部门
void input(){
Student::input();
cout << "请输入编号:";
cin >> id;
cout << "请输入职称:";
cin >> title;
cout << "请输入部门:";
cin >> department;
}
void output(){
Student::output();
cout << "编号:" << id << endl;
cout << "职称:" << title << endl;
cout << "部门:" << department << endl;
}
// 访问Student类中的成员函数和成员变量
using Student::input;
using Student::output;
using Student::number;
using Student::name;
using Student::gender;
};
int main(){
Teacher t;
t.input();
t.output();
cout << "学号:" << t.number << endl; // 编译错误,无法直接访问Student类中的成员变量
return 0;
}
```
3. 保护继承
```
#include <iostream>
#include <string>
using namespace std;
// 学生类
class Student{
protected:
string number; // 学号
string name; // 姓名
string gender; // 性别
public:
void input(){
cout << "请输入学号:";
cin >> number;
cout << "请输入姓名:";
cin >> name;
cout << "请输入性别:";
cin >> gender;
}
void output(){
cout << "学号:" << number << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
}
};
// 教师类
class Teacher: protected Student{
public:
string id; // 编号
string title; // 职称
string department; // 部门
void input(){
Student::input();
cout << "请输入编号:";
cin >> id;
cout << "请输入职称:";
cin >> title;
cout << "请输入部门:";
cin >> department;
}
void output(){
Student::output();
cout << "编号:" << id << endl;
cout << "职称:" << title << endl;
cout << "部门:" << department << endl;
}
// 访问Student类中的成员函数和成员变量
using Student::input;
using Student::output;
using Student::number;
using Student::name;
using Student::gender;
};
int main(){
Teacher t;
t.input();
t.output();
cout << "学号:" << t.number << endl; // 编译错误,无法直接访问Student类中的成员变量
return 0;
}
```
以上三种方式均可以实现教师和学生数据输入和输出。