• Problem B: 类的初体验(IV)


    Description

    定义一个类Data,只有一个int类型的属性和如下方法:

    1.   缺省构造函数,将属性初始化为0,并输出"Data's default constructor."。

    2.  带参构造函数,将属性初始化为指定参数,并输出"Data # is created.",其中“#”即参数值。

    3. 析构函数,输出:"Data # is erased.",其中“#”是属性值。

    4.   int getValue()——获得属性值。

    5.    void setValue(int)——设置属性值。

    定义一个由Data的对象组成的数组类DataArray,包括:

    1. 由10个Data类的对象组成的对象数组。

    2. 缺省构造函数——不做任何操作,仅输出一行"DataArray's default constructor."。

    3. void setValues(int d[10])——设置10个Data的对象的属性值分别为参数d的10个值。

    4. int getSum()——求10个Data对象属性值的和。

    5. 析构函数——输出一行"DataArray's deconstructor."。

    Input

    输入为11个int类型范围内的整数。

    Output

    见样例~~~

    Sample Input

    1 2 3 4 5 6 7 8 9 10 100

    Sample Output

    Data's default constructor. Data's default constructor. Data's default constructor. Data's default constructor. Data's default constructor. Data's default constructor. Data's default constructor. Data's default constructor. Data's default constructor. Data's default constructor. DataArray's default constructor. Sum is 55 Data 100 is created. aData = 100 Data 100 is erased. DataArray's deconstructor. Data 10 is erased. Data 9 is erased. Data 8 is erased. Data 7 is erased. Data 6 is erased. Data 5 is erased. Data 4 is erased. Data 3 is erased. Data 2 is erased. Data 1 is erased.

    HINT

    Append Code

    #include<iostream>
    using namespace std;
    class Data
    {
    private:
        int t;
    public:
     
        Data(){cout<<"Data's default constructor. ";}
        Data(int a){t=a;cout<<"Data "<<t<<" is created. ";}
        ~Data(){cout<<"Data "<<t<<" is erased. ";}
        int getValue(){return t;}
        void setValue(int a){t=a;}
    };
    class DataArray
    {
    private:
        Data tt[10];
    public:
        DataArray(){cout<<"DataArray's default constructor. ";}
        void setValues(int d[10])
        {
            for(int i=0;i<10;i++)
                tt[i].setValue(d[i]);
        }
        int getSum()
        {
            int sum=0;
            for(int i=0;i<10;i++)
            sum+=tt[i].getValue();
            return sum;
        }
        ~DataArray(){cout<<"DataArray's deconstructor. ";}
    };
    int main()
    {
        DataArray dataArray;
        Data *aData;
        int data[10], tmp;
        int i;
        for (i = 0; i < 10; i++){
            cin>>data[i];
        }
        dataArray.setValues(data);
        cout<<"Sum is "<<dataArray.getSum()<<endl;
     
        cin>>tmp;
        aData = new Data(tmp);
        cout<<"aData = "<<aData->getValue()<<endl;
        delete aData;
        return 0;
    }
  • 相关阅读:
    哈希表--扩展数组
    哈希表效率
    P=(1+1/(1-L))/2
    函数推进
    简单函数2
    简单函数
    getting data from the keybroad
    nutch-2.2.1 hadoop-1.2.1 hbase-0.92.1 集群部署(实用)
    hbase zookeeper独立搭建
    Orchard 介绍
  • 原文地址:https://www.cnblogs.com/TogetherLaugh/p/6544674.html
Copyright © 2020-2023  润新知