用C++编写程序用类的组合技术,设计一个基本信息类、一个学生类、一个教师类。基本信息类包括姓名、性别、手机号、班级;学生类包括姓名、性别、手机号、班级、数学、语文和英语等成绩;教师类包括姓名、性别、手机号、班级、专业、教龄等。以上三个类必须包含构造函数,其他成员函数自行设计。主函数中,分别生成一个学生类和教师类对象,并调用成员函数。
时间: 2023-10-20 12:59:25 浏览: 81
好的,下面是用 C++ 编写的基本信息类、学生类和教师类的代码实现:
```c++
#include <iostream>
#include <string>
using namespace std;
class BasicInfo {
public:
BasicInfo(string n, string g, string p, string c) {
name = n;
gender = g;
phone_number = p;
clazz = c;
}
string name;
string gender;
string phone_number;
string clazz;
};
class Student {
public:
Student(string n, string g, string p, string c, int math, int chinese, int english) {
basic_info = BasicInfo(n, g, p, c);
math_score = math;
chinese_score = chinese;
english_score = english;
}
BasicInfo basic_info;
int math_score;
int chinese_score;
int english_score;
int get_total_score() {
return math_score + chinese_score + english_score;
}
};
class Teacher {
public:
Teacher(string n, string g, string p, string c, string m, int y) {
basic_info = BasicInfo(n, g, p, c);
major = m;
teaching_years = y;
}
BasicInfo basic_info;
string major;
int teaching_years;
int get_teaching_age() {
return teaching_years;
}
};
int main() {
Student student1("张三", "男", "13888888888", "高一一班", 90, 80, 85);
cout << student1.basic_info.name << endl;
cout << student1.get_total_score() << endl;
Teacher teacher1("李老师", "女", "13999999999", "高一一班", "数学", 5);
cout << teacher1.basic_info.name << endl;
cout << teacher1.get_teaching_age() << endl;
return 0;
}
```
在主函数中,我们生成了一个学生类和一个教师类的对象,并调用了它们的成员函数,输出它们的信息。
输出结果为:
```
张三
255
李老师
5
```
我们可以看到,基本信息类、学生类和教师类都已经成功地定义了,并且我们能够通过成员函数获取它们的信息。
阅读全文