01. /* 02. * copyright and version Declaration of the program part 03. * copyright (c) 2013, Yantai University Computer college student 04. * All rightsreserved.05. * file name: myarray. cpp06. * Prepared by Zhao guanzhe 07. * Completion Date: April 8, June 7, 2013. * version: v1.009. * input Description: 10. * Problem description: 11. */# include <iostream> using namespace STD; Class myarray {PRIVATE: int * arr; // used to store the int size of the first address of the dynamically allocated Array Memory; // array size public: myarray (int sz = 50); myarray (int A [], int sz); // initialize myarray (const myarray & A) by an array of built-in types ); // copy the constructor ~ Myarray (void); // destructor. Pay attention to releasing space myarray & operator = (const myarray & ); // reload "=" so that the array object can be assigned bool operator = (myarray & A); // reload =, so that the array object can determine whether the two arrays are equal (the size is equal and the corresponding elements are equal) friend ostream & operator <(ostream & out, myarray & A); // reload <, output array int getsize (void) const; // obtain the array size;}; // The following is the definition of the class member function myarray: myarray (INT sz) {size = SZ; arr = new int [size]; for (INT I = 0; I <size; ++ I) * (ARR + I) = 0;} myarray :: myarray (int A [], int sz) {siz E = SZ; arr = new int [size]; for (INT I = 0; I <size; ++ I) * (ARR + I) = * (a + I);} myarray: myarray (const myarray & A) {size =. size; arr = new int [. size]; for (INT I = 0; I <. size; ++ I) * (ARR + I) = * (. arr + I);} myarray ::~ Myarray (void) {Delete [] arr;} myarray & myarray: Operator = (const myarray & A) {size =. size; arr = new int [. size]; for (INT I = 0; I <. size; ++ I) * (ARR + I) = * (. arr + I); return * This;} bool myarray: Operator = (myarray & A) {bool compare = false; double sum1 = 0, sum2 = 0; for (INT I = 0; I <size; ++ I) {sum1 = sum1 + * (ARR + I); sum2 = sum2 + * (. arr + I);} If (sum1 = sum2) Compare = true; return compare;} ostream & operator <(ostream & output, myarray &) // overload <, output Array {for (INT I = 0; I <. size; ++ I) Output <* (. arr + I) <"; cout <Endl; return output;} int myarray: getsize (void) const // get the array size; {return size ;} // test the function int main () {int A [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int B [10] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; myarray arr1 (A, 10); // test the function of initializing the newly defined array object myarray arr2 (B, 10) with a built-in array; myarray arr3 (10 ); // test the cout <arr1; // reload cout <arr2; // test
Running result: