The practice of C + + language Foundation--an array for data members

Source: Internet
Author: User

Back to: Teacher He course teaching link


"Item 5-Array for data member" Reading textbook P255 example 8.4. Note that a data member in a class can be an array. Design a Payroll class (Salary), in which 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 wage entry). The payroll is saved to the salary array, and the actual number is saved in numbers.
    • void Add_salarys (int x): Raise X-dollar wages for everyone
    • void Sort_salarys (): Sort wages
    • void Show_salarys (): Show payroll information
(1) In the main function, define the object of the salary class and enter the salary. Give each person a 500-dollar salary, sort the payroll data, and then output the results.
(2) limited by salary[50]. The actual number of people, will waste space, a lot of people. Unable to complete the task. In Main () first enter the number of employees, as the parameters passed to the input staff member function, and then use the mechanism of dynamic allocation of memory. Open up a continuous space of exactly the right size and finish the work above.
(3) Manually enter wages?! It's so unbearable. Now, including less than 500 workers pay the document Salary.txt (download), read data from the file, completed the above work.
(4) Add a member function. Saves the sorted result to a file.
(5) Organizing the final procedure in a multi-document manner.


[Answer]

(1) The object of the salary class is defined in the main function. Input wages, and then give each person up to 500 yuan wages, sorted after the wage data. And then output the results.

#include <iostream>using namespace Std;class salary{public:void Set_salarys ();     Enter the wage void Add_salarys (int x); Wage rise void Sort_salarys ();     Sort wages void Show_salarys ();    Show wage private:double salarys[50]; wage int number;//actual number};void Salary::set_salarys () {int X,i=0;cin>>x;while (x>0) {salarys[i]=x;  The payroll is saved to the array data member ++i;cin>>x;} number=i;   Number is a data member and records the employee}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]) {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]<< "";} int main () {Salary s;s.set_salarys (); S.add_salarys (+); S.sort_salarys (); S.show_salarys (); return 0;}


(2) with SALARY[50] There are restrictions, the actual number of young people, will waste space, a lot of people. Unable to complete the task. Enter the number of employees in main () first. The member function that is passed to the employee's salary as a parameter. The mechanism of dynamically allocating memory is then utilized. Open up a contiguous space of exactly the right size. Finished the work above.

 #include < Iostream>using namespace Std;class salary{public:void Set_salarys (); void Add_salarys (int x); void Sort_salarys ();    void Show_salarys ();p rivate:double *salarys; Salary, defined as a pointer. In the future point to a dynamic array int number;//actual number};void Salary::set_salarys () {int x,i;cout<< "Please enter the number of employees:"; cin>>number;salarys=  New Double[number]; Allocate the appropriate size space to store the data cout<< "Please enter the employee's salary:"; for (i=0;i<number;++i) {cin>>x;salarys[i]=x;}} 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]) {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]<< "";} int main () {Salary s;s.set_salarys (); S.add_salarys (+); S.sort_salarys (); S.show_salarys (); return 0;} 


(3) Manually enter wages?!

It's so unbearable.

Now, including less than 500 workers pay the document Salary.txt (download), read data from the file, completed the above work.


(4) Add a member function to save the sorted result to a file.

#include <iostream> #include <fstream> #include <cstdlib>using namespace Std;const int N=500;class    Salary{public:void Read_data ();    void Write_data ();    void Add_salarys (int x);    void Sort_salarys (); void Show_salarys ();p rivate:double salarys[n];   Wages, with pointers better int number;    Number};void Salary::read_data () {int i;   Ifstream infile ("Salary.txt", ios::in); Open file as input if (!infile)//test successfully opened {cerr<< "open error!"        <<endl;    Exit (1);    } i=0;    while (Infile>>salarys[i]) i++;    Number=i; Infile.close ();}    void Salary::write_data () {int i;   Ofstream outfile ("Salary_ordered.txt", ios::out); Open file as input if (!outfile)//test successfully opened {cerr<< "open error!"        <<endl;    Exit (1);    } for (i=0; i<number; ++i) {outfile<<salarys[i]<<endl; } outfile.close ();}    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])                {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]<< "\ T";}    int main () {Salary s;    S.read_data ();    S.add_salarys (500);    S.sort_salarys ();    S.write_data ();    S.show_salarys (); return 0;}


(5) Organizing the final procedure in a multi-document manner.

(for a brief answer)



The practice of C + + language Foundation--an array for data members

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.