学习复数类CComplex

C++运算符重载:

使对象的运算表现和编译器内置类型一样

1
2
3
4
5
6
template <typename T>
T sum(T a, T b)
{
return a + b;
}
//如果T为对象,无法相加,需要重载运算符 a.operator+(b)

复数类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class CComplex
{
public:
//CComplex();CComplex(1)给左边;CComplex(1,1);这三个都成立
CComplex(int r=0,int i=0)
:mreal(r),mimag(i) //初始化列表
{

}
//指导编译器怎么做CComplex类的加法运算

CComplex operator+(const CComplex &src)
{
CComplex comp;
comp.mreal = this->meral+src.mreal;
comp.mimag = this->mimag+src.mimag;
return comp;
}
void show()
{
cout<<"("<<mreal<<","<<mimag<<")"<<endl;
}
CComplex operator++(int)
{
CComplex comp=*this;
mreal+=1;
mimage+=1;
return comp; //返回新建的不变的
}
CComplex operator++()
{
mreal+=1;
mimage+=1;
return *this; //返回加了1的旧址
}

private:
int mreal;
int mimag;
friend CComplex operator+(const CComplex &lhs,const CComplex &rhs);//友元函数可以访问私有
friend ostream& operator<<(ostream& os,const CComplex &c);
};
//这是全局的,lhs.mreal访问不了私有,可以定义友元
CComplex operator+(const CComplex &lhs,const CComplex &rhs)
{
return CComplex(lhs.mreal+rhs.mreal,lhs.mimag+rhs.mimag);
}
int main ()
{
CComplex c1(1,2);
CComplex c2(3,4);
// c1.operator+(c2); 加法运算符的重载函数
CComplex c3 = c1 + c2;
//在类中没有找到对应的,在全局中找
CComplex c4 = 10+c1;
CComplex c5 = c1++; //++是单目运算符,operator++()前置++,operator++(int)后置++
c1.show();
c5.show();

return 0;
}

1
2
3
4
5
6
template <typename T>
void show(T a)
{
cout<<a<<endl; //a若是int无问题,若a是别的自定义类,会报错
}

1
2
3
4
5
6
7
8
9
10
11
12
13
//全局,cout重写
ostream& operator<<(ostream& out,const CComplex &src)
{
out<<"mreal:"<<src.mreal<<"mimage:"<<src.mimage<<endl;
return out;
}
//重写cin
istream& operator>>(istream& in, CComplex& c)
{
in >> c.mreal >> c.mimag;
return in;
}