C++ 运算符重载四(自定义数组类)

Posted 庖丁解牛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 运算符重载四(自定义数组类)相关的知识,希望对你有一定的参考价值。

//自定义数组类
#include<iostream>
using namespace std;

//分析:能获取数组长度,添加元素,删除元素,修改元素
//要求重载[],=,==,!=运算符

class MyArray
{
private:
    int mLength;
    int* mSpace;

public:
    MyArray(int length){
        cout << "有参构造函数被调用了1" << endl;
        mLength = length;
        mSpace = new int[mLength];
        memset(mSpace, 0, sizeof(int)*mLength);
    }
    MyArray(const MyArray& obj){
        cout << "拷贝构造函数被调用了2" << endl;
        mLength = obj.mLength;
        mSpace = new int[mLength];
        int i = 0;
        for (i = 0; i < mLength; i++)
        {
            mSpace[i] = obj.mSpace[i];
        }
    }
    int length(){
        return mLength;
    }
    void setData(int index, int value){
        if (index>(mLength-1))
        {
            cout << "数组越界!" << endl;
            return;
        }
        mSpace[index] = value;
    }
    int getData(int index){
        if (index>(mLength - 1))
        {
            cout << "数组越界!" << endl;
            return 0;
        }
        return mSpace[index];
    }
    void PrintfA(){
        for (int i = 0; i < mLength; i++)
        {
            cout << ""<<i<<"个元素是" << mSpace[i] << endl;
        }
    }
    ~MyArray(){
        cout << "析构函数被调用了3" << endl;
        if (mSpace!=NULL)
        {

            delete[] mSpace;
        }
        mLength = 0;
    }
public:
    //[]操作符重载
    int & operator[](int num){
        if (num>(mLength-1))
        {
            cout << "数组越界!" << endl;
            return mSpace[mLength - 1];
        }
        return mSpace[num];
    }
    //=操作符重载
    MyArray & operator=(MyArray &obj){
        mLength = obj.mLength;
        if (mSpace==NULL)
        {
            mSpace = new int[mLength];
        }
        else{
            //释放原来的数组内存
            delete[] mSpace;
            mSpace = new int[mLength];
        }
        int i = 0;
        for (i = 0; i < mLength; i++)
        {
            mSpace[i] = obj.mSpace[i];
        }
        return *this;
    }
    //==操作符重载
    bool operator==(MyArray &myarr){
        bool flag = true;
        if (mLength != myarr.mLength)
        {
            flag = false;
            return flag;
        }
        for (int i = 0; i < mLength; i++)
        {
            if (mSpace[i] != myarr.mSpace[i])
            {
                flag = false;
                break;
            }
        }
        return flag;
    }
    //!=操作符重载
    bool operator!=(MyArray &myarr){
        bool flag = false;
        if (mLength == myarr.mLength)
        {
            for (int i = 0; i < mLength; i++)
            {
                if (mSpace[i] != myarr.mSpace[i])
                {
                    flag = true;
                    break;
                }
            }
        }
        else{
            flag = true;
        }
        return flag;
    }

};

void ProtectA(){
    MyArray arr(10);
    arr.PrintfA();
    //arr[10];  arr[10]=2;
    //根据需求推理出参数列表是Int  返回值是int &
    //int & arr.operator[](int num)
    arr[5]=5;
    arr.PrintfA();
    //arr1=arr;  赋值操作
    //分析可知:函数名operator=()  ;操作数 2个(不涉及系统类库)arr.operator=(MyArray &arr1) 
    //因为存在 arr1=arr2=arr3; 所以返回值MyArray & ;结果: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();

    
}

void ProtectB(){
    //arr1==arr;  判断是否相等
    //分析 :函数名 operator==(), 有2个操作数  arr1.operator==(arr)  返回值:判断是否相等 那么肯定返回bool值
    //结果:bool operator==(MyArray &myarr)
    MyArray arr1(3), arr2(5);
    if (arr1 == arr2)
    {
        cout << "这两个数组相等" << endl;
    }
    else{
        cout << "这两个数组不相等" << endl;
    }
    if (arr1 != arr2)
    {
        cout << "这两个数不组相等" << endl;
    }
    else{
        cout << "这两个数组相等" << endl;
    }
}

void main(){
    ProtectB();
    system("pause");
}

 

以上是关于C++ 运算符重载四(自定义数组类)的主要内容,如果未能解决你的问题,请参考以下文章

C++类和对象

c++实验四 类的继承派生和多态

c++中加号运算符重载

c++运算符重载

c++自定义数组越异常 ArrayIndexOutOfBoundsException (学习)

c++ 拷贝构造函数与赋值运算符重载函数的区别是