#include iostreamusing namespace std;class Fatherpublic Father cout Father endl; ~Father cout ~Father endl; int m_bear; int m_age;;class Motherpublic Mother cout Mother endl; ~Mother
输出结果为:
Father Mother Son ~Son ~Mother ~Father 24
解释:
- 首先创建了一个Son对象jack。
- 在创建Son对象之前,会先创建Father和Mother对象。所以会先输出Father和Mother的构造函数。
- 接着创建Son对象,输出Son的构造函数。
- 然后程序结束,会依次调用Son、Mother和Father的析构函数,所以会依次输出Son、Mother和Father的析构函数。
- 最后输出sizeof(jack),即对象jack的大小,结果为24字节。
- jack.m_bear = 10;给jack对象的m_bear成员变量赋值为10。
- jack.Mother::m_age = 20;给jack对象的从Mother继承而来的m_age成员变量赋值为20。
- 输出jack.Father::m_age,输出jack对象从Father继承而来的m_age成员变量的值。
原文地址: https://www.cveoy.top/t/topic/iL5f 著作权归作者所有。请勿转载和采集!