📄 l6_3.cpp
字号:
#include "iostream.h"
class CComplex
{
private:
double real;
double imag;
public:
CComplex();
CComplex(double r, double i);
void Set(double r, double i);
CComplex(CComplex &c);
void Print();
CComplex Add(CComplex c);
CComplex Sub(CComplex c);
};
CComplex::CComplex()
{
real = 0.0;
imag = 0.0;
}
CComplex::CComplex (double r, double i)
{
real = r;
imag = i;
}
CComplex::CComplex (CComplex &c)
{
real = c.real;
imag = c.imag;
}
// set thc value of a CComplex
void CComplex::Set(double r, double i)
{
real = r;
imag = i;
}
// display a comnlex
void CComplex::Print()
{
cout << "(" << real << "," << imag << ")" << endl;
}
// return the result of adding two CComplexes
CComplex CComplex::Add(CComplex c)
{
CComplex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
// return the result of substracting two CComplexes
CComplex CComplex::Sub(CComplex c)
{
CComplex temp;
temp.real = real - c.real;
temp.imag = imag - c.imag;
return temp;
}
// main program
void main(void)
{
CComplex a, b(3.0, 4.0), c;
CComplex d(b);
cout << "a = ";
a.Print();
cout << "b = ";
b.Print();
cout << "d = ";
d.Print();
c = b.Add(d);
d = a.Sub(d);
cout << "c = ";
c.Print();
cout << "d = ";
d.Print();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -