📄 operamemb.cpp
字号:
#include <iostream.h>
class Complex
{
private: // 声明为私有成员,能够在成员函数(运算符函数)中访问
float r; // 实部
float i; // 虚部
public:
Complex(float x=0, float y=0) { r=x; i=y; };
Complex operator+(Complex);
void Display() { cout<<r<<'+'<<i<<'i'<<endl; }; // 输出实部和虚部
};
Complex Complex::operator+(Complex other) // 利用成员函数重载运算符
{
Complex temp;
temp.r=this->r+other.r; // 可以省略this指针
temp.i=this->i+other.i;
return temp;
}
void main()
{
Complex complex1(3.34f, 4.8f), complex2(12.8f, 5.2f);
Complex complex;
complex=complex1+complex2; // 进行两个复数的相加运算
complex.Display();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -