9th Week Item Two-my array class

Source: Internet
Author: User

Code:

/**copyright (c) 2016, College of Computer and Control engineering, Yantai University *all rights reserved.* file name: main.cpp;* Author: Ye Chengyan; * Completion Date: May 6, 2015; * Version number: vc++6.0;** problem description (1) Assigning values to each member function, where ARRAYADDR should be the first address of a new contiguous space to hold the data, and note that the constructor (2) that requires a deep copy copies the values in the array to which a is pointed, one after the other to the newly allocated space. * Program input: none; * Program output: see Run test; */#include <iostream>using namespace Std;class myarray{private:int *arrayaddr;//    Holds the first address of an array with len integer elements int len;//records the length of the dynamic array int max;//The maximum value in the dynamic array (not the data member that must be in the dynamic array) public:myarray (int *a,int n);    ~myarray (); int getValue (int i);//Gets the value of the element labeled I in the array int getlen ();//returns the array length int getmax ();//returns the maximum value in the array};//writes out the definition of each member function Myarray::myarray (i    NT *a,int N) {arrayaddr = new int [n];    Arrayaddr = A; len = n;} Myarray::~myarray () {delete []arrayaddr;} int myarray::getvalue (int i)//Get the element value labeled I for an array of a pointing {return arrayaddr[i];} int Myarray::getlen ()//returns the array length {return len;}    int Myarray::getmax ()//returns the maximum value in the array {max=arrayaddr[0];    for (int i = 0; i < len; i++) {if (Max < arrayaddr[i]) max = Arrayaddr[i]; } return Max;}    int main () {int b[10]={75,99,90,93,38,15,5,7,52,4};    MyArray R1 (b,10);    cout<< "Max:" <<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<< "and for all elements:" <<s<<endl; return 0;}
To run the test:

Summary of Knowledge points:

Deep copy: When the copied object data member is a pointer type, allocate space specifically for it.


9th Week Item Two-my array class

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.