?? c++
字號:
#include <iostream.h>
//定義復數類
class complex{
float real; //實部
float image; //虛部
public:
//重載的運算符"+"的原型
complex operator+ (complex right);
//重載賦值運算符"="的定義
complex operator= (complex right);
void set_complex(float re, float im);
void put_complex(char *name);
};
//重載加法運算符"+"的定義
complex complex::operator+ (complex right) {
complex temp;
temp.real = this->real + right.real;
temp.image = this->image + right.image;
return temp;
}
//重載加賦值運算符"="的定義
complex complex::operator= (complex right) {
this->real = right.real;
this->image = right.image;
return *this;
}
//定義set_complex()成員函數
void complex::set_complex(float re, float im) {
real = re;
image = im;
}
//定義put_complex()成員函數
void complex::put_complex(char *name) {
cout<<name<<": ";
cout << real << ' ';
if (image >= 0.0 ) cout << '+';
cout << image << "i\n";
}
//在main()函數中使用complex類的對象
main(void)
{
complex A, B, C; //創建復數對象
//設置復數變量的值
A.set_complex(1.2, 0.3);
B.set_complex(-0.5, -0.8);
//顯示復數數據
A.put_complex("A");
B.put_complex("B");
//賦值運算,顯示結果
C = A;
C.put_complex("C=A");
//加法及賦值運算,顯示結果
C = A + B;
C.put_complex("C=A+B");
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -