飛翔(三十八)私の配列類
1298 ワード
#include <iostream>
using namespace std;
class MyArray
{
private:
int *arrayAddr;
int len;
int max;
public:
MyArray(int *a,int n);
~MyArray();
int getValue(int i);
int getLen();
int getMax();
};
MyArray::MyArray(int*a,int n)
{
len=n;
arrayAddr=new int [n];
max=a[0];
for(int i=0;i<n;i++)
{
arrayAddr[i]=a[i];
if(max<a[i])
max=a[i];
}
}
MyArray::~MyArray()
{
delete []arrayAddr;
}
int MyArray::getValue(int i)
{
return arrayAddr[i];
}
int MyArray::getMax()
{
return max;
}
int MyArray::getLen()
{
return len;
}
int main()
{
int b[10]={75,99,90,93,38,15,5,7,52,4};
MyArray r1(b,10);
cout<<"the Max is "<< r1.getMax()<<endl;
int c[15]={18,68,10,52,3,19,12,100,56,96,95,97,1,4,93};
MyArray r2(c,15);
int i,s=0;
for(i=0;i<r2.getLen();i++)
s+=r2.getValue(i);
cout<<"the sum is "<< s<<endl;
return 0;
}
<img src="http://img.blog.csdn.net/20160509091258865?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />