C + + operator overload four (custom array Class)

Source: Internet
Author: User

//Custom Array Classes#include <iostream>using namespacestd;//Analysis: Ability to get array lengths, add elements, delete elements, modify elements//require overloading [],=,==,!= operatorclassmyarray{Private:    intmlength; int*Mspace; Public: MyArray (intlength) {cout<<"The parameter constructor is called 1"<<Endl; Mlength=length; Mspace=New int[Mlength]; memset (Mspace,0,sizeof(int)*mlength); } MyArray (Constmyarray&obj) {cout<<"the copy constructor was called 2"<<Endl; Mlength=obj.mlength; Mspace=New int[Mlength]; inti =0;  for(i =0; i < mlength; i++) {Mspace[i]=Obj.mspace[i]; }    }    intLength () {returnmlength; }    voidSetData (intIndexintvalue) {        if(Index> (mlength-1) ) {cout<<"array out of bounds! "<<Endl; return; } Mspace[index]=value; }    intGetData (intindex) {        if(Index> (Mlength-1) ) {cout<<"array out of bounds! "<<Endl; return 0; }        returnMspace[index]; }    voidPRINTFA () { for(inti =0; i < mlength; i++) {cout<<"Section"<<i<<"an element is"<< Mspace[i] <<Endl; }    }    ~MyArray () {cout<<"destructors are called 3"<<Endl; if(mspace!=NULL) {            Delete[] mspace; } mlength=0; } Public:    //[] operator overloading    int&operator[](intnum) {        if(Num> (mlength-1) ) {cout<<"array out of bounds! "<<Endl; returnMspace[mlength-1]; }        returnMspace[num]; }    //= operator OverloadingMyArray &operator= (MyArray &obj) {Mlength=obj.mlength; if(mspace==NULL) {Mspace=New int[Mlength]; }        Else{            //releasing the original array memory            Delete[] mspace; Mspace=New int[Mlength]; }        inti =0;  for(i =0; i < mlength; i++) {Mspace[i]=Obj.mspace[i]; }        return* This; }    //= = operator overloading    BOOL operator= = (MyArray &Myarr) {        BOOLFlag =true; if(Mlength! =myarr.mlength) {flag=false; returnFlag; }         for(inti =0; i < mlength; i++)        {            if(Mspace[i]! =Myarr.mspace[i]) {Flag=false;  Break; }        }        returnFlag; }    //! = operator overloading    BOOL operator! = (MyArray &Myarr) {        BOOLFlag =false; if(Mlength = =myarr.mlength) { for(inti =0; i < mlength; i++)            {                if(Mspace[i]! =Myarr.mspace[i]) {Flag=true;  Break; }            }        }        Else{flag=true; }        returnFlag; }};voidProtecta () {MyArray arr (Ten); Arr.    PRINTFA (); //ARR[10];    arr[10]=2; //based on demand reasoning out that the parameter list is an int return value is int &//int & arr.operator[] (int num)arr[5]=5; Arr.    PRINTFA (); //Arr1=arr; Assignment Operation//Analysis: Function name operator= (); operand 2 (not involving System class library) arr.operator= (MyArray &arr1)//because there is ARR1=ARR2=ARR3, so the return value MyArray &; Result: MyArray & operator= (MyArray &arr1)MyArray ARR2 (5); arr2[0] =1; arr2[2] =1; arr2[3] =1; arr2[4] =1; arr2[1] =1; Arr=arr2; Arr.    PRINTFA (); }voidPROTECTB () {//Arr1==arr; Judging if they are equal//Analysis: Function name operator== (), there are 2 operand arr1.operator== (arr) return values: determine equality then definitely return bool value//results: bool operator== (MyArray &myarr)MyArray arr1 (3), ARR2 (5); if(arr1 = =arr2) {cout<<"the two arrays are equal"<<Endl; }    Else{cout<<"the two arrays are not equal"<<Endl; }    if(Arr1! =arr2) {cout<<"These two numbers are not equal to each other"<<Endl; }    Else{cout<<"the two arrays are equal"<<Endl; }}voidMain () {PROTECTB (); System ("Pause");}

C + + operator overload four (custom array Class)

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.