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

2020-04-21 16:05:13来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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

  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 }

 


原文链接:https://www.cnblogs.com/Conan-jine/p/12748052.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Test for change word to publish on cnblogs

下一篇:C++基础 学习笔记四:重载之函数重载