• 类的派生


      1 #include<iostream>
      2 #include<fstream>
      3 #include<string> 
      4 using namespace std;
      5 #define maxlen 100
      6 
      7 //---------------------------------基类-------------------------------------
      8 class Teacher{//基类
      9 public:
     10     //Teacher(string name,string position);
     11     virtual int CaleSalary() const = 0;//纯虚函数,计算教师工资
     12     virtual void Show() const = 0;//纯虚函数,输出教师的姓名、职称、工资
     13     string name;//姓名
     14     string position;//职称
     15     int Salary;//工资
     16 };
     17 //---------------------------------基类-------------------------------------
     18 
     19 
     20 //---------------------------------助教类-----------------------------------
     21 //助教
     22 class Assistant:public Teacher{
     23 public:
     24     Assistant(string name,string position);//构造函数
     25     virtual int CaleSalary()const ;//计算教师工资
     26      virtual void Show()const;//输出教师的姓名、职称、工资
     27 };
     28 
     29 //Assistant(助教)构造函数
     30 Assistant::Assistant(string Name,string Position)
     31 {
     32     name = Name;
     33     position = Position;
     34 }
     35 
     36 //Assistant(助教)//计算教师工资
     37 int Assistant::CaleSalary()const
     38 {
     39     int temp;
     40     if(position=="助教"){
     41         temp = 900;
     42     }else if(position=="讲师"){
     43         temp = 1000;
     44     }else if(position=="副教授"){
     45         temp = 1300;
     46     }else if(position=="教授"){
     47         temp = 1600;
     48     }
     49     
     50     return temp;
     51 
     52 }
     53 
     54 
     55 //Assistant(助教)//输出显示
     56 void Assistant::Show()const
     57 {
     58     cout<<"姓名: "<<name<<", 职称: "<<position<<", 工资: "<<Salary<<""<<endl;
     59 }
     60 
     61 
     62 //---------------------------------助教类-----------------------------------
     63 
     64 
     65 
     66 //---------------------------------讲师类-----------------------------------
     67 
     68 //讲师类
     69 class Lecture:public Teacher{
     70 public:
     71     Lecture(string name,string position);//构造函数
     72     virtual int CaleSalary()const;//计算教师工资
     73     virtual void Show()const;//输出教师的姓名、职称、工资
     74 };
     75 
     76 //Lecture(讲师)构造函数
     77 Lecture::Lecture(string Name,string Position)
     78 {
     79     name = Name;
     80     position = Position;
     81 }
     82 
     83 //Lecture(讲师)//计算教师工资
     84 int Lecture::CaleSalary()const
     85 {
     86     int temp;
     87     if(position=="助教"){
     88         temp = 900;
     89     }else if(position=="讲师"){
     90         temp = 1000;
     91     }else if(position=="副教授"){
     92         temp = 1300;
     93     }else if(position=="教授"){
     94         temp = 1600;
     95     }
     96     
     97     return temp;;
     98 }
     99 
    100 
    101 //Lecture(讲师):输出显示
    102 void Lecture::Show()const
    103 {
    104     cout<<"姓名: "<<name<<", 职称: "<<position<<", 工资: "<<Salary<<""<<endl;
    105 }
    106 //---------------------------------讲师类-----------------------------------
    107 
    108 
    109 
    110 
    111 //---------------------------------副教授类---------------------------------
    112 
    113 //副教授类(AssociateProfessor)
    114 class AssociateProfessor:public Teacher{
    115 public:
    116     AssociateProfessor(string name,string position);//构造函数
    117     virtual int CaleSalary()const;//计算教师工资
    118     virtual void Show()const;//输出教师的姓名、职称、工资
    119 };
    120 
    121 //副教授类(AssociateProfessor)构造函数
    122 AssociateProfessor::AssociateProfessor(string Name,string Position)
    123 {
    124     name = Name;
    125     position = Position;
    126 }
    127 
    128 //副教授类(AssociateProfessor)//计算教师工资
    129 int AssociateProfessor::CaleSalary()const
    130 {
    131     int temp;
    132     if(position=="助教"){
    133         temp = 900;
    134     }else if(position=="讲师"){
    135         temp = 1000;
    136     }else if(position=="副教授"){
    137         temp = 1300;
    138     }else if(position=="教授"){
    139         temp = 1600;
    140     }
    141     
    142     return temp;
    143 }
    144 
    145 
    146 //副教授类(AssociateProfessor)//输出显示
    147 void AssociateProfessor::Show()const
    148 {
    149     cout<<"姓名: "<<name<<", 职称: "<<position<<", 工资: "<<Salary<<""<<endl;
    150 }
    151 //---------------------------------副教授类---------------------------------
    152 
    153 //---------------------------------教授类-----------------------------------
    154 
    155 //Proffessor(教授类)
    156 class Proffessor:public Teacher{
    157 public:
    158     Proffessor(string name,string position);//构造函数
    159     virtual int CaleSalary()const;//计算教师工资
    160     virtual void Show()const;//输出教师的姓名、职称、工资
    161 };
    162 
    163 //Proffessor(教授类)构造函数
    164 Proffessor::Proffessor(string Name,string Position)
    165 {
    166     name = Name;
    167     position = Position;
    168 }
    169 
    170 int Proffessor::CaleSalary()const
    171 {
    172     int temp;
    173     if(position=="助教"){
    174         temp = 900;
    175     }else if(position=="讲师"){
    176         temp = 1000;
    177     }else if(position=="副教授"){
    178         temp = 1300;
    179     }else if(position=="教授"){
    180         temp = 1600;
    181     }
    182     
    183     return temp;
    184 }
    185 
    186 
    187 //Proffessor(教授类)输出显示
    188 void Proffessor::Show()const
    189 {
    190     cout<<"姓名: "<<name<<", 职称: "<<position<<", 工资: "<<Salary<<""<<endl;
    191 }
    192 //---------------------------------教授类-----------------------------------
    193 
    194 struct Total
    195 {
    196     string name;
    197     string position;
    198     int salary;
    199 };
    200 Total Data[maxlen];
    201 int num;//几组数据
    202 
    203 void analyse(ifstream &in)
    204 { 
    205     int i;
    206     string s="";//初始字符串s为空
    207     char ch;//字符
    208     string str[maxlen];//存放字符串
    209     int count = 0;
    210     while(!in.eof())//当文件没读完
    211     {
    212         ch=in.get();//读入字符
    213         if(ch!='
    '&&ch!=' ')//当前字符不死空格或者换行符
    214         {
    215             s +=ch;//加入到字符串尾部
    216         }else {//是空格或换行符
    217             str[count] =s;
    218             count++;//计数+1
    219             s ="";//字符冲为空
    220         }
    221      }
    222 
    223     num = 0;//统计行数
    224     for(i=0;i<count;i=i+2)
    225     {
    226         Data[num].name = str[i];//姓名
    227         Data[num].position = str[i+1];//职称
    228         Data[num].salary = 0;//工资初始化为0
    229         num++;//行数+1
    230     }
    231 }
    232 
    233 
    234 int main() 
    235 {  
    236     int i;
    237     ifstream in; 
    238     ofstream out;
    239 /*
    240     char a[30];
    241     cout<<"请输入文件名:";
    242     cin>>a;
    243     in.open(a,ios::in); //打开文件
    244 */
    245     in.open("D:/teacher.txt",ios::in); //打开文件
    246     if(in.is_open()){
    247         analyse(in);   
    248         in.close();
    249     }else printf("文件操作出错!
    "); 
    250 
    251     out.open("D:/Teache.txt");//将某些关键词替换后的字符串存到文件中
    252      if(out.is_open())//打开文件
    253     {
    254         for( i=0;i<num;i++)
    255         {
    256             if(Data[i].position=="助教")
    257             {
    258                 Teacher  *assistant = new Assistant(Data[i].name,Data[i].position);
    259                 assistant->Salary =assistant->CaleSalary();
    260                 assistant->Show();
    261                 out<<assistant->name;
    262                 out<<" ";
    263                 out<<assistant->position;
    264                 out<<" ";
    265                 out<<assistant->Salary;
    266                 out<<"
    ";
    267      
    268             }else if(Data[i].position=="讲师")
    269             {
    270                 Teacher *lecture = new Lecture(Data[i].name,Data[i].position);
    271                 
    272                 lecture->Salary =lecture->CaleSalary();
    273                 lecture->Show();
    274                 out<<lecture->name;
    275                 out<<" ";
    276                 out<<lecture->position;
    277                 out<<" ";
    278                 out<<lecture->Salary;
    279                 out<<"
    ";
    280             }else if(Data[i].position=="副教授")
    281             {
    282                 Teacher *associateProfessor = new AssociateProfessor (Data[i].name,Data[i].position);
    283                 associateProfessor->Salary =associateProfessor->CaleSalary();
    284                 associateProfessor->Show();
    285                 out<<associateProfessor->name;
    286                 out<<" ";
    287                 out<<associateProfessor->position;
    288                 out<<" ";
    289                 out<<associateProfessor->Salary;
    290                 out<<"
    ";
    291                 
    292             }else if(Data[i].position=="教授")
    293             {
    294                 Teacher *proffessor = new Proffessor (Data[i].name,Data[i].position);
    295                 proffessor->Salary =proffessor->CaleSalary();
    296                 proffessor->Show();
    297                 out<<proffessor->name;
    298                 out<<" ";
    299                 out<<proffessor->position;
    300                 out<<" ";
    301                 out<<proffessor->Salary;
    302                 out<<"
    ";
    303             }
    304         }
    305      }else printf("文件操作出错!
    "); //文件带卡失败
    306     return 0;
    307 }
  • 相关阅读:
    利用百度云盘API上传文件至百度云盘
    测试Centos硬盘读写速度
    into outfile 生成sql脚本
    Nginx设置Js、Css等静态文件的缓存过期时间
    mysql查询区分大小写
    Table './mysql/proc' is marked as crashed and should be repaired 解决方法
    CentOS 6.6 下配置软RAID5
    管道限流利器pv
    mydumper使用
    RAID详解
  • 原文地址:https://www.cnblogs.com/minmsy/p/5093971.html
Copyright © 2020-2023  润新知