* Copyright (c) 2015, Yantai University School of Computer * All rights reserved. * File name: Test.cpp *: Liu Chang * completion Date: March 21, 2015 * version number: v1.0 * * Problem Description: Read the textbook P255 Example 8.4, notice that a data member in a class can be an array 。 Design a Payroll class (Salary), where the data members of the class are as follows: class Salary { private: double salarys[50];//multi-person wage int number ; Actual number }; The member functions to be designed are: void Set_salarys (): Enter the employee's salary (input-1 marks the end of the payroll input), the wages are saved to the salary array, the actual number is saved to the numbers; void Add_salarys ( int x): Give everyone a raise x yuan pay void Sort_salarys (): Sort the wages by void Show_salarys (): Show payroll information (2) with SALARY[50] There is a limit, the actual number of people, will waste space, There are too many people to complete the task. In Main () first enter the number of employees, as a parameter passed to the member function of input workers ' wages, and then use the mechanism of dynamic allocation of memory, to open up a continuous space of the size exactly, complete the above work. * Input Description: NULL; * Program output: Output as required.
The code is as follows:
#include <iostream>using namespace Std;class salary{public:void Set_salarys (); Enter the employee's salary (input-1 is the end of the payroll input), and the payroll is saved to the salary array, and the actual number is saved in the numbers. void Add_salarys (int x); Give everyone a raise x yuan pay void Sort_salarys (); Sort of the Payroll void Show_salarys (); Display wage information private:double *salarys; Multi-person salary int number; Actual number};int Main () {Salary s; S.set_salarys (); S.show_salarys (); S.add_salarys (500); S.sort_salarys (); S.show_salarys (); return 0;} void Salary::set_salarys () {int i; Double money; cout<< "Please enter the number of employees:"; cin>>number; Salarys=new Double[number]; cout<< "Please enter the employee's salary:"; for (i=0;i<number;i++) {cin>>money; Salarys[i]=money; }}void Salary::add_salarys (int x) {int i; for (i=0;i<number;i++) salarys[i]+=x;} void Salary::sort_salarys () {int i,j; Double T; for (i=0;i<number-1;i++) for (j=0;j<number-i-1;j++if (salarys[j]<salarys[j+1])//from high to low arrangement {T=SALARYS[J]; SALARYS[J]=SALARYS[J+1]; salarys[j+1]=t; }}void Salary::show_salarys () {int i; for (i=0;i<number;i++) Cout<<salarys[i]<<endl;}
Operation Result:
3rd week Item 5-Array as data member (2)