类的派生

  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!=‘\n‘&&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("文件操作出错!\n");
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<<"\n";
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<<"\n";
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<<"\n";
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<<"\n";
303             }
304         }
305      }else printf("文件操作出错!\n"); //文件带卡失败
306     return 0;
307 }
时间: 2024-08-05 11:14:05

类的派生的相关文章

C++:基类与派生类对象之间的赋值兼容关系

4.5 基类与派生类对象之间的赋值兼容关系 在一定条件下,不同类型的数据之间可以进行类型转换,例如可以将整型数据赋给双精度型变量. 在赋值之前,先把整型数据转换为双精度型数据,然后再把它双精度型变量.这种不同类型之间的自动转换,称为赋值兼容.在基类和派生类对象之间也存在有赋值兼容关系,基类和派生类对象之间的赋值兼容规则是指在需要基类对象的任何地方,都可以使用公有派生类的对象来代替.因为,通过公有继承,除了构造函数和析构函数外,派生类保留了基类其他的所有的成员.那么,派生类就具有基类的全部功能,凡

C++ Primer 学习笔记_66_面向对象编程 --定义基类和派生类[续]

算法旨在用尽可能简单的思路解决问题,理解算法也应该是一个越看越简单的过程,当你看到算法里的一串概念,或者一大坨代码,第一感觉是复杂,此时不妨从例子入手,通过一个简单的例子,并编程实现,这个过程其实就可以理解清楚算法里的最重要的思想,之后扩展,对算法的引理或者更复杂的情况,对算法进行改进.最后,再考虑时间和空间复杂度的问题. 了解这个算法是源于在Network Alignment问题中,图论算法用得比较多,而对于alignment,特别是pairwise alignment, 又经常遇到maxim

基类与派生类的构造函数

一.缺省构造函数的调用关系 通过下面的例子,我们来看一下基类与派生的构造函数的调用顺序.创建时先基类后派生类.销毁时先派生类后基类. #include <iostream> #include <string> using namespace std; class CBase { string name; int age; public: CBase() { cout <<"BASE" << endl; } ~CBase() { cout

C++ Primer 学习笔记_65_面向对象编程 --概述、定义基类和派生类

面向对象编程 --概述.定义基类和派生类 引言: 面向对象编程基于的三个基本概念:数据抽象.继承和动态绑定. 在C++中,用类进行数据抽象,用类派生从一个类继承另一个:派生类继承基类的成员.动态绑定使编译器能够在运行时决定是使用基类中定义的函数还是派生类中定义的函数. 继承和动态绑定在两个方面简化了我们的程序:[继承]能够容易地定义与其他类相似但又不相同的新类,[派生]能够更容易地编写忽略这些相似类型之间区别的程序. 面向对象编程:概述 面向对象编程的关键思想是多态性(polymorphism)

C++:基类和派生类

4.1 派生类的声明 继承实例如下: class Person{ //声明基类Person public: void print() { cout<<"name:"<<name<<endl; cout<<"age:"<<age<<endl; cout<<"sex:"<<sex<<endl; } protected: string name;

c++中基类与派生类中隐含的this指针的分析

先不要看结果,看一下你是否真正了解了this指针? 1 #include<iostream> 2 using namespace std; 3 4 class Parent{ 5 public: 6 int x; 7 Parent *p; 8 public: 9 Parent(){} 10 Parent(int x){ 11 this->x=x; 12 p=this; 13 } 14 virtual void f(){ 15 cout<<"Parent::f()&q

c++基类与派生类之间的转换

1 #include <iostream> 2 #include <string> 3 using namespace std; 4 class Box 5 { 6 public: 7 void setWidth(double width){ 8 this->width=width; 9 } 10 void setHieght(double height){ 11 this->height=height; 12 } 13 void getWidth(){ 14 cout

继承,基类,派生类

在 C++中,继承机制通过类的派生实现,被继承的类称为基类或父类:在继承类的基础上创建的新类称为派生类或子类.派生类的定义格式为:class 派生类名:继承方式基类名 1,继承方式基类名 2,…,继承方式基类名 n{派生类增加的成员声明;};其中,定义中的基类名必须是已有类的名称,派生类名则是新建的类名.一个派生类可以只有一个基类,称为单继承:也可以同时有多个基类,称为多继承.派生类也可作为基类继续派生子类.

基类与派生类的关系

任何一个类都可以派生出一个新类,派生类也可以再派生出新类,因此,基类和派生类是相对而言的. 基类与派生类之间的关系可以有如下几种描述: 1. 派生类是基类的具体化 类的层次通常反映了客观世界中某种真实的模型.在这种情况下,不难看出:基类是对若干个派生类的抽象,而派生类是基类的具体化.基类抽取了它的派生类的公共特征,而派生类通过增加行为将抽象类变为某种有用的类型. 2. 派生类是基类定义的延续 先定义一个抽象基类,该基类中有些操作并未实现.然后定义非抽象的派生类,实现抽象基类中定义的操作.例如,虚

C++学习21 基类和派生类的赋值

在C/C++中,经常会发生数据类型转换,例如整型数据可以赋值给浮点型变量,在赋值之前,先把整型数据转换为浮点型:反过来,浮点型数据也可以赋值给整型变量. 数据类型转换的前提是,编译器知道如何对数据进行取舍.例如: int a = 10.9; printf("%d\n", a); 输出结果为 10,编译器会将小数部分直接丢掉(不是四舍五入).再如: float b = 10; printf("%f\n", b); 输出结果为 10.000000,编译器会自动添加小数部