Introduction to C++ Operator Overloading

Introduction to C++ Operator Overloading

Operator Overloading is essentially a syntax feature that allows users to customize the behavior of class objects when using operators. Although operator overloading can make the operation of class objects more intuitive and flexible, it is not directly related to the concept of polymorphism.

In C++, operator overloading is a feature that allows users to customize the behavior of objects of class types when operators are used. Through operator overloading, class objects can be operated and manipulated like basic data types.

General syntax for operator overloading:

return value type operator operator (parameter list)

{

    // Implement specific operations

}

Among them, the return value type indicates the type of the operation result, the operator indicates the operator that needs to be overloaded, and the formal parameter list indicates the parameters of the operand.

Operators in C++ can be overloaded as member functions or non-member functions, depending on the nature of the operator. Following are some common examples of operator overloading.

1. Binary arithmetic operator overloading example source code:

#include<iostream> 
using namespace std;

class Complex {
public:
    double real;
    double imag;

    Complex() : real(0), imag(0) {}

    Complex(double r, double i) : real(r), imag(i) {}

    // 加法运算符重载(非成员函数版本)
    Complex operator+(const Complex& other) const {
        return Complex(real + other.real, imag + other.imag);
    }
};

int main() {
    Complex c1(2.0, 3.0);
    Complex c2(4.0, 5.0);
    Complex c3 = c1 + c2;  // 调用operator+运算符重载

    cout << "c3.real = " << c3.real << ", c3.imag = " << c3.imag << endl; //c3.real = 6, c3.imag = 8

    return 0;
}

In the above example, we have defined a class called Complex which represents complex numbers. Then, we overloaded the addition operator + so that two Complex objects can be added.

2. Unary arithmetic operator overloading example source code:

#include <iostream>
using namespace std;

class Vector {
public:
    int x;
    int y;

    Vector() : x(0), y(0) {}

    Vector(int a, int b) : x(a), y(b) {}

    // 一元负号运算符重载(成员函数版本)
    Vector operator-() const {
        return Vector(-x, -y);
    }
};

int main() {
    Vector v(2, 3);
    Vector negV = -v;  // 调用operator-运算符重载

    cout << "negV.x = " << negV.x << ", negV.y = " << negV.y << endl; //negV.x = -2, negV.y = -3

    return 0;
}

In the above example, we defined a class called Vector, which represents a two-dimensional vector. Then, we overload the unary minus operator - so that a Vector object can be negated.

3. Relational operator overloading example source code:

#include <iostream>
using namespace std;

class Point {
public:
    int x;
    int y;

    Point() : x(0), y() {}

    Point(int a, int b) : x(a), y(b) {}

    // 相等运算符重载(非成员函数版本)
    bool operator==(const Point& other) const {
        return (x == other.x && y == other.y);
    }
};

int main() {
    Point p1(2, 3);
    Point p2(2, 3);
    if (p1 == p2) {  // 调用operator==运算符重载
        cout << "p1 and p2 are equal." << endl;
    } else {
        cout << "p1 and p2 are not equal." << endl;
    }

    return 0;
}

In the above example, we defined a class called Point, which represents a point on a two-dimensional plane. Then, we overloaded the equality operator == to compare two Point objects for equality. Output of this example: p1 and p2 are equal.

The scope of operator overloading is limited to the scope of the class, and only affects the operations between objects of this class, and will not affect the operations of other objects. That is to say, operator overloading is only valid inside the class.

It should be noted that operator overloading should be used with caution, and should try to follow the original semantics and idioms of operators. Excessive or inappropriate use of operator overloading can lead to less readable code and produce confusing behavior.

Guess you like

Origin blog.csdn.net/cnds123/article/details/131973207