Item 3-the variable wage category

Source: Internet
Author: User

"Item 3-the variable wage category"
Design a wage class (Salary), in which the data members include the number of employees (number, variable) and the salary Salary of numbers of employees, the input of staff wages and output.
Tip: Storing the salary of a number of employees with a fixed size array may result in wasted space and may not be able to handle too many employees due to lack of space. Declare salary as a member of a pointer type by dynamically allocating space and allocating exactly the size of space to store the data.

Class Salary{public:    Salary (int n);  n is the number of employees, and the allocation of space is    ~salary () when the initialization is completed;      The Space    void input_salary () allocated when initializing is released in the destructor;      void Show_salary ();p rivate:    double *salary;    int number;};/ /The following defines the member functions of the class ...//The following is the test function int main () {    Salary s ();    S.input_salary ();    S.show_salary ();    return 0;}

#include <iostream>using namespace Std;class salary{public:salary (int n);    Salary (const Salary &s);    ~salary ();    void Input_salary ();    void Show_salary ();p rivate:double *salary; int number;};    Salary::salary (int n) {number=n; Salary=new double[n];}    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;}    void Salary::input_salary () {cout<< "Please enter this" <<number<< "Employee's salary" <<endl;    for (int i=0;i<number;i++) {cin>>* (salary+i);    } cout<<endl; return;}    void Salary::show_salary () {cout<< "This" <<number<< "Employee's payroll list:" <<endl;    for (int i=0;i<number;i++) {cout<<* (salary+i) << "";    }}int Main () {Salary S1 (10);    S1.input_salary ();    Salary S2 (S1);    S2.show_salary (); return 0;}



Item 3-the variable wage category

Related Article

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.