C++,运算符重载,复数类

(1)定义一个复数类(实部和虚部),通过成员函数的方式编写自增一元运算符的重载函数,重载函数需要分别对实部与虚部都加1,要求在主函数初始化一个复数对象并显示实部与虚部的值,并进行自增运算符计算再次显示。

#include <iostream>

using namespace std;

class Complex
{
public:
    Complex() = default;

    Complex(float r, float i)
        : real(r),
          img(i)
    {
    }

    Complex &operator++()
    {
        real++;
        img++;
        return *this;
    }

    Complex operator++(int)
    {
        Complex old(*this);
        ++*this;
        return old;
    }

    float real = 0.0f;
    float img = 0.0f;
};

int main()
{
    Complex c(2.0f, 5.0f);
    cout << "实部: " << c.real << endl;
    cout << "虚部: " << c.img << endl;
    ++c;
    cout << "自增后的实部: " << c.real << endl;
    cout << "自增后的虚部: " << c.img << endl;
}