题目:7-10 定义object类,有weight属性及相应的操作函数,由此派生出box类,增加Height和width属性及相应的操作函数,声明一个box对象,观察构造函数与析构函数的调用顺序。
#include <iostream>
using namespace std;
class object
{
public:
object()
{
cout << "构造object对象" << endl;
Weight = 0;
}
int GetWeight(){ return Weight;}
void SetWeight(int n){ Weight = n;}
~object() { cout << "析构object对象" << endl;}
private:
int Weight;
};
class box : public object
{
public:
box()
{
cout << "构造box对象" << endl;
Height = Width = 0;
}
int GetHeight(){ return Height;}
void SetHeight(int n){ Height = n;}
int GetWidth(){ return Width;}
void SetWidth(int n){ Width = n;}
~box() { cout << "析构box对象" << endl;}
private:
int Height,Width;
};
int main()
{
box a;
return 0;
}

本文通过一个C++示例程序演示了基类与派生类中构造函数和析构函数的调用顺序。具体实现了一个object基类,并从其派生出box类,展示了不同情况下构造与析构的过程。
2731

被折叠的 条评论
为什么被折叠?



