c++操作符重载

2017-08-14  本文已影响20人  第八区

定义

运算符重载的 实质 是 函数的重载

注意:

示例

//
// Created by eric on 2017/8/14.
//

#ifndef CHONGZAI_ARRAY_H
#define CHONGZAI_ARRAY_H

#include "iostream"

using namespace std;

class Array {
private:
    int mLength;
    int *data;

    friend Array operator+(Array &array1, Array &array2);

    friend ostream &operator<<(ostream &out, Array &array);

    friend istream &operator>>(istream &in, Array &array);

public:
    Array(int len = 0);

    Array(const Array &array);

    Array(const int *arr);

    void setData(int index, int data);

    int getData(int index);

    int &operator[](int index);

    void operator=(Array &array);

    Array &operator()(Array &array);

    void print();

    ~Array();

    int getMLength() const;

    void setMLength(int mLength);

    int *getData() const;

    void setData(int *data);

};


#endif //CHONGZAI_ARRAY_H
//
// Created by eric on 2017/8/14.
//

#include "Array.h"
#include <iostream>

using namespace std;


Array::Array(const int *arr) {

}

void Array::setData(int index, int data) {
    this->data[index] = data;
}

int &Array::operator[](int index) {
    return data[index];
}

Array::Array(int len) {
    mLength = len;
    data = new int[mLength];
}

Array::Array(const Array &array) {
    cout << "copy construct func" << endl;
    if (mLength > 0 && data != NULL) {
        delete[]data;
    }
    mLength = array.getMLength();
    data = new int[mLength];
    for (int i = 0; i < mLength; i++) {
        data[i] = array.getData()[i];
    }
}

int Array::getMLength() const {
    return mLength;
}

void Array::setMLength(int mLength) {
    Array::mLength = mLength;
}

int *Array::getData() const {
    return data;
}

void Array::setData(int *data) {
    Array::data = data;
}

Array::~Array() {
    if (data != NULL) {
        delete[]data;
    }
}

void Array::operator=(Array &array) {
    cout << "operator=" << endl;
    if (mLength > 0 && data != NULL) {
        delete[]data;
    }
    mLength = array.getMLength();
    data = new int[mLength];
    for (int i = 0; i < mLength; i++) {
        data[i] = array.getData()[i];
    }
}

void Array::print() {
    for (int i = 0; i < mLength; i++) {
        cout << data[i] << " ";
    }
    cout << endl;
}

Array &Array::operator()(Array &array) {
    if (mLength >= array.getMLength()) {
        return *this;
    } else {
        return array;
    }
}

Array operator+(Array &array1, Array &array2) {
    cout << "operator+" << endl;
    int len = array1.mLength + array2.mLength;
    Array array(len);
    for (int i = 0; i < array1.mLength; i++) {
        array.setData(i, array1.getData(i));
    }
    for (int i = 0; i < array2.mLength; i++) {
        array.setData(array1.getMLength() + i, array2.getData(i));
    }
//    return array;
    array.print();
    array1.print();
    array2.print();
    return array;

}

int Array::getData(int index) {
    return data[index];
}

ostream &operator<<(ostream &out, Array &array) {
    cout << "operator<<" << endl;
    for (int i = 0; i < array.getMLength(); i++) {
        cout << array.getData(i) << " ";
    }
    cout << endl;
    return out;
}

istream &operator>>(istream &in, Array &array) {
    cout << "&operator>>" << endl;
    for (int i = 0; i < array.mLength; i++) {
        in >> array[i];
    }
    return in;
}
#include <iostream>
#include "Array.h"

using namespace std;

int main() {
    Array array(3);
    for (int i = 0; i < array.getMLength(); i++) {
        array[i] = i * 10;
    }
    array.print();
    Array array1 = array;
    array1.print();
    Array array2(0);
    array2 = array;
    array2.print();
    Array array3(2);
    Array array4 = array3(array);
    array4.print();
    Array array5 = array + array1;
    cout << array5;
    Array array6(5);
    cin >> array6;
    cout << array6;
    return 0;
}
0 10 20
copy construct func
0 10 20
operator=
0 10 20
copy construct func
0 10 20
operator+
0 10 20 0 10 20
0 10 20
0 10 20
operator<<
0 10 20 0 10 20
&operator>>
1
1
2
2
3
3
4
4
5
5
operator<<
1 2 3 4 5

Process finished with exit code 0
上一篇 下一篇

猜你喜欢

热点阅读