#include <iostream>
using namespace std;
class Complex
{
friend Complex& operator++(Complex &c1);
friend Complex operator++(Complex &c1, int);
private:
int a;
int b;
public:
Complex& operator--()
{
this->a--;
this->b--;
return *this;
}
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
void printCom()
{
cout << a << "+" << b << "i" << endl;
}
//成员函数 法 实现 - 运算符重载
Complex operator-(Complex &c2)
{
Complex tmp(this->a - c2.a, this->b - c2.b);
return tmp;
}
//后置--
Complex operator--(int)
{
Complex tmp = *this;
this->a--;
this->b--;
return tmp;
}
};
Complex& operator++(Complex &c1)
{
c1.a++;
c1.b++;
return c1;
}
Complex operator++(Complex &c1, int)
{
Complex tmp = c1;
//先使用,再++ 即先把c1先返回去
//return c1; //返回去&
P212 成员函数和友元函数完成一元运算后置++和后置 - -
最新推荐文章于 2021-04-05 11:13:20 发布
本文详细介绍了如何使用C++的成员函数和友元函数来实现一元运算符后置++和--的功能。通过实例代码解析了这两种操作符的重载方法,探讨了它们在类设计中的应用及其对封装性和效率的影响。
摘要由CSDN通过智能技术生成