继承with virtual functions
- 非虚函数:不希望子类重新定义(override)它;
- 虚函数:希望子类可以重新定义它,并且你已经有一个默认的定义了—子类可以自己重新定义,也可以不定义
- 纯虚函数:希望子类 一定 要重新定义它,对它没有默认定义
**纯虚函数draw() :**你们继承之后,必须重新自己定义一个draw(),因为我不知道该怎么画,你们自己画自己搞。
设计模式Template Method
MFC
可以在多年之后,框架依旧可以用,很健壮性啊
上图注意this指针的意义,this->Serialize()就跑到Application 类CMYDoc中了 这里的this就是myDoc
继承与复合关系下的构造和析构
对于第一种关系:
先执行父类构造函数,再执行复合类的构造函数,最后执行本类的构造函数。而析构次序反之
代码如下:
#include <iostream>
using namespace std;
class Base
{
public:
Base()
{
cout << "ctor of base" << endl;
}
~Base()
{
cout << "dtor of base" << endl;
}
};
class Component
{
public:
Component()
{
cout << "ctor of component" << endl;
}
~Component()
{
cout << "dtor of component" << endl;
}
};
class Derived : public Base
{
public:
Derived()
{
cout << "ctor of derived" << endl;
}
~Derived()
{
cout << "dtor of derived" << endl;
}
protected:
Component c;
private:
};
void objectplay(void)//通过调用这个函数观察对象的析构与构造函数
{
Derived d;
}
int main()
{
objectplay();
system("pause");
return 0;
}
结果:
对于第二种关系:
先执行Component的构造函数,再执行Base的构造函数,最后执行Derived的构造函数,最里面的函数先被调用
代码如下:
#include <iostream>
using namespace std;
class Component//组件
{
public:
Component()
{
cout << "ctor of component" << endl;
}
~Component()
{
cout << "dtor of component" << endl;
}
};
class Base//父类
{
public:
Base()
{
cout << "ctor of base" << endl;
}
~Base()
{
cout << "dtor of base" << endl;
}
protected:
Component c;
};
class Derived : public Base//子类
{
public:
Derived()
{
cout << "ctor of derived" << endl;
}
~Derived()
{
cout << "dtor of derived" << endl;
}
private:
};
void objectplay(void)//通过调用这个函数观察对象的析构与构造函数
{
Derived d;
}
int main()
{
objectplay();
system("pause");
return 0;
}
结果:
代码参考
设计一个窗口观察多个内容的功能,利用delegation:
重要语句:vector <Observer*>m_view;