2015C++第二週,任務九:定義一個person類,派生student類,在派生teacher類,並派生當老師的學生。
任務九:定義一個person類,派生student類,在派生teacher類,並派生當老師的學生。
#include "stdafx.h"
#include <stdio.h>
#include<iostream>
#include<string>
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<< "
}
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();
}