要注意培养正规的、大气的编程习惯。
1、基于对象(Object Based):面向单一class的设计。
class有两种经典的分类:
一种不带有指针
另一种带有指针
2、面向对象(Object Oriented):面对的是多重classes的设计,classes和classes之间的关系。
-继承
-复合
-委托
3、推荐书籍
本文所谈的对象是不带有指针的类(Class without pointer);
具体的分析了类的组成,重点介绍类方法的设计准则;
主要是内联函数,参数传递,返回值传递,友元函数以及操作符重载在类设计中的使用。
下面的示例程序来自于--侯捷老师的网络课程《C++面向对象高级开发》。
通过程序及注释详细的介绍Class without pointer的设计。
complex.h
//写在前面
//1.关于内联函数:
// 如果函数在body内定义,这样便成为了inline的候选人。
// 如果在body之外定义,默认不是inline函数。需要在函数的前面加inline关键字。
// 在声明时写不写inline没有影响,建议不写。
// 是否真的成为inline函数,最终由编译器决定。
//2.不带指针的类多半不用自己写析构函数。
//3.参数传递
// 值传递:尽量不要使用值传递,不然会出现大量的拷贝。
// 引用传递:相当于传指针,速度很快。
// const引用,保证传进去的值不会被修改。
//4.返回值传递
// 尽量传递引用,局部变量不能作为引用返回。
//5.friend友元函数
// 可以直接读取对象内部的私有成员;
// 相同class的各个objects互为friends;
// 友元会破坏类的封装性,不宜多用。
//防卫式声名头文件
#ifndef __MYCOMPLEX__
#define __MYCOMPLEX__
class complex;
complex&
__doapl(complex* ths, const complex& r);
complex&
__doami(complex* ths, const complex& r);
complex&
__doaml(complex* ths, const complex& r);
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&);
//常量成员函数,不改变对象内的数据。
double real() const { return re; }
double imag() const { return im; }
//数据一定放在private里面
private:
double re, im;
friend complex& __doapl(complex *, const complex&);
friend complex& __doami(complex *, const complex&);
friend complex& __doaml(complex *, const complex&);
};
inline complex&
__doapl(complex* ths, const complex& r)
{
ths->re += r.re;
ths->im += r.im;
return *ths;
}
inline complex&
complex::operator += (const complex& r)
{
return __doapl(this, r);
}
inline complex&
__doami(complex* ths, const complex& r)
{
ths->re -= r.re;
ths->im -= r.im;
return *ths;
}
inline complex&
complex::operator -= (const complex& r)
{
return __doami(this, r);
}
inline complex&
__doaml(complex* ths, const complex& r)
{
double f = ths->re * r.re - ths->im * r.im;
ths->im = ths->re * r.im + ths->im * r.re;
ths->re = f;
return *ths;
}
inline complex&
complex::operator *= (const complex& r)
{
return __doaml(this, r);
}
inline double
imag(const complex& x)
{
return x.imag();
}
inline double
real(const complex& x)
{
return x.real();
}
inline complex
operator + (const complex& x, const complex& y)
{
return complex(real(x) + real(y), imag(x) + imag(y));
}
inline complex
operator + (const complex& x, double y)
{
return complex(real(x) + y, imag(x));
}
inline complex
operator + (double x, const complex& y)
{
return complex(x + real(y), imag(y));
}
inline complex
operator - (const complex& x, const complex& y)
{
return complex(real(x) - real(y), imag(x) - imag(y));
}
inline complex
operator - (const complex& x, double y)
{
return complex(real(x) - y, imag(x));
}
inline complex
operator - (double x, const complex& y)
{
return complex(x - real(y), -imag(y));
}
inline complex
operator * (const complex& x, const complex& y)
{
return complex(real(x) * real(y) - imag(x) * imag(y),
real(x) * imag(y) + imag(x) * real(y));
}
inline complex
operator * (const complex& x, double y)
{
return complex(real(x) * y, imag(x) * y);
}
inline complex
operator * (double x, const complex& y)
{
return complex(x * real(y), x * imag(y));
}
complex
operator / (const complex& x, double y)
{
return complex(real(x) / y, imag(x) / y);
}
inline complex
operator + (const complex& x)
{
return x;
}
inline complex
operator - (const complex& x)
{
return complex(-real(x), -imag(x));
}
inline bool
operator == (const complex& x, const complex& y)
{
return real(x) == real(y) && imag(x) == imag(y);
}
inline bool
operator == (const complex& x, double y)
{
return real(x) == y && imag(x) == 0;
}
inline bool
operator == (double x, const complex& y)
{
return x == real(y) && imag(y) == 0;
}
inline bool
operator != (const complex& x, const complex& y)
{
return real(x) != real(y) || imag(x) != imag(y);
}
inline bool
operator != (const complex& x, double y)
{
return real(x) != y || imag(x) != 0;
}
inline bool
operator != (double x, const complex& y)
{
return x != real(y) || imag(y) != 0;
}
#include <cmath>
inline complex
polar(double r, double t)
{
return complex(r * cos(t), r * sin(t));
}
inline complex
conj(const complex& x)
{
return complex(real(x), -imag(x));
}
inline double
norm(const complex& x)
{
return real(x) * real(x) + imag(x) * imag(x);
}
#include <iostream>
using namespace std;
ostream&
operator << (ostream& os, const complex& x)
{
return os << '(' << real(x) << ',' << imag(x) << ')';
}
#endif //__MYCOMPLEX__
complex-test.cpp
#include "complex.h"
int main()
{
complex c1(2, 1);
complex c2(4, 0);
cout << c1 << endl;
cout << c2 << endl;
cout << c1 + c2 << endl;
cout << c1 - c2 << endl;
cout << c1*c2 << endl;
cout << c1 / 2 << endl;
cout << conj(c1) << endl;
cout << norm(c1) << endl;
cout << polar(10, 4) << endl;
cout << (c1 += c2) << endl;
cout << (c1 == c2) << endl;
cout << (c1 != c2) << endl;
cout << +c2 << endl;
cout << -c2 << endl;
cout << (c2 - 2) << endl;
cout << (5 + c2) << endl;
return 0;
}