C++重载的例子

此处代码,请思考理解

  • 重载运算符,为什么需要返回引用?
  • 什么是委托构造函数?
  • 什么是类内重载,什么是类外重载?默认调用那个?
  • 数组对象
  • 函数对象(仿函数)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
using namespace std;

class Point {
public:
    Point();
    Point(int x, int y);
    //类内重载
    Point operator+(const Point &);
    Point &operator+=(int);
    int operator[](string);
    int getX() {return x; }
    int getY() {return y; }

private:
    friend Point operator+(const Point &, const Point &);
    friend ostream &operator<<(ostream &, const Point &);
    int x, y;
};

class PPoint {
public:
    PPoint(Point *p) : p(p) {}
    Point *operator->() {return p;}
private:
    Point *p;
};

class ADD {
public:
    ADD(int c) : c(c) {}
    int operator()(int a, int b) {
        return a + b + c;
    } 
private:
    int c;
};

//委托构造函数
Point::Point() : Point(0, 0) {}
Point::Point(int x, int y) : x(x), y(y) {}
//数组对象
int Point::operator[](string s) {
    if (s == "x") return x;
    if (s == "y") return y;
    return 0;
}

ostream &operator<<(ostream &out, const Point &a) {
    out << "(" << a.x << "," << a.y << ")";
    return out;
}

Point &Point::operator+=(int n){
    x += n, y += n;
    return *this;
}
//类内重载
Point Point::operator+(const Point &a) {
    cout << "inner operator+" << endl;
    Point c(x + a.x, y + a.y);
    return c;
}
//类外重载
Point operator+(const Point &a, const Point &b) {
    cout << "outer operator+" << endl;
    Point c(a.x + b.x, a.y + b.y);
    return c;
}

int main() {
    ADD add(5);//仿函数,可调用对象
    cout << add(6, 7) << endl;
    Point a(3, 4);
    Point b(7, 9);
    cout << a["x"] << endl;//数组对象
    cout << a["y"] << endl;
    Point c = a + b;
    cout << a << endl;
    cout << b << endl;
    cout << c << endl;
    (a += 3) += 2;//x和y统一加上3,再加上2
    cout << a << endl;

    PPoint p = &a;
    cout << p->getX() << " " << p->getY() << endl;
    return 0;
}

输出

18
3
4
inner operator+
(3,4)
(7,9)
(10,13)
(8,9)
8 9

struct形式类外重载

#include <iostream>
#include <string>
using namespace std;
struct Point {
    int x;
    int y;
};
struct Vector {
    int x;
    int y;
};

Vector operator+(Vector a, Vector b){
    return Vector{a.x + b.x, a.y + b.y};
}

Point operator+(Point a, Vector b) {
    return Point{a.x + b.x, a.y + b.y};
}

Point operator+(Vector a, Point b) {
    return Point{a.x + b.x, a.y + b.y};
}

int main()
{
    Point a{1, 2};
    Vector b{3, 4};
    
    auto x = b + b;
    cout << "b + b = {" << x.x << ", " << x.y << "}" << endl;
    
    auto y = a + b;
    cout << "a + b = {" << y.x << ", " << y.y << "}" << endl;
    
    auto z = b + a;
    cout << "b + a = {" << z.x << ", " << z.y << "}" << endl;
    
    return 0;
}

输出

b + b = {6, 8}
a + b = {4, 6}
b + a = {4, 6}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

杰之行

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

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

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

打赏作者

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

抵扣说明:

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

余额充值