定义一个基类有姓名、性别、年龄,再由基类派生出教师类和学生类。教师类增加工号、职称和工资,学生类增加学号、班级、专业和入学成绩。
编程要求:
1、设计虚函数输出教师和学生的基本信息;
2、计算教师的平均工资并输出;
3、使用友元函数重载运算符“、”,找出入学成绩最高的学生并输出;
#include <iostream>
#include <string>
using namespace std;
class Person
{
protected:
string name;
string gender;
int age;
public:
Person(string n, string g, int a)
{
name = n;
gender = g;
age = a;
}
virtual void showInfo()
{
cout << "Name: " << name << "\nGender: " << gender << "\nAge: " << age << endl;
}
};
class Teacher : public Person
{
private:
int empID;
string title;
int salary;
public:
Teacher(string n, string g, int a, int id, string t, int s) : Person(n,g,a)
{
empID = id;
title = t;
salary = s;
}
void showInfo() override
{
Person::showInfo();
cout << "Employee ID: " << empID << "\nTitle: " << title << "\nSalary: " << salary << endl;
}
friend double averageSalary(Teacher *tArray, int size);
};
double averageSalary(Teacher *tArray, int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += tArray[i].salary;
}
return (double)sum / size;
}
class Student : public Person
{
private:
int stuID;
string className;
string major;
int score;
public:
Student(string n, string g, int a, int id, string c, string m, int s) : Person(n,g,a)
{
stuID = id;
className = c;
major = m;
score = s;
}
void showInfo() override
{
Person::showInfo();
cout << "Student ID: " << stuID << "\nClass Name: " << className << "\nMajor: " << major << "\nScore: " << score << endl;
}
friend Student operator<(Student& s1, Student& s2);
};
Student operator<(Student& s1, Student& s2)
{
if (s1.score > s2.score)
{
return s1;
}
else
{
return s2;
}
}
int main()
{
// 初始化教师和学生
Teacher t1("John", "Male", 35, 1001, "Associate Professor", 80000);
Teacher t2("Amy", "Female", 45, 1002, "Professor", 100000);
Teacher t3("David", "Male", 28, 1003, "Lecturer", 60000);
Student s1("Tom", "Male", 20, 20231001, "Class 2", "Computer Science", 90);
Student s2("Lucy", "Female", 19, 20231002, "Class 2", "Software Engineering", 95);
Student s3("Peter", "Male", 21, 20231003, "Class 1", "Information Management", 88);
// 输出所有人的信息
cout << "Teacher 1 information:" << endl;
t1.showInfo();
cout << endl;
cout << "Teacher 2 information:" << endl;
t2.showInfo();
cout << endl;
cout << "Teacher 3 information:" << endl;
t3.showInfo();
cout << endl;
cout << "Student 1 information:" << endl;
s1.showInfo();
cout << endl;
cout << "Student 2 information:" << endl;
s2.showInfo();
cout << endl;
cout << "Student 3 information:" << endl;
s3.showInfo();
cout << endl;
// 计算所有教师的平均工资
Teacher tArray[3] = {t1,t2,t3};
double avgSalary = averageSalary(tArray, 3);
cout << "The average salary of all teachers is: " << avgSalary << endl;
// 找到入学成绩最高的学生
Student result = s1 < s2 < s3;
cout << "The student with the highest exam score is: " << result.name << endl;
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:思路:
本题采用二维数组来存放4个学生的5门课程,定义int a[4][5],在编写函数时,我们需要使用指针进行遍历.
大家都了解一维数组转换成指针是这样的:a[4]等价*(a+4)
那么二维数组的转换方式就是a[4][5]等价*(*(a+4)+5)
大家只要理解了这个转换方式,然后分别在所编写的函数中使用指针的方式替换数组,这道题就解出来了,求平均分和每科成绩都不难,定义两个辅助变量进行判断即可。