当前位置:编程学习 > C/C++ >>

C++子类析构问题

 [cpp]
创建基类class base 

创建基类class base[cpp]
#ifndef __base_h  
#define __base_h  
class base 

public: 
    base(int a,int b); 
    ~base(); 
public: 
    int m; 
    int n; 
}; 
#endif 

#ifndef __base_h
#define __base_h
class base
{
public:
 base(int a,int b);
 ~base();
public:
 int m;
 int n;
};
#endif[cpp] 
**************base.cpp*************** 

**************base.cpp***************[cpp] view plaincopyprint?
#include "stdafx.h"  
#include "base.h"  
 
base::base(int a, int b) 

    m=a; 
    n=b; 
    printf("gou zao ji lei\n"); 

 
base::~base() 

    printf("xi gou ji lei\n"); 

#include "stdafx.h"
#include "base.h"

base::base(int a, int b)
{
 m=a;
 n=b;
 printf("gou zao ji lei\n");
}

base::~base()
{
 printf("xi gou ji lei\n");
}
 

**************frombase.h************


创建子类frombase继承基类class base

[cpp] 
#ifndef __use_h  
#define __use_h  
 
#include "base.h"  
class frombase:public base 

public: 
 frombase(int q,int w,int e,int r); 
 ~frombase(); 
public: 
 int x; 
 int y; 
}; 
#endif 

#ifndef __use_h
#define __use_h

#include "base.h"
class frombase:public base
{
public:
 frombase(int q,int w,int e,int r);
 ~frombase();
public:
 int x;
 int y;
};
#endif

************frombase.cpp************

[cpp] 
#include "stdafx.h"  
#include "use.h"  
 
frombase::frombase(int q,int w,int e,int r):base(e,r) 

    x=q; 
    y=w; 

frombase::~frombase() 

    printf("xi gou frombase\n"); 

#include "stdafx.h"
#include "use.h"

frombase::frombase(int q,int w,int e,int r):base(e,r)
{
 x=q;
 y=w;
}
frombase::~frombase()
{
 printf("xi gou frombase\n");
}
C++ 控制台程序测试 11

[cpp] 
#include "stdafx.h"  
#include "base.h"  
#include "use.h"  
 
//int _tmain(int argc, _TCHAR* argv[])  
int main() 

 int aa =1; 
 int bb =2; 
 int cc = 3; 
 int dd = 4; 
 base* _base; 
 frombase * _frombase; 
 _base = (base*)new frombase(aa,bb,cc,dd); 
 printf("%d %d\n",_base->m,_base->n); 
 delete _base; 
 getchar(); 
 return 0; 

 
执行结果:: 

#include "stdafx.h"
#include "base.h"
#include "use.h"

//int _tmain(int argc, _TCHAR* argv[])
int main()
{
 int aa =1;
 int bb =2;
 int cc = 3;
 int dd = 4;
 base* _base;
 frombase * _frombase;
 _base = (base*)new frombase(aa,bb,cc,dd);
 printf("%d %d\n",_base->m,_base->n);
 delete _base;
 getchar();
 return 0;
}

执行结果::构造基类base

构造子类frombase

3 4

析构基类 base


 C++ 控制台程序测试 22

[cpp] 
#include "stdafx.h"  
#include "base.h"  
#include "use.h"  
int main() 

    int aa =1; 
    int bb =2; 
    int cc = 3; 
    int dd = 4; 
    base* _base; 
    frombase * _frombase; 
    _base = (base*)new frombase(aa,bb,cc,dd); 
    printf("%d %d\n",_base->m,_base->n); 
    _frombase  = (frombase*)_base; 
    printf("%d %d %d %d\n",_frombase->m,_frombase->n,_frombase->x,_frombase->y); 
    delete _frombase; 
    getchar(); 
 
    return 0; 

执行结果::: 

#include "stdafx.h"
#include "base.h"
#include "use.h"
int main()
{
 int aa =1;
 int bb =2;
 int cc = 3;
 int dd = 4;
 base* _base;
 frombase * _frombase;
 _base = (base*)new frombase(aa,bb,cc,dd);
 printf("%d %d\n",_base->m,_base->n);
 _frombase  = (frombase*)_base;
 printf("%d %d %d %d\n",_frombase->m,_frombase->n,_frombase->x,_frombase->y);
 delete _frombase;
 getchar();

 return 0;
}
执行结果:::构造基类 base

构造子类 frombase

3 4

3 4 1 2

析构子类 frombase
析构基类 base

 

总结:当子类的对象直接释放时:

            先调用子类自身的析构函数 再调用基类的析构函数

        当子类的对象被强制转换为基类类型时:

            直接调用基类的析构函数,忽略掉子类的析构函数

 

 

 

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,