任务九:定义一个person类,派生student类,在派生teacher类,并派生当老师的学生。
#include "stdafx.h"
#include
#include
#include
using namespacestd;
class person
{
public:
person(char*n,char*s,inta)
{
{
name= new char[strlen(n)+ 1];
strcpy(name,n);
}
{
sex = newchar[strlen(s)+ 1];
strcpy(sex,s);
}
{
age= a;
}
}
voidprint()
{
cout<< "姓名:" << name << endl;
cout<< "性别:" << sex << endl;
cout<< "年龄:" << age << endl;
}
protected:
char*name;
char*sex;
intage;
};
class student:virtualpublicperson
{
public:
student(char*n,char*s,inta,char*g):person(n,s,a)
{
grad =newchar[strlen(g)+ 1];
strcpy(grad,g);
}
voidprint()
{
person::print();
cout<< "年级: " << grad << endl;
}
protected:
char*grad;
};
class teacher:virtualpublicstudent
{
public:
teacher(char*n,char*s,inta,char*g,char*l):student(n, s,a,g),person(n, s,a)
{
lesson= new char[strlen(l)+ 1];
strcpy(lesson,l);
}
voidprint()
{
student::print();
cout<< "授课名: " << lesson << endl;
}
protected:
char*lesson;
};
class teacherstudent: public teacher
{
public:
teacherstudent(char*n,char*s,inta,char*g,char*l,char*m): teacher(n,s,a,g,l),student(n, s,a,g),person(n, s,a)
{
Major= new char[strlen(m)+ 1];
strcpy(Major,m);
}
voidprint()
{
teacher::print();
cout<< "专业方向:" << Major << endl;
}
protected:
char*Major;
};
int main()
{
student stu("李明","男",21, "14级");
teacher teac("张华", "男", 35, " ", "计算机基础");
teacherstudent teacstu("李淼", "男", 28, "07级", "机电一体化", "机械电子");
stu.print();
teac.print();
teacstu.print();
}