Project 3 in week 6-fixed salaries, project salaries in week 6

Source: Internet
Author: User

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!

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.