CSDN博客

img DanceFire

理解继承

发表于2004/10/16 20:31:00  2133人阅读

分类: C++

#include <iostream>

using namespace std;

int inc = 0;
string tab()
{
    string t;
    t.reserve(inc+1);
    for(int i = 0 ; i < inc ; ++i)
    {
        t.push_back('/t');
    }
    return t;        
}    

class base{
public:
    base(void){
        inc++;
        cout<<tab()<<"Base::ctor()"<<endl;
        cout<<tab()<<"I am a "<<typeid(this).name()<<endl;
        vfunc();
        func();
        inc--;
    }
    ~base(void){
        inc++;
        cout<<tab()<<"Base::~ctor()"<<endl;
        inc--;
    }
    
    base(double d)
    {
        inc++;
        cout<<tab()<<"Base::ctor( "<<d<<" )"<<endl;
        cout<<tab()<<"I am a "<<typeid(this).name()<<endl;
        vfunc();
        func();
        inc--;
    }    

    base(int a){
        inc++;
        cout<<tab()<<"Base::ctor( "<<a<<" )"<<endl;
        cout<<tab()<<"I am a "<<typeid(this).name()<<endl;
        vfunc();
        func();
        inc--;
    }        
    virtual void vfunc(void){
        inc++;
        cout<<tab()<<"Base::Virtual function()"<<endl;
        inc--;
    }
    void func(void){
        inc++;
        cout<<tab()<<"Base::function()"<<endl;
        inc--;
    }
    void call(void){
        inc++;
        cout<<tab()<<"Base::call()"<<endl;
        vfunc();
        func();
        inc--;
    }
};

class child : public base{
public:
    child(void){
        inc++;
        cout<<tab()<<"Child::ctor()"<<endl;
        cout<<tab()<<"I am a "<<typeid(this).name()<<endl;
        vfunc();
        func();
        inc--;
    }
    ~child(void){
        inc++;
        cout<<tab()<<"Child::~ctor()"<<endl;
        inc--;
    }
    child(double d)
    {
        inc++;
        cout<<tab()<<"Child::ctor( "<<d<<" )"<<endl;
        cout<<tab()<<"I am a "<<typeid(this).name()<<endl;
        vfunc();
        func();
        inc--;
    }    
   	child(int a) : base( a )
    {
        inc++;
   	    cout<<tab()<<"Child::ctor( "<<a<<" )"<<endl;
        cout<<tab()<<"I am a "<<typeid(this).name()<<endl;
        vfunc();
        func();
        inc--;
   	}    
    virtual void vfunc(void){
        inc++;
        cout<<tab()<<"Child::Virtual function()"<<endl;
        inc--;
    }
    void func(void){
        inc++;
        cout<<tab()<<"Child::function()"<<endl;
        inc--;
    }
};

int main(void)
{
    cout<<tab()<<"Before create b"<<endl;
    base b;
    cout<<tab()<<"After create b"<<endl;
    b.vfunc();
    b.func();
    b.call();
    cout<<"-----------------"<<endl;
    cout<<tab()<<"Before create c"<<endl;
    child c;
    cout<<tab()<<"After create c"<<endl;
    c.vfunc();
    c.func();
    c.call();
    cout<<"-----------------"<<endl;
    cout<<tab()<<"Before create c1"<<endl;
    child c1(1);
    cout<<tab()<<"After create c1"<<endl;
    c1.vfunc(); 
    c1.func();
    c1.call();
    cout<<"-----------------"<<endl;
    cout<<tab()<<"Before create c2"<<endl;
    child c2(1.1);
    cout<<tab()<<"After create c2"<<endl;
    c2.call();
}    
运行结果:







Before create b
        Base::ctor()
        I am a P4base
                Base::Virtual function()
                Base::function()
After create b
        Base::Virtual function()
        Base::function()
        Base::call()
                Base::Virtual function()
                Base::function()
-----------------
Before create c
        Base::ctor()
        I am a P4base
                Base::Virtual function()
                Base::function()
        Child::ctor()
        I am a P5child
                Child::Virtual function()
                Child::function()
After create c
        Child::Virtual function()
        Child::function()
        Base::call()
                Child::Virtual function()
                Base::function()
-----------------
Before create c1
        Base::ctor( 1 )
        I am a P4base
                Base::Virtual function()
                Base::function()
        Child::ctor( 1 )
        I am a P5child
                Child::Virtual function()
                Child::function()
After create c1
        Child::Virtual function()
        Child::function()
        Base::call()
                Child::Virtual function()
                Base::function()
-----------------
Before create c2
        Base::ctor()
        I am a P4base
                Base::Virtual function()
                Base::function()
        Child::ctor( 1.1 )
        I am a P5child
                Child::Virtual function()
                Child::function()
After create c2
        Base::call()
                Child::Virtual function()
                Base::function()
        Child::~ctor()
        Base::~ctor()
        Child::~ctor()
        Base::~ctor()
        Child::~ctor()
        Base::~ctor()
        Base::~ctor()
阅读全文
0 0

相关文章推荐

img
取 消
img