声明一个学生类,包含学生的学号、姓名、性别、C++成绩;声明一个教师类,包含教工号、姓名、职称;声明一个领导类,包含领导的工号、姓名和职位;实例化每个类的对象一个,声明一个show函数为这三个类的友元函数,分别显示其详细信息。
你好,你的问题解析如下,供参考:
#include <iostream>
#include <string>
using namespace std;
//声明一个学生类
class Student {
private:
int id; // 学号
string name; // 姓名
char gender; // 性别
double cpp_score; // C++成绩
public:
//构造函数,初始化学生的信息
Student(int i, string n, char g, double s) {
id = i;
name = n;
gender = g;
cpp_score = s;
}
//友元函数,显示学生的详细信息
friend void show(Student s);
};
//声明一个教师类
class Teacher {
private:
int tid; // 教工号
string name; // 姓名
string title; // 职称
public:
//构造函数,初始化教师的信息
Teacher(int i, string n, string t) {
tid = i;
name = n;
title = t;
}
//友元函数,显示教师的详细信息
friend void show(Teacher t);
};
//声明一个领导类
class Leader {
private:
int lid; // 领导的工号
string name; // 姓名
string position; // 职位
public:
//构造函数,初始化领导的信息
Leader(int i, string n, string p) {
lid = i;
name = n;
position = p;
}
//友元函数,显示领导的详细信息
friend void show(Leader l);
};
//定义一个show函数,为三个类的友元函数,分别显示其详细信息
void show(Student s) {
cout << "学生的信息如下:" << endl;
cout << "学号:" << s.id << endl;
cout << "姓名:" << s.name << endl;
cout << "性别:" << s.gender << endl;
cout << "C++成绩:" << s.cpp_score << endl;
}
void show(Teacher t) {
cout << "教师的信息如下:" << endl;
cout << "教工号:" << t.tid << endl;
cout << "姓名:" << t.name << endl;
cout << "职称:" << t.title << endl;
}
void show(Leader l) {
cout << "领导的信息如下:" << endl;
cout << "工号:" << l.lid << endl;
cout << "姓名:" << l.name << endl;
cout << "职位:" << l.position << endl;
}
int main() {
//实例化每个类的对象一个
Student s1(2021001, "张三", 'M', 95.5);
Teacher t1(1001, "李四", "副教授");
Leader l1(1, "王五", "院长");
//调用show函数,显示每个对象的详细信息
show(s1);
show(t1);
show(l1);
return 0;
}
void show()
{
system("cls");
order2();
FILE *fp;
int i,m=0,c=0,m1=0,e=0;
fp=fopen("data.txt","rb");
while(!feof(fp))
{
if(fread(&stu[m],LEN,1,fp)==1)
m++;
}
fclose(fp);
printf("|学号 |姓名 |学年 |班级 |语文 |数学 |英语 |总分 |平均分 |\t\n");
for(i=0;i<m;i++)
{
printf(FORMART,DATA);
}
}
#include<iostream>
#include<string>
using namespace std;
class Teacher; //提前声明教师类
class Student //学生类
{
friend void show(Student stu, Teacher tea, Leader lea); //声明友元函数
private:
string stuid; //学号
string name; //姓名
string sex; //性别
double score; //C++成绩
public:
Student(string id, string n, string s, double sc):stuid(id),name(n),sex(s),score(sc){} //构造函数
};
class Teacher //教师类
{
friend void show(Student stu, Teacher tea, Leader lea); //声明友元函数
private:
string techid; //教工号
string name; //姓名
string title; //职称
public:
Teacher(string id, string n, string t):techid(id),name(n),title(t){} //构造函数
};
class Leader //领导类
{
friend void show(Student stu, Teacher tea, Leader lea); //声明友元函数
private:
string leaderid; //工号
string name; //姓名
string position; //职位
public:
Leader(string id, string n, string p):leaderid(id),name(n),position(p){} //构造函数
};
void show(Student stu, Teacher tea, Leader lea) //友元函数
{
cout<<"学生信息:"<<endl;
cout<<"学号:"<<stu.stuid<<" 姓名:"<<stu.name<<" 性别:"<<stu.sex<<" C++成绩:"<<stu.score<<endl;
cout<<"教师信息:"<<endl;
cout<<"教工号:"<<tea.techid<<" 姓名:"<<tea.name<<" 职称:"<<tea.title<<endl;
cout<<"领导信息:"<<endl;
cout<<"工号:"<<lea.leaderid<<" 姓名:"<<lea.name<<" 职位:"<<lea.position<<endl;
}
int main()
{
Student stu("001", "张三", "男", 90); //实例化学生类对象
Teacher tea("002", "李四", "教授"); //实例化教师类对象
Leader lea("003", "王五", "院长"); //实例化领导类对象
show(stu, tea, lea); //调用友元函数
return 0;
}