用类的组合技术,设计一个基本信息类、一个学生类、一个教师类。基本信息类包括姓名、性别、手机号、班级;学生类包括姓名、性别、手机号、班级、数学、语文和英语等成绩;教师类包括姓名、性别、手机号、班级、专业、教龄等。以上三个类必须包含构造函数,其他成员函数自行设计。主函数中,分别生成一个学生类和教师类对象,并调用成员函数。
#include <iostream>
#include <string>
using namespace std;
class BasicInfo {
private:
string name;
string gender;
string phoneNumber;
string className;
public:
BasicInfo(const string& n, const string& g, const string& phone, const string& cls)
: name(n), gender(g), phoneNumber(phone), className(cls) {}
void displayInfo() {
cout << "Name: " << name << endl;
cout << "Gender: " << gender << endl;
cout << "Phone Number: " << phoneNumber << endl;
cout << "Class: " << className << endl;
}
};
class Student {
private:
BasicInfo basicInfo;
int mathScore;
int chineseScore;
int englishScore;
public:
Student(const string& n, const string& g, const string& phone, const string& cls,
int math, int chinese, int english)
: basicInfo(n, g, phone, cls),
mathScore(math),
chineseScore(chinese),
englishScore(english) {}
void displayInfo() {
basicInfo.displayInfo();
cout << "Math Score: " << mathScore << endl;
cout << "Chinese Score: " << chineseScore << endl;
cout << "English Score: " << englishScore << endl;
}
};
class Teacher {
private:
BasicInfo basicInfo;
string major;
int teachingYears;
public:
Teacher(const string& n, const string& g, const string& phone, const string& cls,
const string& m, int years)
: basicInfo(n, g, phone, cls),
major(m),
teachingYears(years) {}
void displayInfo() {
basicInfo.displayInfo();
cout << "Major: " << major << endl;
cout << "Teaching Years: " << teachingYears << endl;
}
};
int main() {
Student student("John Doe", "Male", "1234567890", "Class A", 90, 85, 95);
Teacher teacher("Jane Smith", "Female", "9876543210", "Class B", "Mathematics", 10);
cout << "Student Information:" << endl;
student.displayInfo();
cout << endl;
cout << "Teacher Information:" << endl;
teacher.displayInfo();
cout << endl;
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:#include <iostream>
#include <string>
using namespace std;
class BasicInfo {
protected:
string name;
string gender;
string phone;
string class_info;
public:
BasicInfo(string n, string g, string p, string c): name(n), gender(g), phone(p), class_info(c) {}
BasicInfo() {}
// get methods
string get_name() const { return name; }
string get_gender() const { return gender; }
string get_phone() const { return phone; }
string get_class_info() const { return class_info; }
// set methods
void set_name(string n) { name = n; }
void set_gender(string g) { gender = g; }
void set_phone(string p) { phone = p; }
void set_class_info(string c) { class_info = c; }
virtual void display_info() const {
cout << "Name: " << name << endl;
cout << "Gender: " << gender << endl;
cout << "Phone: " << phone << endl;
cout << "Class info: " << class_info << endl;
}
};
class Student: public BasicInfo {
protected:
int math_grade;
int chinese_grade;
int english_grade;
public:
Student(string n, string g, string p, string c, int m, int ch, int e): BasicInfo(n, g, p, c), math_grade(m), chinese_grade(ch), english_grade(e) {}
Student() {}
// get methods
int get_math_grade() const { return math_grade; }
int get_chinese_grade() const { return chinese_grade; }
int get_english_grade() const { return english_grade; }
// set methods
void set_math_grade(int m) { math_grade = m; }
void set_chinese_grade(int ch) { chinese_grade = ch; }
void set_english_grade(int e) { english_grade = e; }
void display_info() const override {
BasicInfo::display_info();
cout << "Math grade: " << math_grade << endl;
cout << "Chinese grade: " << chinese_grade << endl;
cout << "English grade: " << english_grade << endl;
}
};
class Teacher: public BasicInfo {
protected:
string major;
int teaching_years;
public:
Teacher(string n, string g, string p, string c, string m, int t): BasicInfo(n, g, p, c), major(m), teaching_years(t) {}
Teacher() {}
// get methods
string get_major() const { return major; }
int get_teaching_years() const { return teaching_years; }
// set methods
void set_major(string m) { major = m; }
void set_teaching_years(int t) { teaching_years = t; }
void display_info() const override {
BasicInfo::display_info();
cout << "Major: " << major << endl;
cout << "Teaching years: " << teaching_years << endl;
}
};
int main() {
Student s("John", "male", "123456789", "Class A", 85, 90, 95);
Teacher t("Mary", "female", "987654321", "Class B", "Mathematics", 10);
s.display_info();
cout << endl;
t.display_info();
return 0;
}
解释: 首先定义一个基础信息类 BasicInfo
,包括姓名、性别、手机号和班级信息。然后对于学生类和教师类,都要继承这个基础信息类,再在其基础上加上各自特有的成员变量和成员函数。
构造函数直接使用参数初始化列表对所有基础信息成员变量进行初始化。在基础信息类中提供了 get 和 set 方法,可以用来获取和修改相应的成员变量。
在 Student
类中,还包括数学、语文和英语成绩,而在 Teacher
类中,还包括专业和教龄信息。由于每个类都需要实现构造函数以及其他成员函数,所以在这里我们为它们都提供了默认构造函数和拷贝构造函数,同时为每个类提供了 display_info()
方法来打印所有成员变量的值。
在 main
函数中定义了一个学生类对象和一个教师类对象,分别初始化完毕后调用它们的 display_info()
方法输出成员变量的值。