C++はやるべきことはCとは違うだけだ6個の演算子リロード1、代入演算子、算術演算子、比較演算子

40148 ワード

演算子過負荷1、代入演算子、算術演算子、比較演算子
https://modoocode.com/202
演算子のリロードは、既存の演算子(+、-、%、/、>=、=、+=、-=、++、--、[]など)を上書きすることによって実現され、これらの演算子上で自分の望む論理を実現することができる.
演算子のオーバーロードに注意してください.これは大げさではない.
既存のコードで比較演算子や+、-演算子などを使用すると、作成した関数を変更することなく、既存のコードを変更せずにコードを変更できるので、これは良いことです.
通常、演算子を再ロードするには、次の演算子関数を作成します.
(리턴 타입) operator(연산자)(인자){}
関数名は演算子でなければなりません.演算子と連用する必要があります.つまり、+演算子を再ロードする場合は、operator+を使用します.
1.==演算子の再ロード
`==演算子は過負荷であり、戻り型はboolであるべきである.ちなみに、演算子のオーバーロードの戻り形式は好きなようにすることができます.boolじゃなくdoubleでも大丈夫だけどboolでやるのが基本
次のような顔をしています.
bool operator==(Object obj);
現在,object1 == object2を実行し,内部処理はobject.operator==(object2)である.
#include <iostream>

using namespace std;

class Object{
    private:
        int value;
    public:
        Object(int _value) : value(_value) {};
        bool operator==(Object& obj);
};

bool Object::operator==(Object& obj){
    return value == obj.value;
}

int main(){
    Object obj1(10);
    Object obj2(14);
    cout << (obj1 == obj2) << endl;
}
0
次に示すように、比較演算子obj == objを使用できます.
2、+、-、*、/、%オペレータリロード
値x,yのPointがあります.
#include <iostream>

using namespace std;

class Point{
    private:
        int x;
        int y;
    public:
        Point(int _x, int _y);
};

Point::Point(int _x, int _y) : x(_x), y(_y) {}


int main(){
    Point point1(1,10);
    Point point2(3,5);
}
+演算子を追加したいです.追加する前に、代入演算子をオーバーロードします.代入演算子がオーバーロードされていない場合、結果は保存されません.
Point operator=(const Point& point)
以下のように自分のオペレータに戻ることができます.
より速く演算するために、参照者に戻ることができます.
Point& operator=(const Point& point)
しかし、このように書くと混同される可能性があるので、Point戻り型にしましょう.
#include <iostream>

using namespace std;

class Point{
    private:
        int x;
        int y;
    public:
        Point(int _x, int _y);
        Point operator=(const Point& point);
        void printXY();
};

Point::Point(int _x, int _y) : x(_x), y(_y) {}
Point Point::operator=(const Point& point){
    x = point.x;
    y = point.y;
    return *this;
}
void Point::printXY(){
    cout << "point x : " << x << " y : " << y << endl;
}

int main(){
    Point point1(1,10);
    Point point2(3,5);
    point1 = point2;
    point1.printXY();
}
point x : 3 y : 5
+、-、*、%および/演算子が作成され、returnタイプはPointになります.
#include <iostream>

using namespace std;

class Point{
    private:
        int x;
        int y;
    public:
        Point(int _x, int _y);
        Point operator=(const Point& point);
        Point operator+(const Point& point);
        Point operator-(const Point& point);
        Point operator*(const Point& point);
        Point operator/(const Point& point);
        Point operator%(const Point& point);
        void printXY();
};

Point::Point(int _x, int _y) : x(_x), y(_y) {}
Point Point::operator=(const Point& point){
    x = point.x;
    y = point.y;
    return *this;
}
Point Point::operator+(const Point& point){
    x += point.x;
    y += point.y;
    return *this;
}
Point Point::operator-(const Point& point){
    x -= point.x;
    y -= point.y;
    return *this;
}
Point Point::operator*(const Point& point){
    x *= point.x;
    y *= point.y;
    return *this;
}
Point Point::operator/(const Point& point){
    x /= point.x;
    y /= point.y;
    return *this;
}
Point Point::operator%(const Point& point){
    x %= point.x;
    y %= point.y;
    return *this;
}
void Point::printXY(){
    cout << "point x : " << x << " y : " << y << endl;
}

int main(){
    Point point1(1,10);
    Point point2(3,5);
    
    point1 = point1 + point2;
    point1.printXY();

    point1 = point1 - point2;
    point1.printXY();

    point1 = point1 * point2;
    point1.printXY();

    point1 = point1 / point2;
    point1.printXY();

    point1 = point2 % point1;
    point1.printXY();
}
point x : 4 y : 15
point x : 1 y : 10
point x : 3 y : 50
point x : 1 y : 10
point x : 0 y : 5
好きなようによく撮れていることを確認することができます.
3.<、>、>=、<=比較演算子のロード
今回は比較演算子の再ロードを行います
これは非常に一般的な過負荷であり,構造体やクラスを比較して演算子を過負荷し,並べ替えアルゴリズムを回転させると並べ替えが容易になる.
なぜなら、タイプを変更すると、ソートアルゴリズムにcompare関数を追加する必要がなく、比較演算子をロードするだけでよいからです.
では、上記の例で点を比較する場合、xが大きい場合はtrueを選択し、xが同じ場合はyが大きい場合はtrueを選択します.
すなわち、(1,3) < (3,4)はtrueであり、(1,3) > (1,2)はtrueである.
したがって、比較演算子の戻り値はboolです.
bool operator<(const Point& point)
次のようにしましょう
class Point{
    private:
        int x;
        int y;
    public:
        Point(int _x, int _y);
        Point operator=(const Point& point);
        Point operator+(const Point& point);
        Point operator-(const Point& point);
        Point operator*(const Point& point);
        Point operator/(const Point& point);
        Point operator%(const Point& point);
        bool operator<(const Point& point);
        bool operator<=(const Point& point);
        bool operator>(const Point& point);
        bool operator>=(const Point& point);
        void printXY();
};
宣言の作成:
bool Point::operator<(const Point& point){
    if(x == point.x){
        return y < point.y;
    }
    return x < point.x;
}
bool Point::operator<=(const Point& point){
    if(x == point.x){
        return y <= point.y;
    }
    return x < point.x;
}
bool Point::operator>(const Point& point){
    if(x == point.x){
        return y > point.y;
    }
    return x > point.x;
}
bool Point::operator>=(const Point& point){
    if(x == point.x){
        return y >= point.y;
    }
    return x > point.x;
}
実施書を下記に書いてください.
int main(){
    Point point1(1,10);
    Point point2(2,5);
    Point point3(1,12);
    Point point4(1,5);
    Point point5(1,10);
    
    if( point1 < point2){
        cout <<"point2 is greater than point1" << endl;
    }else{
        cout <<"point1 is greater than point1" << endl;
    }
    cout << "-----------------------------" << endl;
    if( point1 < point3){
        cout <<"point3 is greater than point1" << endl;
    }else{
        cout <<"point1 is greater than point1" << endl;
    }
    cout << "-----------------------------" << endl;
    if( point1 < point4){
        cout <<"point4 is greater than point1" << endl;
    }else{
        cout <<"point1 is greater than point1" << endl;
    }
    cout << "-----------------------------" << endl;
    if(point1 <= point5){
        cout << "point5 is equal and greater than point1" << endl;
    }
}
以下のように駆動すると、結果が得られます.
point2 is greater than point1
-----------------------------
point3 is greater than point1
-----------------------------
point1 is greater than point1
-----------------------------
point5 is equal and greater than point1