4.7自增自减运算符重载

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
class A
{
public:
    int first,second;
    A(int a, int b)
    {
        first = a;
        second = b;
    }
    A()
    {
        first = 0;
        second = 0;
    }
    A operator ++(int k)//后缀
    {
        A tmp(first, second);
        first++;
        second++;
        return tmp;
    }
    A& operator ++()//前缀
    {
        first++;
        second++;
        return *this;
    }
};
int main()
{
    A a(7, 8);
    cout << a.first << ' ' << a.second << endl;
    a++;
    ++a;
    return 0;
}
posted @ 2023-02-21 15:48  穿过雾的阴霾  阅读(19)  评论(0)    收藏  举报