设为首页 加入收藏

TOP

C++子类析构问题
2014-11-23 20:25:35 来源: 作者: 【 】 浏览:9
Tags:子类 问题

[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

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

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

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

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

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hdu1166 树状数组 下一篇POJ1679The Unique MST (最小生..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·我的Linux内核学习笔 (2025-12-26 22:21:10)
·如何评价腾讯开源的 (2025-12-26 22:21:07)
·为什么TCP网络编程中 (2025-12-26 22:21:04)
·Python 数据分析与可 (2025-12-26 21:51:20)
·从零开始学Python之 (2025-12-26 21:51:17)