1、声明一个学生类,包含学生的学号、姓名、性别、C++成绩;声明一个教师类,包含教工号、姓名、职称;声明一个领导类,包含领导的工号、姓名和职位;实例化每个类的对象一个,声明一个show函数为这三个类的友元函数,分别显示其详细信息。
时间: 2024-01-13 15:05:56 浏览: 29
好的,以下是代码实现:
```c++
#include <iostream>
#include <string>
using namespace std;
// 学生类
class Student {
private:
int id; // 学号
string name; // 姓名
string gender; // 性别
double cppScore; // C++成绩
public:
Student(int id, string name, string gender, double cppScore) {
this->id = id;
this->name = name;
this->gender = gender;
this->cppScore = cppScore;
}
friend void show(Student stu, Teacher tea, Leader leader);
};
// 教师类
class Teacher {
private:
int id; // 教工号
string name; // 姓名
string title; // 职称
public:
Teacher(int id, string name, string title) {
this->id = id;
this->name = name;
this->title = title;
}
friend void show(Student stu, Teacher tea, Leader leader);
};
// 领导类
class Leader {
private:
int id; // 工号
string name; // 姓名
string position; // 职位
public:
Leader(int id, string name, string position) {
this->id = id;
this->name = name;
this->position = position;
}
friend void show(Student stu, Teacher tea, Leader leader);
};
// 友元函数,显示三个类的详细信息
void show(Student stu, Teacher tea, Leader leader) {
cout << "学生信息:" << endl;
cout << "学号:" << stu.id << endl;
cout << "姓名:" << stu.name << endl;
cout << "性别:" << stu.gender << endl;
cout << "C++成绩:" << stu.cppScore << endl;
cout << "教师信息:" << endl;
cout << "教工号:" << tea.id << endl;
cout << "姓名:" << tea.name << endl;
cout << "职称:" << tea.title << endl;
cout << "领导信息:" << endl;
cout << "工号:" << leader.id << endl;
cout << "姓名:" << leader.name << endl;
cout << "职位:" << leader.position << endl;
}
int main() {
Student stu(20210001, "小明", "男", 90.5);
Teacher tea(10001, "张老师", "教授");
Leader leader(2001, "李领导", "部门主管");
show(stu, tea, leader);
return 0;
}
```
以上代码定义了三个类:学生类、教师类和领导类,每个类中包含一些属性和构造函数,同时定义了一个友元函数show,该函数可以访问三个类的对象的私有属性,用来显示详细信息。在主函数中实例化了一个学生对象、一个教师对象和一个领导对象,然后调用show函数显示它们的详细信息。