本文由 @lonelyrains 出品,转载请注明出处。
文章链接: http://blog.csdn.net/lonelyrains/article/details/46120095
假设继承关系CDialog->CBaseDialog->CDeriveDialog
CDialog: :CDialog(UINT nIDTemplate, CWnd* pParentWnd = NULL)
CBaseDialog::CBaseDialog(UINT nIDTemplate, CWnd* pParentWnd = NULL)
:CDialog(nIDTemplate, CWnd* pParentWnd)
CDeriveDialog ::CDeriveDialog(UINT nIDTemplate, CWnd* pParentWnd = NULL)
:CBaseDialog(nIDTemplate, CWnd* pParentWnd)
[注:当已有的构造函数参数不完整时,不能使用如下连续构造的形式(假设CBaseDialog的构造没有nIDTemplate参数),而是应该重定义CBaseDialog的构造函数:
CDeriveDialog ::CDeriveDialog(UINT nIDTemplate, CWnd* pParentWnd = NULL)
:CBaseDialog(CWnd* pParentWnd):CDialog(nIDTemplate, pParentWnd)
]
2、虚函数声明
虚函数在父类中声明成为了虚函数,子类中无需再声明一次,也具有虚函数属性
3、子类虚函数中调用父类的实现
子类虚函数中调用父类的实现直接使用Parent::func方式调用
2、3的示例代码如下,其中print1函数用来说明第2点,print函数用来说明第3点
class Parent
{
public :
virtual void print()
{
printf("Parent\r\n");
}
virtual void print1()
{
printf("Parent\r\n");
}
};
class Son : public Parent
{
public :
virtual void print()
{
printf("Parent\r\n");
Parent::print();
}
void print1()
{
printf("Parent\r\n");
}
};
void main()
{
Son son;
son.print();
Parent *person = new Son;
person->print1();
delete person;
}