cc28c_demo.cpp,派生类的构造函数和析构函数-代码示范3

cc28c_demo.cpp,派生类的构造函数和析构函数-代码示范3

 

//派生类的构造函数和析构函数
//派生类的构造函数(执行步骤)
//--执行基类的构造函数
//--执行成员对象的构造函数
//--执行派生类的构造函数

//父类,子类
//构造函数与析构函数不能继承

//派生类的析构函数
//--对派生类新增普通成员进行清理
//--调用成员对象的析构函数
//--调用基类析构函数

没有看到析构执行过程,参考如下链接

https://blog.csdn.net/txwtech/article/details/103978929

 

#include <iostream>//txwtech,cc28c_demo.cpp,派生类的构造函数和析构函数-代码示范3
using namespace std;

enum BREED {GOLDEN,CAIRN,DANDIE,SHETLAND,DOBRMAN,LAB};

class Mammal//哺乳动物
{
public:
    Mammal();
    ~Mammal();
    //存取器成员函数
    int GetAge() const { return itsAge; }
    void SetAge(int age) { itsAge = age; }
    int GetWeight() const { return itsWeight; }
    void SetWeight(int weight) { itsWeight = weight; }

    void Speak() const { cout << "Mammal的声音!\n"; }
    void Sleep() const { cout << "shhh,我正在睡觉"; }

protected:
    int itsAge;
    int itsWeight;

};
class Dog:public Mammal
{
public:
    Dog();
    ~Dog();
    BREED GetBreed() const { return itsBreed; }
    void SetBreed(BREED breed) { itsBreed = breed; }

    void WagTail() const { cout << "Tail wagging...\n" << endl; }
    void BegForFood() const { cout << "beging for food...\n"; }


private:
    BREED itsBreed;

};
Mammal::Mammal():itsAge(3),itsWeight(5)
{
    cout << "Mammal的构造函数正在被调用..." << endl;
}
Mammal::~Mammal()
{
    cout << "Mammal的析构函数被调用。。。" << endl;
}
Dog::Dog() :itsBreed(GOLDEN)
{
    cout << "Dog的构造函数被调用" << endl;
}
Dog::~Dog()
{
    cout << "Dog的析构函数被调用" << endl;
}


int main()
{

    Dog Fido;//狗是小子
    Fido.Speak();
    Fido.WagTail();
    cout << "Fido is: " << Fido.GetAge() << "years old" << endl;
    //getchar();
    //system("pause");
    return 0;
}
posted @ 2020-01-14 21:15  txwtech  阅读(314)  评论(0编辑  收藏  举报