定义一个基类有姓名、性别、年龄,再由基类派生出教师类和学生类。教师类增加工号、职称和工资,学生类增加学号、班级、专业和入学成绩。
编程要求:
1、设计虚函数输出教师和学生的基本信息;
2、计算教师的平均工资并输出;
3、 使用友元函数重载运算符“<”,找出入学成绩最高的学生并输出;
#include <stdio.h>
int main(void)
{
int n = 1, s = 0;
while (s != 300)
{
n++;
s = s + (n - 1); //握手定理
}
printf("%d\n", n);
return 0;
}
我可以回答这个问题。
这个问题需要定义一个基类Person,派生出教师类Teacher和学生类Student,然后完成输出教师和学生的基本信息、计算教师的平均工资并输出、使用友元函数重载运算符“<”,找出入学成绩最高的学生并输出。
首先是定义基类Person,里面包含姓名和年龄两个私有成员变量,以及输出基本信息的公有虚函数printInfo:
class Person {
private:
string name;
int age;
public:
Person(string _name, int _age): name(_name), age(_age) {}
virtual ~Person() {}
virtual void printInfo() {}
};
然后是定义教师类Teacher,它是Person类的一个派生类,多了一个工资成员变量salary,需要实现输出基本信息、计算平均工资的函数printInfo和getAverageSalary:
class Teacher: public Person {
private:
int salary;
public:
Teacher(string _name, int _age, int _salary): Person(_name, _age), salary(_salary) {}
~Teacher() {}
void printInfo() override {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "工资:" << salary << endl;
}
int getSalary() const {
return salary;
}
friend bool operator<(const Teacher& lhs, const Teacher& rhs) {
return lhs.salary < rhs.salary;
}
};
bool salaryCompare(const Teacher& t1, const Teacher& t2) {
return t1.getSalary() < t2.getSalary();
}
float getAverageSalary(const vector<Teacher>& teachers) {
float sum = 0.0;
for (const auto& teacher : teachers) {
sum += teacher.getSalary();
}
return sum / teachers.size();
}
最后是定义学生类Student,它是Person类的一个派生类,单独多了一个成绩成员变量score,需要实现输出基本信息、比较成绩大小的函数printInfo和operator<:
class Student: public Person {
private:
int score;
public:
Student(string _name, int _age, int _score): Person(_name, _age), score(_score) {}
~Student() {}
void printInfo() override {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "成绩:" << score << endl;
}
int getScore() const {
return score;
}
friend bool operator<(const Student& lhs, const Student& rhs) {
return lhs.score < rhs.score;
}
};
bool scoreCompare(const Student& s1, const Student& s2) {
return s1.getScore() < s2.getScore();
}
const Student& getHighestScore(const vector<Student>& students) {
return *max_element(students.begin(), students.end(), scoreCompare);
}
这样就完成了这个问题的解决。完整代码附在下方:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person(string _name, int _age): name(_name), age(_age) {}
virtual ~Person() {}
virtual void printInfo() {}
};
class Teacher: public Person {
private:
int salary;
public:
Teacher(string _name, int _age, int _salary): Person(_name, _age), salary(_salary) {}
~Teacher() {}
void printInfo() override {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "工资:" << salary << endl;
}
int getSalary() const {
return salary;
}
friend bool operator<(const Teacher& lhs, const Teacher& rhs) {
return lhs.salary < rhs.salary;
}
};
bool salaryCompare(const Teacher& t1, const Teacher& t2) {
return t1.getSalary() < t2.getSalary();
}
float getAverageSalary(const vector<Teacher>& teachers) {
float sum = 0.0;
for (const auto& teacher : teachers) {
sum += teacher.getSalary();
}
return sum / teachers.size();
}
class Student: public Person {
private:
int score;
public:
Student(string _name, int _age, int _score): Person(_name, _age), score(_score) {}
~Student() {}
void printInfo() override {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "成绩:" << score << endl;
}
int getScore() const {
return score;
}
friend bool operator<(const Student& lhs, const Student& rhs) {
return lhs.score < rhs.score;
}
};
bool scoreCompare(const Student& s1, const Student& s2) {
return s1.getScore() < s2.getScore();
}
const Student& getHighestScore(const vector<Student>& students) {
return *max_element(students.begin(), students.end(), scoreCompare);
}
int main() {
vector<Teacher> teachers;
vector<Student> students;
teachers.push_back(Teacher("Alice", 30, 3000));
teachers.push_back(Teacher("Bob", 35, 4000));
teachers.push_back(Teacher("Charlie", 40, 5000));
students.push_back(Student("David", 18, 90));
students.push_back(Student("Eli", 19, 85));
students.push_back(Student("Frank", 20, 95));
cout << "教师信息:" << endl;
for (const auto& teacher : teachers) {
teacher.printInfo();
cout << endl;
}
cout << "教师平均工资:" << getAverageSalary(teachers) << endl;
cout << "入学成绩最高的学生:" << endl;
const Student& highestScoreStudent = getHighestScore(students);
highestScoreStudent.printInfo();
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
class Person {
protected:
string name; // 姓名
string gender; // 性别
int age; // 年龄
public:
Person(string name, string gender, int age) : name(name), gender(gender), age(age) {}
virtual void printInfo() {
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
cout << "年龄:" << age << "岁" << endl;
}
};
class Teacher : public Person {
private:
int employeeID; // 工号
string title; // 职称
double salary; // 工资
public:
Teacher(string name, string gender, int age, int employeeID, string title, double salary)
: Person(name, gender, age), employeeID(employeeID), title(title), salary(salary) {}
void printInfo() override {
Person::printInfo();
cout << "工号:" << employeeID << endl;
cout << "职称:" << title << endl;
cout << "工资:" << salary << "元" << endl;
}
double getSalary() const {
return salary;
}
};
class Student : public Person {
private:
int studentID; // 学号
string className; // 班级
string major; // 专业
double entranceScore; // 入学成绩
public:
Student(string name, string gender, int age, int studentID, string className, string major, double entranceScore)
: Person(name, gender, age), studentID(studentID), className(className), major(major), entranceScore(entranceScore) {}
void printInfo() override {
Person::printInfo();
cout << "学号:" << studentID << endl;
cout << "班级:" << className << endl;
cout << "专业:" << major << endl;
cout << "入学成绩:" << entranceScore << endl;
}
double getEntranceScore() const {
return entranceScore;
}
friend bool operator<(const Student& s1, const Student& s2);
};
bool operator<(const Student& s1, const Student& s2) {
return s1.entranceScore < s2.entranceScore;
}
int main() {
vector<Teacher> teachers;
vector<Student> students;
teachers.push_back(Teacher("张三", "男", 40, 1001, "教授", 5000));
teachers.push_back(Teacher("李四", "女", 35, 1002, "副教授", 4000));
students.push_back(Student("车二愣子", "男", 20, 2001, "A班", "计算机科学", 25));
students.push_back(Student("小红", "女", 21, 2002, "B班", "机械工程", 90));
students.push_back(Student("小明", "男", 19, 2003, "C班", "经济学", 95));
// 输出教师和学生的基本信息
cout << "教师信息:" << endl;
for (const auto& teacher : teachers) {
teacher.printInfo();
cout << endl;
}
cout << "学生信息:" << endl;
for (const auto& student : students) {
student.printInfo();
cout << endl;
}
// 计算教师的平均工资并输出
double sumSalary = 0;
for (const auto& teacher : teachers) {
sumSalary += teacher.getSalary();
}
double avgSalary = sumSalary / teachers.size();
cout << "教师平均工资:" << avgSalary << "元" << endl;
// 找出入学成绩最高的学生并输出
const Student& highestScoreStudent = *max_element(students.begin(), students.end());
cout << "入学成绩最高的学生:" << endl;
highestScoreStudent.printInfo();
return 0;
}