使用动态联编的方法完成程序:假设要管理下述几类人员的如下一些数据。 teacher(教师)类:姓名、性别、年龄、职称、担任 课程; student(学生)类:姓名、性别、年龄、学号、系别; gradstudent(研究生)类:姓名、性别、年龄、学号、系别、导师。 要求每个类只设立构造函数以及显示类对象数据的成员函数)。编写主函数,说明有关类对象,并对其类成员函数进行简单使用
时间: 2023-07-04 09:30:52 浏览: 112
下面是使用动态联编实现上述要求的代码,其中每个类放在一个单独的头文件中:
teacher.h:
```cpp
#ifndef TEACHER_H
#define TEACHER_H
#include <string>
class Teacher {
public:
Teacher(const std::string& name, const std::string& gender, int age, const std::string& title, const std::string& course);
void display() const;
private:
std::string name_;
std::string gender_;
int age_;
std::string title_;
std::string course_;
};
#endif
```
teacher.cpp:
```cpp
#include "teacher.h"
#include <iostream>
Teacher::Teacher(const std::string& name, const std::string& gender, int age, const std::string& title, const std::string& course)
: name_(name), gender_(gender), age_(age), title_(title), course_(course) {
}
void Teacher::display() const {
std::cout << "Name: " << name_ << std::endl;
std::cout << "Gender: " << gender_ << std::endl;
std::cout << "Age: " << age_ << std::endl;
std::cout << "Title: " << title_ << std::endl;
std::cout << "Course: " << course_ << std::endl;
}
```
student.h:
```cpp
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
class Student {
public:
Student(const std::string& name, const std::string& gender, int age, const std::string& id, const std::string& department);
void display() const;
private:
std::string name_;
std::string gender_;
int age_;
std::string id_;
std::string department_;
};
#endif
```
student.cpp:
```cpp
#include "student.h"
#include <iostream>
Student::Student(const std::string& name, const std::string& gender, int age, const std::string& id, const std::string& department)
: name_(name), gender_(gender), age_(age), id_(id), department_(department) {
}
void Student::display() const {
std::cout << "Name: " << name_ << std::endl;
std::cout << "Gender: " << gender_ << std::endl;
std::cout << "Age: " << age_ << std::endl;
std::cout << "ID: " << id_ << std::endl;
std::cout << "Department: " << department_ << std::endl;
}
```
gradstudent.h:
```cpp
#ifndef GRADSTUDENT_H
#define GRADSTUDENT_H
#include <string>
class GradStudent {
public:
GradStudent(const std::string& name, const std::string& gender, int age, const std::string& id, const std::string& department, const std::string& advisor);
void display() const;
private:
std::string name_;
std::string gender_;
int age_;
std::string id_;
std::string department_;
std::string advisor_;
};
#endif
```
gradstudent.cpp:
```cpp
#include "gradstudent.h"
#include <iostream>
GradStudent::GradStudent(const std::string& name, const std::string& gender, int age, const std::string& id, const std::string& department, const std::string& advisor)
: name_(name), gender_(gender), age_(age), id_(id), department_(department), advisor_(advisor) {
}
void GradStudent::display() const {
std::cout << "Name: " << name_ << std::endl;
std::cout << "Gender: " << gender_ << std::endl;
std::cout << "Age: " << age_ << std::endl;
std::cout << "ID: " << id_ << std::endl;
std::cout << "Department: " << department_ << std::endl;
std::cout << "Advisor: " << advisor_ << std::endl;
}
```
然后在主函数中使用这些类:
```cpp
#include "teacher.h"
#include "student.h"
#include "gradstudent.h"
int main() {
Teacher t("Tom", "Male", 35, "Professor", "Math");
Student s("Amy", "Female", 20, "001", "Computer Science");
GradStudent g("Jerry", "Male", 25, "101", "Physics", "Dr. Smith");
t.display();
s.display();
g.display();
return 0;
}
```
运行结果:
```
Name: Tom
Gender: Male
Age: 35
Title: Professor
Course: Math
Name: Amy
Gender: Female
Age: 20
ID: 001
Department: Computer Science
Name: Jerry
Gender: Male
Age: 25
ID: 101
Department: Physics
Advisor: Dr. Smith
```
阅读全文