第九周-工资

设计一个工资类(Salary),其中的数据成员包括职工人数(number,人数不定)和number个职工的工资salary,要求输入职工工资并逐个输出。

提示:用固定大小的数组存储number个职工的工资,可能造成空间的浪费,也可能会由于空间不够而不能处理职工人数过多的应用。将salary声明为指针类型的成员,通过动态分配空间,分配正好大小的空间存储数据。

<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">class Salary
{
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span>:
    <span class="hljs-title" style="box-sizing: border-box;">Salary</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> n);  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//n为职工人数,初始化时完成空间的分配</span>
    ~Salary();      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//析构函数中释放初始化时分配的空间</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> input_salary();  
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> show_salary();
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span>:
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> *salary;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> number;
};
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//下面定义类的成员函数</span>
……
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//下面是测试函数</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main()
{
    Salary s(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>);
    s.input_salary();
    s.show_salary();
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
</code><p><code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">}</code><code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">
</code><code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">答案</code><code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">
</code><code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">#include<iostream>
using namespace std;
class Salary
{
public:
    Salary(int n);  //n为职工人数,初始化时完成空间的分配
    ~Salary();      //析构函数中释放初始化时分配的空间
    void input_salary();
    void show_salary();
private:
    double *salary;
    int number;
};
Salary::Salary(int n)
{
    number=n;
    salary=new double[n];
}
Salary:: ~Salary()
{
    delete []salary;
}
void Salary::input_salary()
{
    int i;
    for(i=0;i<number;i++)
        cin>>salary[i];
}
void Salary:: show_salary()
{
     int i;
    for(i=0;i<number;i++)
        cout<<salary[i]<<"  ";
}
int main()
{
    Salary s(10);
    s.input_salary();
    s.show_salary();
    return 0;
}
</code></p>

你可能感兴趣的:(第九周-工资)