欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

从Student类和Teacher类多重派生Graduate类 代码参考

程序员文章站 2022-04-04 10:57:21
1 #include 2 #include 3 4 using namespace std; 5 6 class Person 7 { 8 private: 9 char Name[10]; 10 char Sex; 11 int Age; 12 publi ......
  1 #include <iostream>
  2 #include <cstring>
  3 
  4 using namespace std;
  5 
  6 class person
  7 {
  8     private:
  9         char name[10];
 10         char sex;
 11         int age;
 12     public:
 13         void register(char *name, int age, char sex);
 14         void showme();
 15 };
 16 
 17 void person::register(char *name, int age, char sex)
 18 {
 19     strcpy(name,name);
 20     age=age;
 21     sex=sex;
 22     return;
 23 }
 24 
 25 void person::showme()
 26 {
 27     cout<<"姓名 "<<name<<endl;
 28     if(sex=='m')    cout<<"性别 男"<<endl;
 29     else cout<<"性别 女"<<endl;
 30     cout<<"年龄 "<<age<<endl;
 31     return;
 32 }
 33 
 34 class teacher:public person
 35 {
 36     private:
 37         char dept[20];
 38         int salary;
 39     public:
 40         teacher(char *name, int age, char sex, char *dept, int salary);
 41         void show();
 42 };
 43 
 44 teacher::teacher(char *name, int age, char sex, char *dept, int salary):person()
 45 {
 46     person::register(name,age,sex);
 47     strcpy(dept,dept);
 48     salary=salary;
 49 }
 50 
 51 void teacher::show()
 52 {
 53     cout<<"工作单位 "<<dept<<endl;
 54     cout<<"月薪 "<<salary<<endl;
 55     return;
 56 }
 57 
 58 class student:public person
 59 {
 60     private:
 61         char id[12];
 62         char class[12];
 63     public:
 64         student(char *name, int age, char sex, char *id, char *class);
 65         void show();
 66 };
 67 
 68 student::student(char *name, int age, char sex, char *id, char *class):person()
 69 {
 70     person::register(name,age,sex);
 71     strcpy(this->id,id);
 72     strcpy(this->class,class);
 73 }
 74 
 75 void student::show()
 76 {
 77     cout<<"班级 "<<class<<endl;
 78     cout<<"学号 "<<id<<endl;
 79     person::showme();
 80     return;
 81 }
 82 
 83 class graduate:public teacher,public student
 84 {
 85     public:
 86         graduate(char *name, int age, char sex, char *dept, int salary, char *id, char *classid);
 87         void showme();
 88 };
 89 
 90 graduate::graduate(char *name, int age, char sex, char *dept, int salary, char *id, char *classid):teacher(name,age,sex,dept,salary),student(name,age,sex,id,classid){}
 91 
 92 void graduate::showme()
 93 {
 94     student::show();
 95     teacher::show();
 96     return;
 97 }
 98 
 99 int main()
100 {
101     char name[10],dept[20],id[12],classid[12],sex;
102     int salary,age;
103     cin>>name>>age>>sex>>dept>>salary>>id>>classid;
104     graduate one(name,age,sex,dept,salary,id,classid);
105     one.showme();
106     return 0;
107 }