C + + Practice reference--implementation of array class operation

Source: Internet
Author: User

"Project-Implementation of array class operations"
To design the array class arrays, in order to implement the functions required in the test function, make up the functions (constructs, destructors) and operator overloads of the related functions.
Implement the policy tip: You can add comments to the statement in the test function, uncomment the sentence, add the corresponding function, to gradually achieve all the functions, to avoid the difficulties of overall consideration.

classarray{Private:int*List;//For storage of dynamically allocated array memory first address    intSize//array size (number of elements) Public://member function declaration};//Require the test function to run the correct and reasonable results:intMain () {inta[8]= {1,2,3,4,5,6,7,8};intb[8]= {Ten, -, -, +, -, -, -, the}; Array Array1 (A,8), Array3,array4;ConstArray Array2 (b,8);    Array4=array3=array1+array2;    Array3.show (); Array4.resize ( -); array4[8]= About;cout<<array4[8]<<endl;cout<<array2[3]<<endl;return 0;}

[Reference Solution]

#include <iostream>#include <iomanip>#include <cassert>using namespace STD;classarray{Private:int*List;//For storage of dynamically allocated array memory first address    intSize//array size (number of elements) Public: Array (intSZ = -);//ConstructorsArray (intA[],intSZ);//ConstructorsArray (ConstArray &a);//Copy constructor~array ();//destructorArrayoperator+ (ConstArray &a2);//Reload "="Array &operator= (ConstArray &a2);//Reload "="    int&operator[] (inti);//Reload "[]"    Const int&operator[] (intIConst;intGetSize ()Const;//Take the size of the array    voidResizeintSZ);//Modify the size of the array    voidShow ()Const;}; Array::array (intSz//Constructors{Assert (SZ >=0);//sz Array size (number of elements), should be non-negativesize = SZ;//Assign the number of elements to the variable size    List=New int[Size];//dynamically allocates an element space of size int type}array::array (intA[],intSZ) {assert (SZ >=0);//sz Array size (number of elements), should be non-negativesize = SZ;//Assign the number of elements to the variable size    List=New int[Size];//dynamically allocates an element space of size int type     for(inti =0; i < size; i++)//Copy array elements from Object X to this object        List[i] = a[i];} Array::~array ()//destructor{Delete[]List;}//Copy constructorArray::array (ConstArray &a) {size = A.size;//Gets the array size from Object X and assigns values to the members of the current object    //Request memory for an object and check for errors    List=New int[Size];//dynamically allocates element spaces of n int types     for(inti =0; i < size; i++)//Copy array elements from Object X to this object        List[I] = a.List[i];} Array array::operator+ (ConstArray &a2) {assert (size = = a2.size);//Check if subscript is out of bounds    //If the array size in this object is different from A2, delete the original memory of the array and reassignArray total (size); for(inti =0; i < size; i++) total.List[I] =List[I]+A2.List[i];returnTotal//Returns a reference to the current object}//overloads the "=" operator to assign the object A2 to this object. Implementing an overall assignment between objectsArray &array::operator= (Constarray& A2) {if(&a2! = This)    {//If the array size in this object is different from A2, delete the original memory of the array and reassign        if(Size! = a2.size) {Delete[]List;//delete array original memorysize = A2.size;//Set the array size of this object            List=New int[Size];//re-allocate memory for n elements}//Copy array elements from Object X to this object         for(inti =0; i < size; i++)List[i] = A2.List[i]; }return* This;//Returns a reference to the current object}//overloaded subscript operator, which accesses elements as normal arrays, and has cross-check functionalityint&array::operator[] (intN) {assert (n >=0&& n < size);//Check if subscript is out of bounds    return List[n];//Returns an array element with the subscript n}///constant object, this function will be called, and the result (reference) of the operation will no longer be assignedConst int&array::operator[] (intNConst{ASSERT (n >=0&& n < size);//Check if subscript is out of bounds    return List[n];//Returns an array element with the subscript n}//Take the size of the current arrayintArray::getsize ()Const{returnSize;}//Change the size of the array to SZvoidArray::resize (intSZ) {assert (SZ >=0);//Check if SZ is non-negative    if(SZ = = size)//If the specified size is the same as the original size, do nothing        return;int* NewList =New int[SZ];//Request a new array memory    intn = (sz < size)? Sz:size;//Assign SZ to the smaller one in size to n    //Copy the first n elements of the original array into the new array     for(inti =0; I < n; i++) Newlist[i] =List[i];Delete[]List;//Delete the original array    List= NewList;//Make list point to new arraysize = SZ;//Update size}voidArray::show ()Const{ for(inti =0; i < size; i++)cout<<List[i]<<" ";cout<<endl;}intMain () {inta[8]= {1,2,3,4,5,6,7,8};intb[8]= {Ten, -, -, +, -, -, -, the}; Array Array1 (A,8), Array3,array4;ConstArray Array2 (b,8);    Array4=array3=array1+array2;    Array3.show (); Array4.resize ( -); array4[8]= About;cout<<array4[8]<<endl;cout<<array2[3]<<endl;return 0;}

C + + Practice reference--implementation of array class operation

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.