class Complex
{
public:
Complex(int a, int b)
{
this->a = a;
this->b = b;
}
//操作符重载写在全局
friend Complex operator+(Complex &c1, Complex &c2);
friend Complex operator-(Complex &c1, Complex &c2);
//操作符重载写在类里
Complex operator+(Complex &another)
{
Complex temp(this->a + another.a, this->b + another.b);
return temp;
}
Complex operator-(Complex &another)
{
Complex temp(this->a - another.a, this->b - another.b);
return temp;
}
private:
int a;
int b;
};
//操作符重载写在全局
Complex operator+(Complex &c1, Complex &c2)
{
Complex temp(c1.a + c2.a, c1.b + c2.b);
return temp;
}
Complex operator-(Complex &c1, Complex &c2)
{
Complex temp(c1.a - c2.a, c1.b - c2.b);
return temp;
}
+,-重载
最新推荐文章于 2023-05-23 07:02:46 发布
博客围绕加减运算符重载展开,但具体内容缺失。加减运算符重载是编程中重要操作,可让自定义类型使用加减运算,增强代码灵活性和可读性。
667

被折叠的 条评论
为什么被折叠?



