#include <vector>
#include <iostream>
using namespace std;
class A
{
public:
A(){cout << "A is Construct" << endl;}
~A(){cout << "A is Destruct" << endl;}
};
class B
{
public:
B(){cout << "B is Construct" << endl;}
~B(){cout << "B is Destruct" << endl;}
};
class C : public A
{
public:
C(){cout << "C is Construct" << endl;}
~C(){cout << "C is Destruct" << endl;}
B member;
};
int main()
{
C* c = new C;
delete c;
return 0;
}
輸出:
A is Construct
B is Construct
C is Construct
C is Destruct
B is Destruct
A is Destruct
int main()
{
A* c = new C;
delete c;
return 0;
}
輸出:
A is Construct
B is Construct
C is Construct
A is Destruct
這說明了轉型的危險性。
公告版位
星落的瞬間!放棄的後悔是永遠!
- Nov 24 Tue 2009 20:11
C++繼承簡明範例
全站熱搜
留言列表