码迷,mamicode.com
首页 > 其他好文 > 详细

虚函数与纯虚函数的代码解读——2

时间:2015-10-25 16:23:04      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>

#include <string>

#include <assert.h>
#include <cstring>

#include <unistd.h>
#include <sys/types.h>
#include <signal.h>


using namespace std;

class Base
{
public:
    Base() {cout << "Base con" << endl;}
    virtual ~Base()
    {
        cout << "Base destructor" << endl;
    }
    virtual void Hiberarchy() const = 0;
};

void Base::Hiberarchy() const//pure virtual also can have function body
{
    cout << "Base::Hiberarchy" << endl;
}


class Derived : public Base
{
public:
    Derived() {cout << "Derived con" << endl;}
    virtual void Hiberarchy() const
    {
        Base::Hiberarchy();
        cout << "Derived::Hiberarchy" << endl;
    }

//    virtual ~Derived()
    ~Derived()
    {
        cout << "Derived destructor" << endl;
    }
    virtual void foo(){cout << "Derived foo()" << endl;}
};

int main()
{
    Base *pb = new Derived();
    pb->Hiberarchy();
    pb->Base::Hiberarchy();

    delete pb;
    Derived derivedObj;
    derivedObj.foo();
    //cout <<  << endl;
    return 0;
}

技术分享




虚函数与纯虚函数的代码解读——2

标签:

原文地址:http://www.cnblogs.com/guxuanqing/p/4908842.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!