运算符重载入门demo

#include "pch.h"
#include <iostream>
using namespace std;

class A
{
public:
    A(int x, int y)
    {
        this->x = x;
        this->y = y;
    }
    void printA()
    {
        cout << x << " + " << y << "i" << endl;
    }

    int x;
    int y;
};

A myadd(A a, A b)
{
    A temp(a.x + b.x, a.y + b.y);

    return temp;
}
//运算符重载
A operator+(A a, A b)
{
    A temp(a.x + b.x, a.y + b.y);

    return temp;
}

int main()
{
    //int a = 1, b = 2;
    //int c = a + b;

    A a(1, 1), b(2, 2);
    A c1 = myadd(a, b);
    c1.printA();

    A c2 = a + b;
    c2.printA();
    cout << "Hello World!\n"; 
}

 

posted @ 2019-03-21 09:09  星星之火可以燎源  阅读(110)  评论(0编辑  收藏  举报