Object Oriented Programming
- 概念:Object Based (基于对象) vs. Object Oriented (面向对象)
- 头文件防止重复声明
- 头文件的布局
- class的声明
- Class Template(模板)
- inline(内联)函数
- constructor(构造函数)
- 参数传递::pass by value vs. pass by reference (to const)
- 返回值传递:return by value vs. return by reference (to const)
- friend(友元)
- 相同 class 的各个 objects 互为 friends (友元)
- operator overloading (操作符重载-1, 成员函数) this
- return by reference 语法分析
- operator overloading (操作符重载), 非成员函数
- Big Three, 三个特殊函数
- class with pointer members 必須有 copy ctor 和 copy op=
- 函数模板(类似类模板)
- C++三种关系Inheritance(继承)/Composition(复合)/Delegation(委托)
- End
概念:Object Based (基于对象) vs. Object Oriented (面向对象)
-
Object Based : 面对的是单一 class 的设计。
-
Object Oriented : 面对的是多重 classes 的设计,
classes 和 classes 之间的关系。
头文件防止重复声明
例如<complex.h>
#ifndef __HeaderName_ // __COMPLEX__方便其他人读懂说明
#define __HeaderName__ // __COMPLEX__
...
内容
...
#endi
引用complex的test.h
#include <iostream>
#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 <cmath>
class ostream; //forward declarations(前置声明)
class complex;
complex&
__doapl (complex* ths, const complex& r);
class complex //class declarations(类-声明)
{
...
};
complex::function ... //class definition(类-定义)
#endif
class的声明
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;
...
}
Class Template(模板)
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);
...
}
inline(内联)函数
适用函数内容比较的少的函数,在编译过程中内容直接被替换成函数内的内容。
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 ();
}
constructor(构造函数)
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(重载)。
函数重载往往发生在构造函数中。
参数传递::pass by value vs. pass by reference (to const)
规范:
数据放在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
返回值传递:return by value vs. return by reference (to const)
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
};
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: //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 的各个 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); //c2和c1或为friend
}
operator overloading (操作符重载-1, 成员函数) this
注意:
成员函数默认有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
}
说明:
return by reference 语法分析
注意:
传递者无需知道接收者是以reference形式接收。
operator overloading (操作符重载), 非成员函数
Big Three, 三个特殊函数
构造函数和析构函数;
拷贝构造函数;
拷贝赋值函数(一定要检查判断是否自我赋值);
构造函数(structor)和析构函数(Destructor)
class with pointer members 必須有 copy ctor 和 copy op=
浅拷贝和深拷贝:
浅拷贝:只拷贝指针过去(容易memory leak)。
深拷贝:复制一份新的。
函数模板(类似类模板)
C++三种关系Inheritance(继承)/Composition(复合)/Delegation(委托)
Composition(复合)
两图理解,复合概念
Delegation (委托). Composition by reference
Inheritance (继承), 表示 is-a
Inheritance (继承) with virtual functions (虛函数)
End
需要图内容的欢迎私聊!!