頭文件編寫時(shí)的注意事項(xiàng):
1. Header(頭文件)的布局
#ifndef __xxxx__
#define __xxxx__
// 前置聲明
// 類聲明
// 類定義
#endif
2. 同一功能在不同情況下的實(shí)現(xiàn)方式
通過函數(shù)重載,主要體現(xiàn)在構(gòu)造函數(shù)和各種運(yùn)算符重載上。
3. 數(shù)據(jù)常被放在private區(qū)
4. 常量成員函數(shù)需要加const限定符
例:
double real() const { return re; }
5. 盡可能pass by reference
6. 盡可能return by reference
7. 友元函數(shù)效率較調(diào)用函數(shù)高
8. 相同class的各個(gè)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 參數(shù)列表中不變的參數(shù)應(yīng)加const
附錄:
以下是在第一周的學(xué)習(xí)中寫出的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