Object Based : 面对的是单一 class 的设计。
Object Oriented : 面对的是多重 classes 的设计,
classes 和 classes 之间的关系。
例如
#ifndef __HeaderName_ // __COMPLEX__方便其他人读懂说明
#define __HeaderName__ // __COMPLEX__
...
内容
...
#endi
引用complex的test.h
#include
#include "complex.h“ //引用上面头文件
using namespace std;
int main()
{
complex c1(2,1);
complex c2;
cout << c1 << endl;
cout << c2 << endl;
c2 = c1 + 5;
c2 = 7 + c1;
c2 = c1 + c2;
c2 += c1;
c2 += 3;
c2 = -c1;
cout << (c1 == c2) << endl;
cout << (c1 != c2) << endl;
cout << conj(c1) << endl;
return 0;
}
#ifndef __COMPLEX__
#define __COMPLEX__
#include
class ostream; //forward declarations(前置声明)
class complex;
complex&
__doapl (complex* ths, const complex& r);
class complex //class declarations(类-声明)
{
...
};
complex::function ... //class definition(类-定义)
#endif
class complex //class head
{
//class body
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&); //友元
};
使用方式
{
complex c1(2,1);
complex c2;
...
}
template<typename T> //模板语法格式
class complex
{
public:
complex (T r = 0, T i = 0)
: re (r), im (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<double> c1(2.5,1.5);
complex<int> c2(2,6);
...
}
适用函数内容比较的少的函数,在编译过程中内容直接被替换成函数内的内容。
class complex
{
public:
complex (double r = 0, double i = 0)
: re (r), im (i)
{
} //1
complex& operator += (const complex&);
double real () const {
return re; } //2
double imag () const {
return im; } //3
private:
double re, im;
friend complex& __doapl (complex*, const complex&);
};
注意:
1,2,3处函数若在 class body 內定义完成,便自动成为 inline 候选人。
否则,加上inline。
inline double
imag(const complex& x)
{
return x.imag ();
}
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&);
};
使用
{
complex c1(2,1);
complex c2;
complex* p = new complex(4); //new创建
}
注意:
构造函数可以有多个—overloading(重载)。
函数重载往往发生在构造函数中。
规范:
数据放在private中。
传参用引用传。
返回值用引用返回。
class complex
{
public:
complex (double r = 0, double i = 0) //value
: re (r), im (i)
{
}
complex& operator += (const complex&); //reference
double real () const {
return re; }
double imag () const {
return im; }
private:
double re, im;
friend complex& __doapl (complex*, const complex&); //reference
class complex
{
public:
complex (double r = 0, double i = 0)
: re (r), im (i)
{
}
complex& operator += (const complex&); //reference
double real () const {
return re; } //value
double imag () const {
return im; } //value
private:
double re, im;
friend complex& __doapl (complex*, const complex&); //reference
};
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: //friend 在private中
double re, im;
friend complex& __doapl (complex*, const complex&);
};
我们是好朋友,没有private之分,你随便用吧。
inline complex&
__doapl (complex* ths, const complex& r) {
ths->re += r.re; //自由取得 friend 的private 成员
ths->im += r.im;
return *ths;
}
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); //c2和c1或为friend
}
注意:
成员函数默认有this指针,非成员函数则无。
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); //this
}
构造函数和析构函数;
拷贝构造函数;
拷贝赋值函数(一定要检查判断是否自我赋值);
浅拷贝和深拷贝:
浅拷贝:只拷贝指针过去(容易memory leak)。
深拷贝:复制一份新的。
需要图内容的欢迎私聊!!