• 一个基类Person的多个派生类 代码参考


     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 }
  • 相关阅读:
    洛谷 P4297 [NOI2006]网络收费
    bzoj 5072: [Lydsy1710月赛]小A的树
    树形背包
    loj #2071. 「JSOI2016」最佳团体
    bzoj 2427: [HAOI2010]软件安装
    bzoj 4987: Tree
    loj #2007. 「SCOI2015」国旗计划
    loj #2006. 「SCOI2015」小凸玩矩阵
    loj #2020. 「AHOI / HNOI2017」礼物
    loj #547. 「LibreOJ β Round #7」匹配字符串
  • 原文地址:https://www.cnblogs.com/Conan-jine/p/12745084.html
Copyright © 2020-2023  润新知