一个基类Person的多个派生类 代码参考
程序员文章站
2022-03-25 16:56:39
1 #include 2 #include 3 4 using namespace std; 5 6 class Person 7 { 8 protected: 9 char Name[10]; 10 char Sex; 11 int Age; 12 pub ......
1 #include <iostream> 2 #include <cstring> 3 4 using namespace std; 5 6 class person 7 { 8 protected: 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 class teacher:public person 18 { 19 private: 20 char dept[20]; 21 int salary; 22 public: 23 teacher(char *dept, int salary, char *name, char sex, int age); 24 void showme(); 25 }; 26 27 class student:public person 28 { 29 private: 30 char id[12]; 31 char class[12]; 32 public: 33 student(char *name, int age, char sex, char *id, char *classid); 34 void showme(); 35 }; 36 37 void person::register(char *name,int age,char sex) 38 { 39 strcpy(name,name); 40 age=age; 41 sex=sex; 42 return; 43 } 44 45 void person::showme() 46 { 47 cout<<"姓名 "<<name<<endl; 48 if(sex=='m') cout<<"性别 男"<<endl; 49 else cout<<"性别 女"<<endl; 50 cout<<"年龄 "<<age<<endl; 51 return; 52 } 53 54 teacher::teacher(char *dept, int salary, char *name, char sex, int age) 55 { 56 strcpy(dept,dept); 57 salary=salary; 58 person::register(name,age,sex); 59 } 60 61 void teacher::showme() 62 { 63 person::showme(); 64 cout<<"工作单位 "<<dept<<endl; 65 cout<<"月薪 "<<salary<<endl; 66 return; 67 } 68 69 student::student(char *name, int age, char sex, char *id, char *classid) 70 { 71 person::register(name,age,sex); 72 strcpy(id,id); 73 strcpy(class,classid); 74 } 75 76 void student::showme() 77 { 78 cout<<"学号 "<<id<<endl; 79 person::showme(); 80 cout<<"班级 "<<class<<endl; 81 return; 82 } 83 84 int main() 85 { 86 char name1[20],name2[20],dept[20],id[12],class[12],sex1,sex2; 87 int salary,age1,age2; 88 cin>>name1>>age1>>sex1>>dept>>salary; 89 cin>>name2>>age2>>sex2>>id>>class; 90 teacher one(dept,salary,name1,sex1,age1); 91 student two(name2,age2,sex2,id,class); 92 one.showme(); 93 two.showme(); 94 return 0; 95 }
上一篇: Python使用pip安装速度慢换源