C++语法拾遗

本文由 @lonelyrains 出品,转载请注明出处。 
文章链接: http://blog.csdn.net/lonelyrains/article/details/46120095


1、构造函数的基类参数

假设继承关系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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值