📄 ex8_4.cpp
字号:
#include<iostream.h>
#include<string.h>
enum Tsex{mid,man,woman};
struct course{
char* coursename;
int grade;
};
class Person{
char IdPerson[19]; //身份证号,18位数字
char *Name; //姓名
Tsex Sex; //性别
int Birthday; //生日,格式1986年8月18日写作19860818
char *HomeAddress; //家庭地址
public:
Person(char *,char *,Tsex,int,char *);
Person();
~Person();
void PrintPersonInfo();
//其他接口函数
};
Person::Person(char *id,char *name,Tsex sex,int birthday,char *homeadd){
cout<<"构造Person"<<endl;
strcpy(IdPerson,id);
Name=new char[strlen(name)+1];
strcpy(Name,name);
Sex=sex;
Birthday=birthday;
HomeAddress=new char[strlen(homeadd)+1];
strcpy(HomeAddress,homeadd);
}
Person::Person(){
cout<<"构造Person"<<endl;
IdPerson[0]='\0';Name=NULL;Sex=mid;
Birthday=0;HomeAddress=NULL;
}
Person::~Person(){
cout<<"析构Person"<<endl;
if(Name)delete [] Name;if(HomeAddress) delete [] HomeAddress;
}
void Person::PrintPersonInfo(){
int i;
cout<<"身份证号:"<<IdPerson<<'\n'<<"姓名:"<<Name<<'\n'<<"性别:";
if(Sex==man)cout<<"男"<<'\n';
else if(Sex==woman)cout<<"女"<<'\n';
else cout<<" "<<'\n';
cout<<"出生年月日:";
i=Birthday;
cout<<i/10000<<"年";
i=i%10000;
cout<<i/100<<"月"<<i%100<<"日"<<'\n'<<"家庭住址:"<<HomeAddress<<'\n';
}
class Student:public virtual Person{//以虚基类定义公有派生的学生类
char NoStudent[10];//学号
//30门课程与成绩略
public:
Student(char *id,char *name,Tsex sex,int birthday,char *homeadd,char * nostud);
//注意派生类构造函数声明方式
Student();
~Student(){cout<<"析构Student"<<endl;}
void PrintStudentInfo();
};
Student::Student(char *id,char *name,Tsex sex,int birthday,char *homeadd,char * nostud)
:Person(id,name,sex,birthday,homeadd){//注意Person参数表不用类型
cout<<"构造Student"<<endl;
strcpy(NoStudent,nostud);
}
Student::Student(){//基类缺省的无参数构造函数不必显式给出
cout<<"构造Student"<<endl;
NoStudent[0]=NULL;
}
void Student::PrintStudentInfo(){
cout<<"学号:"<<NoStudent<<'\n';
PrintPersonInfo();
}
class GStudent:public Student{//以虚基类定义公有派生的研究学生类
char NoGStudent[10];//研究生号
//其他略
public:
GStudent(char *id,char *name,Tsex sex,int birthday,char *homeadd,char*nostud,
char * nogstudent); //注意派生类构造函数声明方式
GStudent();
~GStudent(){cout<<"析构GStudent"<<endl;};
void PrintGStudentInfo();
};
GStudent::GStudent(char *id,char *name,Tsex sex,int birthday,char *homeadd,
char * nostud,char * nogstud)
:Student(id,name,sex,birthday,homeadd,nostud){//注意Student参数表不用类型
cout<<"构造GStudent"<<endl;
strcpy(NoGStudent,nogstud);
}
GStudent::GStudent(){//基类缺省的无参数构造函数不必显式给出
cout<<"构造GStudent"<<endl;
NoGStudent[0]=NULL;
}
void GStudent::PrintGStudentInfo(){
cout<<"研究生号:"<<NoGStudent<<'\n';
PrintStudentInfo();
}
class Employee:public virtual Person{//以虚基类定义公有派生的教职工类
char NoEmployee[10];//教职工号
//其他略
public:
Employee(char*id,char*name,Tsex sex,int birthday,char*homeadd,char*noempl);
//注意派生类构造函数声明方式
Employee();
~Employee(){cout<<"析构Employee"<<endl;}
void PrintEmployeeInfo();
void PrintEmployeeInfo1();
};
Employee::Employee(char*id,char*name,Tsex sex,int birthday,char*homeadd,char*noempl)
:Person(id,name,sex,birthday,homeadd){//注意Person参数表可不用类型
cout<<"构造Employee"<<endl;
strcpy(NoEmployee,noempl);
}
Employee::Employee(){//基类缺省的无参数构造函数不必显式给出
cout<<"构造Employee"<<endl;
NoEmployee[0]=NULL;
}
void Employee::PrintEmployeeInfo(){
cout<<"教职工号:"<<NoEmployee<<'\n';
PrintPersonInfo();
}
void Employee::PrintEmployeeInfo1(){cout<<"教职工号:"<<NoEmployee<<'\n';}
class EGStudent:public Employee,public GStudent{//以虚基类定义公有派生的学生类
char NoEGStudent[10];//在职学习号
//其他略
public:
EGStudent(char*id,char*name,Tsex sex,int birthday,char*homeadd,char*nostud,
char*nogstud,char*noempl,char*noegstud);
//注意派生类构造函数声明方式
EGStudent();
~EGStudent(){cout<<"析构EGStudent"<<endl;};
void PrintEGStudentInfo();
};
EGStudent::EGStudent(char*id,char*name,Tsex sex,int birthday,char*homeadd,
char*nostud,char*nogstud,char*noempl,char*noegstud)
:GStudent(id,name,sex,birthday,homeadd,nostud,nogstud),
Employee(id,name,sex,birthday,homeadd,noempl),
Person(id,name,sex,birthday,homeadd){//注意Person再次出现
cout<<"构造EGStudent"<<endl;
strcpy(NoEGStudent,noegstud);
}
EGStudent::EGStudent(){//基类缺省的无参数构造函数不必显式给出
cout<<"构造EGStudent"<<endl;
NoEGStudent[0]=NULL;
}
void EGStudent::PrintEGStudentInfo(){
cout<<"在职学习号:"<<NoEGStudent<<'\n';
PrintEmployeeInfo1();
PrintGStudentInfo();
}
void main(void){
EGStudent egstu1("320102811226161","朱海鹏",man,19811226,"南京市黄浦路1号","06000123",
"034189","06283","030217");
egstu1.PrintEGStudentInfo();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -