[c++]代码库
#include<iostream>
using namespace std;
class Data
{
private:
int data;
public:
Data()
{
data=0;
cout<<"Data's default constructor."<<endl;
}
Data(int a)
{
data=a;
cout<<"Data "<<data<<" is created."<<endl;
}
~Data()
{
cout<<"Data "<<data<<" is erased."<<endl;
}
int getValue()
{
return data;
}
void setValue(int a)
{
data=a;
}
};
class DataArray
{
private:
Data da[10];
public:
DataArray()
{
cout<<"DataArray's default constructor."<<endl;
}
void setValues(int d[10])
{
int i;
for(i=0;i<10;i++)
da[i].setValue(d[i]);
}
int getSum()
{
int i,sum=0;
for(i=0;i<10;i++)
sum = sum + da[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;
}
by: 发表于:2017-09-01 09:53:46 顶(0) | 踩(0) 回复
??
回复评论