定义一个人员类CPeople,其属性有(保护类型):姓名、性别、年龄;从中派生出学生类CStudent,添加属性:学号和入学成绩;从CPeople 类再派生出教师类CTeacher,添加属性:职务、部门;由Cstudent 类派生究生类CGraduate,添加属性:研究方向和导师,由CGraduate 和CTeacher 共同派生出在职研究生类CGradOnWork,分别定义其中的构造函数和输出函数。主函数中定义各种类的对象,并完成测试。
要求:先画出类图,再完成代码。
类图:
头文件(person.h)如下
#pragma once
//人员类
class CPeople {
protected:
char name[10];//姓名
char sex[5];//性别
int age;//年龄
public:
CPeople(const char* n, const char* s, int a);
void display();
~CPeople();
};
//学生类
class CStudent :public CPeople {
protected:
char number[20];//学号
float score;//成绩
public:
CStudent(const char* n, const char* s, int a,
const char* num, float sco);
void display();
~CStudent();
};
//教师类
class CTeacher :public CPeople {
protected:
char duty[20];//职务
char department[20];//部门
public:
CTeacher(const char* n, const char* s, int a,
const char* d, const char* dep);
void display();
~CTeacher();
};
//研究生类
class CGraduate :public CStudent {
protected:
char research_dire[20];//研究方向
char tutor[10];//导师
public:
CGraduate(const char* n, const char* s, int a,
const char* num, float sco, const char* res, const char* tu);
void display();
~CGraduate();
};
//在职研究生类
class CGradOnWork :public CGraduate, public CTeacher {
public:
CGradOnWork(const char* n, const char* s, int a,
const char* num, float sco, const char* res,
const char* tu, const char* d, const char* dep);
void display();
~CGradOnWork();
};
main文件如下
#include<iostream>
using namespace std;
#include"person.h"
#include<string>
CPeople::CPeople(const char* n,const char *s, int a)
{
strcpy_s(name, n);
strcpy_s(sex, s);
age = a;
}
CPeople::~CPeople()
{
cout << "人员类析构函数" << endl;
}
void CPeople::display()
{
cout<<endl<< "姓名:" << name << endl;
cout << "性别:" << sex << endl;
cout << "年龄:" << age << endl;
}
CStudent::CStudent(const char* n, const char* s, int a,
const char* num, float sco) :CPeople(n, s, a) {
strcpy_s(number, num);
score = sco;
}
void CStudent::display() {
CPeople::display();
cout << "学号:" << number << endl;
cout << "成绩:" << score << endl;
}
CStudent::~CStudent() {
cout << "学生类析构函数" << endl;
}
CTeacher::CTeacher(const char* n, const char* s, int a,
const char* d, const char* dep):CPeople(n, s, a) {
strcpy_s(duty, d);
strcpy_s(department, dep);
}
void CTeacher::display()
{
CPeople::display();
cout << "职务" << duty << endl;
cout << "部门" << department << endl;
}
CTeacher::~CTeacher() {
cout << "教师类析构函数" << endl;
}
CGraduate::CGraduate(const char* n, const char* s, int a,
const char* num, float sco, const char* res, const char* tu) :CStudent(n, s, a, num, sco)
{
strcpy_s(research_dire, res);
strcpy_s(tutor, tu);
}
void CGraduate::display() {
CStudent::display();
cout << "研究方向:" << research_dire << endl;
cout << "导师" << tutor << endl;
}
CGraduate::~CGraduate()
{
cout << "研究生类析构函数" << endl;
}
CGradOnWork::CGradOnWork(const char* n, const char* s, int a,
const char* num, float sco, const char* res,
const char* tu, const char* d, const char* dep) :CGraduate(n, s, a, num, sco, res, tu), CTeacher(n, s, a,
d, dep)
{ }
void CGradOnWork::display() {
CGraduate::display();
cout << "职务" << duty << endl;
cout << "部门" << department << endl;
}
CGradOnWork::~CGradOnWork() {
cout << "在职研究生类析构函数" << endl;
}
void main()
{
CPeople p1("张三", "男", 16);
p1.display();
CStudent p2("张三", "男", 16, "17409", 88.0);
p2.display();
CTeacher p3("李四", "男", 40, "高数老师", "基础部");
p3.display();
CGraduate p4("张三", "男", 16, "17409", 88.0,"微积分","李四");
p4.display();
CGradOnWork p5("王五", "男", 25, "111000", 90.0, "微积分", "李四", "高数老师", "基础部");
p5.display();
}
运行截图:
代码仅供参考