📄 操作符.cpp
字号:
#include <iostream.h>
class Complex
{
private:
double real,imag;
public:
void set(double r,double i=0.0);
void print();
Complex operator+(Complex c);
Complex operator-(Complex c);
};
int main(void);
void Complex::set(double r,double i)
{
real = r;
imag = i;
}
void Complex::print()
{
cout << "(" << real << "," << imag << " )";
}
Complex Complex::operator +(Complex c)
{
Complex temp;
temp.real=real+c.real;
temp.imag=imag+c.imag;
return temp;
}
Complex Complex::operator -(Complex c)
{
Complex temp;
temp.real=real-c.real;
temp.imag=imag-c.imag;
return temp;
}
int main(void)
{
Complex a,b,c,d;
a.set(1.0,1.0);
b.set(2.0,2.0);
c=a+b;
d=b+c-a;
cout << "c=";
c.print();
cout << "and ";
cout << "d=";
d.print();
cout <<"\n";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -