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

0
12
  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 }

<

发布回复

请输入评论!
请输入你的名字