//定义并描述一个人员类Person,并派生出学生类和教师类,再又学生类和教师类共同派生出在职读书的教师类。。。。
#include<iostream>
using namespace std;
struct birthday{
int year;
int month;
int date;
};
class Person
{
private:
char name[20];
char sex[10];
char ID[20];
birthday birth;
public:
Person(char *name,char *sex,char *ID, birthday birth)
{
strcpy(this->name,name);
strcpy(this->sex,sex);
strcpy(this->ID,ID);
this->birth=birth;
}
void ShowInfo()
{
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"ID:"<<ID<<endl;
cout<<"birth:"<<birth.year<<"-"<<birth.month<<"-"<<birth.date<<endl;
}
};
class Student:virtual public Person
{
private:
char stuID[10];
double score;
public:
Student(char *name,char *sex,char *ID, birthday birth,char *stuID,double score)
:Person(name,sex,ID,birth)
{
strcpy(this->stuID,stuID);
this->score=score;
}
void ShowInfo()
{
Person::ShowInfo();
cout<<"studentID:"<<stuID<<endl;
cout<<"score:"<<score<<endl;
}
};
class Teacher:virtual public Person
{
private:
char pos[10];
public:
Teacher(char *name,char *sex,char *ID, birthday birth,char *pos):Person(name,sex,ID,birth)
{
strcpy(this->pos,pos);
}
void ShowInfo()
{
Person::ShowInfo();
cout<<"position:"<<pos<<endl;
}
};
class Stu_Tech:public Student,public Teacher
{
public:
Stu_Tech(char *name,char *sex,char *ID, birthday birth,char *stuID,double score,char *pos)
:Person(name,sex,ID,birth),Student(name,sex,ID,birth,stuID,score),Teacher(name,sex,ID,birth,pos)
{
}
void ShowInfo()
{
Person::ShowInfo();
Student::ShowInfo();
Teacher::ShowInfo();
}
};
int main()
{
birthday birth={1990,1,1};
Person p1("zhangsan","man","420117",birth);
p1.ShowInfo();
cout<<endl;
Student p2("lisi","man","420118",birth,"2012210",89.0);
p2.ShowInfo();
cout<<endl;
Teacher p3("wangwu","woman","420119",birth,"jiaoshou");
p3.ShowInfo();
cout<<endl;
Stu_Tech p4("maliu","woman","420120",birth,"2012211",90.0,"zhujiao");
p4.ShowInfo();
cout<<endl;
return 0;
}
派生与继承的应用1
最新推荐文章于 2021-05-01 14:20:52 发布