#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::getLen() { return len; } int MyArray::getMax( ) { return max; } int main(){ int b[10]= {75, 99, 90, 93, 38, 15, 5, 7, 52, 4}; MyArray r1(b,10); cout<<"最大值:"<<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<<"所有元素的和为:"<<s<<endl; return 0; }
原文地址:http://blog.csdn.net/i__am__legend/article/details/45054143