- // test.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- class Base1
- {
- public:
- virtual void fun1()=0;
- int * m_pCount;
- Base1()
- {
- cout<<"Base1()"<<endl;
- m_pCount=new int();
- }
- ~Base1()
- {
- cout<<"~Base1()"<<endl;
- delete m_pCount;
- }
- };
- class Base2
- {
- public:
- virtual void fun1()=0;
- int * m_pCount;
- Base2()
- {
- cout<<"Base2()"<<endl;
- m_pCount=new int();
- }
- virtual ~Base2()
- {
- cout<<"~Base2()"<<endl;
- delete m_pCount;
- }
- };
- class subClass1:public Base1
- {
- public:
- virtual void fun1()
- {
- cout<<"hello,subClass1"<<endl;
- }
- subClass1()
- {
- cout<<"subClass1()"<<endl;
- }
- ~subClass1()
- {
- cout<<"~subClass1()"<<endl;
- }
- };
- class subClass2:public Base2
- {
- public:
- virtual void fun1()
- {
- cout<<"hello,subClass2"<<endl;
- }
- subClass2()
- {
- cout<<"subClass2()"<<endl;
- }
- ~subClass2()
- {
- cout<<"~subClass2()"<<endl;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- Base1 *pBase1=new subClass1();
- delete pBase1;
- cout<<"-----------------"<<endl;
- subClass1 *pClass1=new subClass1();
- delete pClass1;
- cout<<"-----------------"<<endl;
- Base2 *pBase2=new subClass2();
- delete pBase2;
- cout<<"-----------------"<<endl;
- subClass2 *pClass2=new subClass2();
- delete pClass2;
- int i;
- cin>>i;
- return 0;
- }
- 输出:
- Base1()
- subClass1()
- ~Base1()
- -----------------
- Base1()
- subClass1()
- ~subClass1()
- ~Base1()
- -----------------
- Base2()
- subClass2()
- ~subClass2()
- ~Base2()
- -----------------
- Base2()
- subClass2()
- ~subClass2()
- ~Base2()
C++ Construtor Deconstrutor virtual
最新推荐文章于 2022-10-22 23:06:56 发布