C++学习笔记21:派生类的构造函数

1 例子(见本工程paishengleiconstructor.cpp)

//
// 派生类构造函数
//
#include <iostream>

using namespace std;

class Bug   // 虫子类
{
private:
    int nLegs;  // 腿数目
    int nColor; // 颜色
public:
    int nType;  // 类型
    // 构造函数
    Bug(int legs, int color);
    void PrintBug()
    {
        cout << "基类PrintBug" << endl;  // 成员函数
    }
};


class FlyBug:public Bug     // 飞虫
{
    int nWings; // 翅膀数目
public:
    FlyBug(int legs, int color, int wings); // 构造函数
};

Bug::Bug(int legs, int color) {
    nLegs = legs;
    nColor = color;
    cout << "legs = " << legs << " colors = " << color << endl;
}
// 错误的FlyBug的构造函数
/**
FlyBug::FlyBug(int legs, int color, int wings) {
    nLegs = legs;   // 不能访问 因为它在基类中是私有成员
    nColor = color; // 同上
    nType = 1;  // ok
    nWings = wings;
}
*/

/**问题:派生类对象的构造函数里面怎么去初始化从基类继承得到的那些私有成员
 * 解决办法:初始化列表,直接初始化派生类对象里面所包含的那个基类的对象。
 * */
// 正确的FlyBug的构造函数
FlyBug::FlyBug(int legs, int color, int wings):Bug(legs,color){
    nWings = wings;
    cout << "Wings = " << wings << endl;
}


int main()
{
    FlyBug fb(2, 3, 4);
    fb.PrintBug();
    fb.nType = 1;
    //fb.nLegs = 2;   // error ,nLegs is private
    return 0;
}

2 在创建派生类的对象时,需要调用基类的构造函数:初始化派生类对象中从基类继承的成员。
3 在执行一个派生类的构造函数之前,总是先执行基类的构造函数。
4 调用基类构造函数的两种方式

    --显式方式:在派生类的构造函数中,为基类的构造函数提供参数。
    eg: derived::derived(arg_derived-list):base(arg_base-list)
    --隐式方式:在派生类的构造函数中,省略基类构造函数时,派生类的构造函数则自动调用基类的默认构造函数。

5 派生类的析构函数被执行时,执行完派生类的析构函数之后,自动调用基类的析构函数。
6 实例(见本工程paishengxigou.cpp)

//
// 派生类析构函数调用顺序案例
//

#include <iostream>

using namespace std;

class Base
{
public:
    int n;
    Base(int i):n(i)
    {
        cout << "Base" << n << " constructed" << endl;
    }
    ~Base()
    {
        cout << "Base" << n << " destructed" << endl;
    }

};

class Derived:public Base
{
public:
    Derived(int i):Base(i)  // 派生类的构造函数得写法  需要跟初始化列表  指定基类部分如何进行初始化
    {
        cout << "Derived constructed" << endl;
    }
    ~Derived()
    {
        cout << "Derived destructed" << endl;
    }

};

int main()
{
    Derived Obj(3);
    return 0;
}


7 包含成员对象的派生类的构造函数的写法 (见本工程fengbileipaisheng.cpp)

//
// 包含成员对象(封闭类)的派生类的构造函数写法  意思就是派生类也是封闭类的时候
//

#include <iostream>

using namespace std;

class Bug
{
private:
    int nLegs;
    int nColor;
public:
    int nType;
    Bug(int legs, int color);
    void PrintBug(){};
};

class Skill
{
public:
    Skill(int n);
};

class FlyBug:public Bug
{
    int nWings;
    Skill sk1, sk2;
public:
    FlyBug(int legs, int color, int wings);
};

// 使用初始化列表来编写一个即是派生类又是封闭类的构造函数。
FlyBug::FlyBug(int legs, int color, int wings):Bug(legs, color),sk1(5),sk2(color),nWings(wings){}

int main()
{

}


8 封闭派生类的构造函数执行顺序

    在创建派生类的对象时:
        (1)先执行基类的构造函数, 用以初始化派生类对象中从基类继承的成员;
        (2)再执行成员对象类的构造函数,用以初始化派生类对象中成员对象。
        (3)最后执行派生类自己的构造函数。
    派生类对象消亡的时候析构函数消亡的顺序:
        (1)先执行派生类自己的析构函数
        (2)再依次执行各成员对象类的析构函数
        (3)最后执行基类的析构函数
    析构函数的调用顺序与构造函数的调用顺序相反。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值