09-C++常见运算符重载

 V信公众号:  程序员架构笔记

运算符重载(Operator Overloading)是C++中的一种特性,允许程序员为用户定义的类型(如类或结构体)重新定义或重载已有的运算符。通过运算符重载,可以使自定义类型的对象像内置类型一样使用运算符进行操作,从而增强代码的可读性和简洁性。

例如,你可以重载 + 运算符,使得两个自定义的 Point 类对象可以直接相加,而不需要调用特定的成员函数。

常见运算符的重载

以下是一些常见运算符的重载示例:

1. 重载 + 运算符
class Point {
public:
    int x, y;

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

    // 重载 + 运算符
    Point operator+(const Point& other) const {
        return Point(x + other.x, y + other.y);
    }
};

int main() {
    Point p1(1, 2);
    Point p2(3, 4);
    Point p3 = p1 + p2;  // 使用重载的 + 运算符
    std::cout << "p3.x = " << p3.x << ", p3.y = " << p3.y << std::endl;
    return 0;
}
2. 重载 - 运算符
class Point {
public:
    int x, y;

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

    // 重载 - 运算符
    Point operator-(const Point& other) const {
        return Point(x - other.x, y - other.y);
    }
};

int main() {
    Point p1(5, 6);
    Point p2(3, 4);
    Point p3 = p1 - p2;  // 使用重载的 - 运算符
    std::cout << "p3.x = " << p3.x << ", p3.y = " << p3.y << std::endl;
    return 0;
}
3. 重载 * 运算符
class Point {
public:
    int x, y;

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

    // 重载 * 运算符
    Point operator*(int scalar) const {
        return Point(x * scalar, y * scalar);
    }
};

int main() {
    Point p1(2, 3);
    Point p2 = p1 * 3;  // 使用重载的 * 运算符
    std::cout << "p2.x = " << p2.x << ", p2.y = " << p2.y << std::endl;
    return 0;
}
4. 重载 / 运算符
class Point {
public:
    int x, y;

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

    // 重载 / 运算符
    Point operator/(int scalar) const {
        return Point(x / scalar, y / scalar);
    }
};

int main() {
    Point p1(6, 9);
    Point p2 = p1 / 3;  // 使用重载的 / 运算符
    std::cout << "p2.x = " << p2.x << ", p2.y = " << p2.y << std::endl;
    return 0;
}
5. 重载 == 运算符
class Point {
public:
    int x, y;

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

    // 重载 == 运算符
    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) {  // 使用重载的 == 运算符
        std::cout << "p1 and p2 are equal" << std::endl;
    } else {
        std::cout << "p1 and p2 are not equal" << std::endl;
    }
    return 0;
}
6. 重载 << 运算符(输出流运算符)
class Point {
public:
    int x, y;

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

    // 重载 << 运算符
    friend std::ostream& operator<<(std::ostream& os, const Point& p) {
        os << "(" << p.x << ", " << p.y << ")";
        return os;
    }
};

int main() {
    Point p(4, 5);
    std::cout << "Point: " << p << std::endl;  // 使用重载的 << 运算符
    return 0;
}
7. 重载 >> 运算符(输入流运算符)
class Point {
public:
    int x, y;

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

    // 重载 >> 运算符
    friend std::istream& operator>>(std::istream& is, Point& p) {
        is >> p.x >> p.y;
        return is;
    }
};

int main() {
    Point p;
    std::cout << "Enter x and y: ";
    std::cin >> p;  // 使用重载的 >> 运算符
    std::cout << "Point: (" << p.x << ", " << p.y << ")" << std::endl;
    return 0;
}

注意事项

  1. 不能重载的运算符:C++中有一些运算符不能被重载,例如 ::(作用域解析运算符)、.(成员访问运算符)、.*(成员指针访问运算符)、?:(三元条件运算符)等。

  2. 运算符重载的限制:运算符重载不能改变运算符的优先级和结合性,也不能改变运算符的操作数个数。

  3. 保持一致性:重载运算符时,应尽量保持其原有的语义。例如,+ 运算符通常用于表示加法操作,重载时也应保持这种语义。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员架构笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值