⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 10章 虚函数与多态性.txt

📁 C++大学教程txt版中文版 C++大学教程txt版中文版
💻 TXT
📖 第 1 页 / 共 5 页
字号:
84   virtual double earnings() const;
85   virtual void print() const;
86 private:
87   double weeklySalary;
88 };
89
90 #endif
91 // Fig. 10.1: boss1.cpp
92 // Member function definitions for class Boss
93 #include "boss1.h"
94
95 // Constructor function for class Boss
96 BOSS::BOSS( const char *first, const char *last, double s )
97   : Employee( first, last )  // call base-class constructor
98 { setWeeklySalary( s ); }
99
100 // Set the Boss's salary
101 void Boss::setWeeklySalary( double s )
102   { weeklySalary = s > 0 ? s : 0; }
103
104 // Get the BOSS'S pay
105 double Boss::earnings() const  { return weeklySalary; }
106
107 // Print the BOSS'S name
108 void Boss::print() const
109 {
110   cout << "\n          Boss: ";
111   Employee::print();
112 }
113 / Fig. 10.1: commisl.h
115 #ifndef COMMIS1_H
116 #define COMMIS1_H
117 #include "employ2.h"
118
119 class commissionWorker : public Employee {
120 public:
121   CommissionWorker( const char *, const char *,
122                     double = 0.0, double = 0.0,
123                     int= 0 );
124   void setSalary( double );
125   void setCommission( double );
126   void setQuantity( int );
127   virtual double earnings() const;
128   virtual void print() const;
129 private:
130   double salary;      // base salary per week
131   double commission;  // amount per item sold
132   int quantity;      // total items sold for week
133 };
134
135 #endif
136 // Fig. 10.1; commis1.cpp
137 // Member function definitions for class CommissionWorker
138 #include <iostream.h>
139 #include "commis1.h"
140
141 // Constructor for class CommissionWorker
142 CommissionWorker::CommissionWorker( const char * first,
143               const char *last, double s, double c, int q )
144   : Employee( first, last )  // call base-class constructor
145 {
146   setSalary( s );
147   setCommission( c );
148   setQuantity( q );
149 }
150
151 // Set CommissionWorker's weekly base salary
152 void CommissionWorker::setSalary( double s )
153   { salary = s > 0 ? s : 0; }
154
155 // Set CommissionWorker's conunission
156 void CommissionWorker::setCommission( double c )
157   { commission = c > 0 ? c : 0; }
158
159 // Set commissionWorker's quantity sold
160 void CommissionWorker::setQuantity( int q )
161   { quantity = q > 0 ? q : 0; }
i62
163 // Determine CommissionWorker's earnings
164 double CommissionWorker::earnings() const
165   { return salary + commission * quantity; }
166
167 // Print the CommissionWorker's name
168 void CommissionWorker::print() const
169 {
170   cout << "\nCommission worker: ";
171   Employee::print();
172 }
173 // Fig. 10.1: piecel.h
174 // pieceWorker class derived from Employee
175 #ifndef PIECE1_H
176 #define PIECE1_H
177 #include "employ2.h"
178
179 class PieceWorker : public Employee {
180 public:
181   PieceWorker( const char *, const char *,
182   double = 0.0, int = 0);
183   void setWage( double );
184   void setQuantity( int );
185   virtual double earnings() const;
186   virtual void print() const;
187 private:
188   double wagePerPiece; // wage for each piece output
189   int quantity;       // output for week
190 };
191
192 #endif
193 // Fig. 10.1: piecel.cpp
194 // Member function definitions for class pieceWorker
195 #include <iostream.h>
196 #include "piecel.h"
197
198 // Constructor for class PieceWorker
199 pieceWorker::pieceWorker( const char *first, const char *last,
200                          double w, int q )
201   : Employee( first, last )  // call base-class constructor
202 {
203   setWage( w );
204   setQuantity( q );
205 }
206
207 // Set the wage
208 void PieceWorker::setwage( double w )
209   { wagePerPiece = w > 0 ? w : 0; }
210
211 // Set the number of items output
212 void PieceWorker::setQuantity( int q )
213   { quantity = q > 0 ? q : 0; }
214
215 // Determine the PieceWorker's earnings
216 double PieceWorker::earnings() const
217   { return quantity * wagePerPiece; }
218
219 // Print the PieceWorker's name
220 void PieceWorker::print() const
221 {
222   cout << "\n    Piece worker: ";
223   Employee::print();
224 }
225 // Fig. 10.1: hourlyl.h
226 // Definition of class HourlyWorker
227 #ifndef HOURLY1_H
228 #define HOURLY1_H
229 #include "employ2.h"
230 
231 class HourlyWorker : public Employee {
232 public:
233   HourlyWorker( const char *, const char *,
234                 double = 0.0, double = 0.0);
235   void setWage( double );
236   void setHours( double );
237   virtual double earnings() const;
238   virtual void print () const;
239 private:
240   double wage;  // wage per hour
241   double hours;  // hours worked for week
242 } ;
243
244 #endif
245 // Fig. 10.1: hourly1.cpp
246 // Member function definitions for class HourlyWorker
247 #include <iostream.h>
248 #include "hourly1.h"
249
250 // Constructor for class HourlyWorker
251 HourlyWorker::HourlyWorker( const char *first,
252                             const char *last,
253                             double w, double h }
254   : Employee( first, last )  // call base-class constructor
255 {
256    setwage( w );
257    setHours( h );
258 }
259
260 // Set the wage
261 void HourlyWorker::setwage( double w )
262   { wage = w > 0 ? w : 0; }
263
264 // Set the hours worked
265 void HourlyWorker::setHours( double h )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -