Project 3 in week 6-fixed salaries, project salaries in week 6
Design a Salary. The data includes the number of employees (number, number of people is not fixed) and the number of employees salary Salary. Enter the employee salary and output the Salary one by one.
Tip: using a fixed array of sizes to store the salaries of a number of employees may result in a waste of space, or applications with too many employees cannot be processed due to insufficient space. Declare salary as a member of the pointer type and allocate a space of exactly the size to store data by dynamically allocating space.
Class Salary {public: Salary (int n); // n indicates the number of employees. space allocation is completed during initialization ~ Salary (); // release the space allocated during initialization in The Destructor void input_salary (); void show_salary (); private: double * salary; int number ;}; // The member functions of the class defined below ...... // The following is the test function int main () {Salary s (10); s. input_salary (); s. show_salary (); return 0 ;}
References (Important Notes ):
/* All rights reserved. * file name: test. cpp * Author: Chen Danni * completion date: April 15, 2015 * version No.: v1.0 */# include <iostream> using namespace std; class Salary {public: Salary (int n ); // n is the number of employees. during initialization, the space is allocated to Salary (const Salary & s); // create a replication constructor ~ Salary (); // release the space allocated during initialization in The Destructor void input_salary (); void show_salary (); private: double * salary; int number ;}; Salary :: salary (int n) {number = n; salary = new double [number]; // determine the size of the allocated space based on the number of people .} Salary: Salary (const Salary & s) {number = s. number; salary = new double [number]; for (int I = 0; I <number; I ++) {* (salary + I) = * (s. salary + I) ;}} Salary ::~ Salary () {delete [] salary; // this step is required. Release the space allocated with new in the Destructor} void Salary: input_salary () {int I; cout <"enter" <number <"employee's salary:" <endl; for (I = 0; I <number; I ++) {cin> * (salary + I);} return;} void Salary: show_salary () {cout <"Employee salary list :"; for (int I = 0; I <number; I ++) {cout <* (salary + I) <";}cout <endl; return ;} int main () {Salary s (10); s. input_salary (); s. show_salary (); return 0 ;}
Experience: Use the replication function flexibly (remember to use the new function to allocate a space that is exactly the same. After the function is completed, use the Destructor delete to release the space we allocated .), Sometimes it is very convenient. Continue to cheer!