[c++]代码库
#include<iostream>
using namespace std;
class Data
{
private:
int data;
public:
Data()
{
data=0;
cout<<"Data's default constructor."<<endl;
}
~Data()
{
cout<<"Data "<<data<<" is erased."<<endl;
}
int getValue()
{
return data;
}
void setValue(int a)
{
data=a;
}
};
class DataArray
{
private:
int num;
Data* da;
public:
DataArray(int n,int *t)
{
num=n;
da=new Data[n];
int i;
for(i=0;i<n;i++)
{
da[i].setValue(t[i]);
}
cout<<"DataArray's constructor."<<endl;
}
int getSum()
{
int i,sum=0;
for(i=0;i<num;i++)
sum = sum + da[i].getValue();
return sum;
}
~DataArray()
{
delete []da;
cout<<"DataArray's deconstructor."<<endl;
}
};
int main()
{
int i, n;
Data test, *testP;
cin>>n;
int tmp[n];
for (i = 0; i < n; i++)
cin>>tmp[i];
DataArray datas(n, tmp);
cout<<"Sum is "<<datas.getSum()<<endl;
testP = new Data;
cin>>n;
testP->setValue(n);
delete testP;
return 0;
}
by: 发表于:2017-09-01 09:53:28 顶(0) | 踩(0) 回复
??
回复评论