一.问题描述
设计一个类people,有保护数据成员:age(年龄,整型),name(姓名,string),行为成员:两个构造函数(一个默认,另一个有参数);默认析构函数;void setValue(int m, string str)给age和name赋值;有一个void类型的纯虚函数display()。
设计一个学生类student,公有继承类people,有私有成员:studentID(学号,整型),行为成员:两个构造函数(一个默认,另一个有参数);默认析构函数;void setID(int m)给studentID赋值;display()函数输出学生的姓名,年龄,学号。
设计一个教师类teacher,公有继承类people,有私有成员:teacherID(工号,整型),行为成员:两个构造函数(一个默认,另一个有参数);默认析构函数;void setID(int m)给teacherID赋值; display()函数输出教师的姓名,年龄,工号。
在main函数定义学生对象和教师对象,利用父类指针给对象初始化赋值或调用setValue()和setID()赋值,并输出学生和老师的信息。
二.设计思路
按照题目要求编写代码即可
三.流程图
四.伪代码
1
五.代码实现
#include<iostream>
using namespace std;
class people {
protected:
int age;
string name;
public:
people() {}
people(int a,string s):age(a),name(s) {}
~people(){}
void setValue(int m, string str) {
age = m; name = str;
}
virtual void setID(int m)=0;
virtual void diaplay() = 0;
};
class student :public people {
private:
int studentID;
public:
student() {}
student(int id,string s,int a):studentID(id),people(a,s){}
~student(){}
void setID(int m) {
studentID = m;
}
void diaplay() {
cout << name << ' ' << age << " " << studentID << endl;
}
};
class teacher:public people {
private:
int teacherID;
public:
teacher(){}
teacher(int id, string s, int a) : teacherID(id), people(a, s){}
~teacher(){}
void setID(int m) {
teacherID = m;
}
void diaplay() {
cout << name << ' ' << age << " " << teacherID << endl;
}
};
int main() {
student feiyue;
teacher wenbin;
people* p, * s;
p = &feiyue;
s = &wenbin;
p->setValue(38,"wenbin");p->setID(10001);
p->diaplay();
s->setValue(19, "feiyue"); s->setID(20223913);
s->diaplay();
}
浙公网安备 33010602011771号