WEEK 1 computer task project 1-create a special array class to process operations on Arrays

Source: Internet
Author: User
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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.