头文件编写时的注意事项:
1. Header(头文件)的布局
#ifndef __xxxx__
#define __xxxx__
// 前置声明
// 类声明
// 类定义
#endif
2. 同一功能在不同情况下的实现方式
通过函数重载,主要体现在构造函数和各种运算符重载上。
3. 数据常被放在private区
4. 常量成员函数需要加const限定符
例:
double real() const { return re; }
5. 尽可能pass by reference
6. 尽可能return by reference
7. 友元函数效率较调用函数高
8. 相同class的各个object互为友元
例:
class complex
{
public:
complex(double r = 0, double i = 0)
:re(r), im(i)
{}
double func(const complex& c)
{
return c.re + c.im;
}
private:
double re, im;
};
complex c1(2, 1);
complex c2;
c2.func(c1);
9. 操作符重载尽量定义在类外
防止使用受限
10 参数列表中不变的参数应加const
附录:
以下是在第一周的学习中写出的complex.h头文件
#ifndef __MYCOMPLEX__
#define __MYCOMPLEX__
#include <iostream>
class ostream;
class complex;
inline double real(const complex&);
inline double imag(const complex&);
inline complex& __doapl(complex*, const complex&);
inline complex& __doami(complex*, const complex&);
inline complex& __doaml(complex*, const complex&);
class complex
{
public:
complex(double r = 0, double i = 0)
: re(r), im(i)
{}
complex& operator +=(const complex&);
complex& operator -=(const complex&);
complex& operator *=(const complex&);
complex& operator /=(const complex&);
double real() const { return re; }
double imag() const { return im; }
private:
double re, im;
friend complex& __doapl(complex*, const complex&);
friend complex& __doami(complex*, const complex&);
friend complex& __doaml(complex*, const complex&);
friend double real(const complex&);
friend double imag(const complex&);
};
inline double real(const complex& c){ return c.re; }
inline double imag(const complex& c){ return c.im; }
inline complex& __doapl(complex* ths, const complex& r)
{
ths->re += r.re;
ths->im += r.im;
return *ths;
}
inline complex& __doami(complex* ths, const complex& r)
{
ths->re -= r.re;
ths->im -= r.im;
return *ths;
}
inline complex& __doaml(complex* ths, const complex& r)
{
ths->re = ths->re * r.re - ths->im * r.im;
ths->im = ths->re * r.im + ths->im * r.re;
return *ths;
}
inline complex& complex::operator +=(const complex& r)
{
return __doapl(this, r);
}
inline complex operator +(const complex& l, const complex& r)
{
return complex(real(l) + real(r), imag(l) + imag(r));
}
inline complex operator +(const complex& l, const double& r)
{
return complex(real(l) + r, imag(l));
}
inline complex operator +(const double& l, const complex& r)
{
return complex(l + real(r), imag(r));
}
inline complex operator -(const complex& l, const complex& r)
{
return complex(real(l) - real(r), imag(l) - imag(r));
}
inline complex operator -(const complex& l, const double& r)
{
return complex(real(l) - r, imag(l));
}
inline complex operator -(const double& l, const complex& r)
{
return complex(l - real(r), -imag(r));
}
inline complex operator *(const complex& l, const complex& r)
{
return complex(real(l) * real(r) - imag(l) * imag(r), real(l) * imag(r) + imag(l) * real(r));
}
inline complex operator *(const complex& l, const double& r)
{
return complex(real(l) * r, imag(l) * r);
}
inline complex operator *(const double& l, const complex& r)
{
return complex(l*real(r), l*imag(r));
}
inline complex operator /(const complex& l, const double& r)
{
return complex(real(l) / r, imag(l) / r);
}
inline complex operator +(const complex& c)
{
return c;
}
inline complex operator -(const complex& c)
{
return complex(-real(c), -imag(c));
}
inline std::ostream& operator <<(std::ostream& os, const complex& c)
{
return os << '('<<real(c) << ',' << imag(c) << ')';
}
#endif