采用面向对象思想,设计大学生信息管理系统相关类的定义和测试。要求为: 1)定义大学生基类; 2)具有构造函数,析构函数; 3)定义派生类测控学生、机自学生、过控学生,包装学生; 4)定义类数据成员,实现数据封装; 5)定义虚函数实现多态性:信息输入输出、析构等。
时间: 2024-03-15 14:44:29 浏览: 7
好的,以下是一个参考实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
// 大学生基类
class Student {
private:
string name; // 姓名
int age; // 年龄
string id; // 学号
public:
Student(string name, int age, string id) : name(name), age(age), id(id) {}
virtual ~Student() {}
// 获取姓名、年龄、学号
string getName() const { return name; }
int getAge() const { return age; }
string getId() const { return id; }
// 设置姓名、年龄、学号
void setName(string name) { this->name = name; }
void setAge(int age) { this->age = age; }
void setId(string id) { this->id = id; }
// 信息输入输出
virtual void input() {
cout << "姓名:";
cin >> name;
cout << "年龄:";
cin >> age;
cout << "学号:";
cin >> id;
}
virtual void output() const {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "学号:" << id << endl;
}
};
// 测控学生
class MeasurementStudent : public Student {
private:
int experimentCount; // 实验次数
public:
MeasurementStudent(string name, int age, string id, int experimentCount) : Student(name, age, id), experimentCount(experimentCount) {}
// 获取实验次数
int getExperimentCount() const { return experimentCount; }
// 设置实验次数
void setExperimentCount(int experimentCount) { this->experimentCount = experimentCount; }
// 信息输入输出
void input() {
Student::input();
cout << "实验次数:";
cin >> experimentCount;
}
void output() const {
Student::output();
cout << "实验次数:" << experimentCount << endl;
}
};
// 机自学生
class AutomaticControlStudent : public Student {
private:
string researchArea; // 研究领域
public:
AutomaticControlStudent(string name, int age, string id, string researchArea) : Student(name, age, id), researchArea(researchArea) {}
// 获取研究领域
string getResearchArea() const { return researchArea; }
// 设置研究领域
void setResearchArea(string researchArea) { this->researchArea = researchArea; }
// 信息输入输出
void input() {
Student::input();
cout << "研究领域:";
cin >> researchArea;
}
void output() const {
Student::output();
cout << "研究领域:" << researchArea << endl;
}
};
// 过控学生
class ProcessControlStudent : public Student {
private:
double score; // 成绩
public:
ProcessControlStudent(string name, int age, string id, double score) : Student(name, age, id), score(score) {}
// 获取成绩
double getScore() const { return score; }
// 设置成绩
void setScore(double score) { this->score = score; }
// 信息输入输出
void input() {
Student::input();
cout << "成绩:";
cin >> score;
}
void output() const {
Student::output();
cout << "成绩:" << score << endl;
}
};
// 包装学生
class PackagingStudent : public Student {
private:
string packagingType; // 包装类型
public:
PackagingStudent(string name, int age, string id, string packagingType) : Student(name, age, id), packagingType(packagingType) {}
// 获取包装类型
string getPackagingType() const { return packagingType; }
// 设置包装类型
void setPackagingType(string packagingType) { this->packagingType = packagingType; }
// 信息输入输出
void input() {
Student::input();
cout << "包装类型:";
cin >> packagingType;
}
void output() const {
Student::output();
cout << "包装类型:" << packagingType << endl;
}
};
int main() {
// 定义大学生对象
Student* student;
// 输入测控学生信息
student = new MeasurementStudent("", 0, "", 0);
student->input();
student->output();
delete student;
// 输入机自学生信息
student = new AutomaticControlStudent("", 0, "", "");
student->input();
student->output();
delete student;
// 输入过控学生信息
student = new ProcessControlStudent("", 0, "", 0.0);
student->input();
student->output();
delete student;
// 输入包装学生信息
student = new PackagingStudent("", 0, "", "");
student->input();
student->output();
delete student;
return 0;
}
```
注意,这只是一个参考实现,您可以根据项目需求进行修改和完善。同时,这里采用了指针和动态内存分配,需要注意正确释放动态内存,避免内存泄漏问题。