C++作业(第七章)7-10

题目: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;
}

猜你喜欢

转载自blog.csdn.net/xiaorui98/article/details/81163912