Problem D: 类的初体验(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

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;
}
 
代码
#include <iostream>

using namespace std;
class Data
{
    int a;
public:
    Data():a(0){cout<<"Data's default constructor."<<endl;}
    Data(int x):a(x)
    {
        cout<<"Data "<<a<<" is created."<<endl;
    }
    ~Data()
    {
        cout<<"Data "<<a<<" is erased."<<endl;
    }
    int getValue()
    {
        return a;
    }
    void setValue(int y)
    {
        a=y;
    }
};
class DataArray
{
   Data array[10];
    public:
        DataArray(){cout<<"DataArray's default constructor."<<endl;}
         void setValues(int d[10])
         {
             for(int i=0;i<10;i++)
             {
                 array[i].setValue(d[i]);
             }
         }
         int getSum()
         {
             int sum=0;
             for(int i=0;i<10;i++)
             {
                 sum+=array[i].getValue();
             }
             return sum;
         }
         ~DataArray()
         {
             cout<<"DataArray's deconstructor."<<endl;
         }
};
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;
}

上一篇:Eclipse使用Git管理项目


下一篇:《实验数据的结构化程序设计》 2.4.4Calendar个人意见,寻求指引