```cpp
//头文件的写法
/*
#ifndef __COMPLEX__
#define __COMPLEX__
----------------------------------------
#include
class ostream;
class complex;
complex&
__doapl (complex* ths,const complex& r);
(forward declarations)
----------------------------------------
class complex
{
...
};
(class declarations)
----------------------------------------
complex::function ...
(class definition)
#endif
*/
//class template(模板)简介
/*
template
class complex
{
public:
complex(T r = 0, T i = 0)
:re(r), in(i)
{ }
complex& operator += (const complex&);
T real() const { return re; }
T imag() const { return im; }
private:
T re, im;
friend complex& __doapl(complex*, const complex&);
};
{
complex c1(2.5, 1.5)
complexc2(2, 6);
...
}
*/
//inline(内联)函数
/*
class complex
{
public:
complex(double r = 0, double i = 0) //默认实参
:re(r), in(i)
{ }
complex& operator += (const complex&);
double real() const { return re; }
double imag() const { return im; }
private:
double re, im;
friend complex& __doapl(complex*, const complex&);
};
inline double
imag(const complex& x)
{
return x.imag();
}
*/
//access level(访问级别)
/*
{
complex c1(2,1);
cout << c1.real();
cout << c1.imag();
}
*/
//constructor(ctor,构造函数)
/*
{
complex c1(2,1);
complex c2;
complex* p = new complex(4);
}
*/
//重载overloading
//1. 同名函数可以同时存在
//2. 函数重载常常发生在构造函数上
//3. 若已经有默认值的构造函数,还是可以通过重载写出其他构造函数
/*
complex(double r = 0, double i = 0) //默认实参
:re(r), in(i)
{ }
complex () :re(0), im(0) { } //无参数
上述同名不成立
----
{
complex c1;
complex c2;
}
*/
//const member functions(常量成员函数)
//命名了const修饰的函数则不可被改变
//friend(友元)
/*
class complex
{
public:
complex (double r = 0, double i = 0)
:re (r), im (i)
{ }
complex& operator += (const complex&);
double real () const { return re; }
double imag () const { return im; }
private:
double re , im;
friend complex& __doapl (complex*, const complex&);
};
inline complex&
__doapl (complex* ths, const complex& r)
{
ths->re += r.re;
ths->im += r.im; ------自由取得friend的private成员
return *ths;
}
*/
//相同class的各个objects互为friends(友元)
/*
class complex
{
public:
complex (double r = 0, double i = 0)
: re (r) , im (i)
{ }
int func(const complex& param)
{ return param.re + param.im; }
private:
double re, im;
};
{
complex c1(2,1);
complex c2;
c2.func(c1);
}
*/
//总结:
/*
数据一定放在private内
参数尽可能以reference来传递
返回值尽量以reference来传递
在类的body内的函数应加const就要加,否则会报错
构造函数特殊语法:initialization list(尽量多用)
*/