假设要管理下述几类人员的如下一些数据。 teacher(教师)类:姓名、性别、年龄、职称、担任 课程; student(学生)类:姓名、性别、年龄、学号、系别; gradstudent(研究生)类:姓名、性别、年龄、学号、系别、导师。 要求每个类只设立构造函数以及显示类对象数据的成员函数)。编写主函数,说明有关类对象,并对其类成员函数进行简单使用(使用动态联编的方法)。
时间: 2023-07-04 18:30:50 浏览: 51
好的,我来回答你的问题。
首先,我们需要定义三个类,分别是teacher(教师)、student(学生)和gradstudent(研究生)。
下面是三个类的定义:
```
class teacher{
public:
teacher(string n, string s, int a, string t, string c):name(n), sex(s), age(a), title(t), course(c){}
virtual void display(){
cout<<"姓名:"<<name<<endl;
cout<<"性别:"<<sex<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"职称:"<<title<<endl;
cout<<"担任课程:"<<course<<endl;
}
protected:
string name;
string sex;
int age;
string title;
string course;
};
class student{
public:
student(string n, string s, int a, int id, string d):name(n), sex(s), age(a), student_id(id), department(d){}
virtual void display(){
cout<<"姓名:"<<name<<endl;
cout<<"性别:"<<sex<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"学号:"<<student_id<<endl;
cout<<"系别:"<<department<<endl;
}
protected:
string name;
string sex;
int age;
int student_id;
string department;
};
class gradstudent{
public:
gradstudent(string n, string s, int a, int id, string d, string t):name(n), sex(s), age(a), student_id(id), department(d), teacher(t){}
virtual void display(){
cout<<"姓名:"<<name<<endl;
cout<<"性别:"<<sex<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"学号:"<<student_id<<endl;
cout<<"系别:"<<department<<endl;
cout<<"导师:"<<teacher<<endl;
}
protected:
string name;
string sex;
int age;
int student_id;
string department;
string teacher;
};
```
我们可以看到,每个类都有一个构造函数,用来初始化对象的数据成员。同时,每个类都有一个虚函数display,用来显示类对象的数据。
接下来,我们来编写主函数,创建对象并调用其成员函数进行简单使用。主函数如下:
```
int main(){
teacher t("张三", "男", 35, "教授", "高等数学");
student s("李四", "女", 20, 20180101, "计算机科学与技术");
gradstudent g("王五", "男", 24, 20160101, "软件工程", "李四");
teacher* p1 = &t;
student* p2 = &s;
student* p3 = &g;
p1->display();
p2->display();
p3->display();
return 0;
}
```
在主函数中,我们分别创建了三个对象t、s和g,分别表示一个教师、一个学生和一个研究生。然后,我们定义了三个指针p1、p2和p3,分别指向t、s和g。这里使用了动态联编的方法,即通过基类的指针来调用派生类的成员函数。
最后,我们调用了p1、p2和p3的display函数来显示对象的数据。
这就是一个简单的类的使用示例。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)