[轉] 08章 基类与派生类的构造函数

出處:http://www.quanxue.cn/JC_CLanguage/Cpp/Cpp08.html

一、缺省构造函数的调用关系
通过下面的例子,我们来看一下基类与派生的构造函数的调用顺序。创建时先基类后派生类。销毁时先派生类后基类。

#include <iostream>
#include <string>
using namespace std;

class CBase {
    string name;
    int age;
public:
    CBase() {
        cout << "BASE" << endl;
    }
    ~CBase() {
        cout << "~BASE" << endl;
    }
};

class CDerive : public CBase {
public:
    CDerive() { 
        cout << "DERIVE" << endl;
    }
    ~CDerive() { 
        cout << "~DERIVE" << endl;
    }
};

int main ( ) 
{
    CDerive d;

    return 0;
}


二、有参数时的传递
当有参数时,参数必须传送给基类。注意例子中传递的方法(第8行、第19行)。

#include <iostream>
#include <string>
using namespace std;

class CBase {
    string name;
public:
    CBase(string s) : name(s) {
        cout << "BASE: " << name << endl;
    }
    ~CBase() {
        cout << "~BASE" << endl;
    }
};

class CDerive : public CBase {
    int age;
public:
    CDerive(string s, int a) : CBase(s), age(a) { 
        cout << "DERIVE: " << age << endl;
    }
    ~CDerive() { 
        cout << "~DERIVE" << endl;
    }
};

int main ( ) 
{
    CDerive d("小雅", 27);

    return 0;
}


三、祖孙三代的参数传递
当有三层继承时,参数要一层一层地传递下去(第30行、第19行、第8行)。

#include <iostream>
#include <string>
using namespace std;

class CBase {
    string name;
public:
    CBase(string s) : name(s) {
        cout << "BASE: " << name << endl;
    }
    ~CBase() {
        cout << "~BASE" << endl;
    }
};

class CDerive : public CBase {
    int age;
public:
    CDerive(string s, int a) : CBase(s), age(a) { 
        cout << "DERIVE: " << age << endl;
    }
    ~CDerive() { 
        cout << "~DERIVE" << endl;
    }
};

class CSon : public CDerive {
    string id;
public:
    CSon(string s1, int a, string s2) : CDerive(s1, a), id(s2) { 
        cout << "SON: " << id << endl;
    }
    ~CSon() { 
        cout << "~SON" << endl;
    }
};

int main ( ) 
{
    CSon s("小雅", 27, "8503026");

    return 0;
}


四、多重继承的参数传递
多重继承时参数的传递方法和上面一样,要注意的是两个基类的顺序。决定2个基类的顺序是知27行。将27行的CBase1和CBase2的顺序交换一下,其结果中BASE1和BASE2的顺序也随之改变,与第30行无关。

#include <iostream>
#include <string>
using namespace std;

class CBase1 {
    string name;
public:
    CBase1(string s) : name(s) {
        cout << "BASE1: " << name << endl;
    }
    ~CBase1() {
        cout << "~BASE1" << endl;
    }
};

class CBase2 {
    int age;
public:
    CBase2(int a) : age(a) {
        cout << "BASE2: " << age << endl;
    }
    ~CBase2() {
        cout << "~BASE2" << endl;
    }
};

class CDerive : public CBase1, public CBase2 {
    string id;
public:
    CDerive(string s1, int a, string s2) : CBase1(s1), CBase2(a), id(s2) { 
        cout << "DERIVE: " << id << endl;
    }
    ~CDerive() { 
        cout << "~DERIVE" << endl;
    }
};

int main ( ) 
{
    CDerive d("小雅", 27, "8503026");

    return 0;
}
C++语言教程
01章 C++与C的不同点02章 名字空间
03章 缺省参数和函数重载04章 引用
05章 类06章 成员函数和运算符的重载
07章 继承08章 基类与派生类的构造函数
09章 派生类中调用基类成员10章 多重继承的问题
11章 虚函数和纯虚函数12章 多态的应用
13章 实例指针(this)14章 友元函数(friend)
15章 构造函数、explicit、类合成16章 new和delete的使用
17章 静态类成员18章 const和mutable的使用
19章 const修饰指针20章 inline和汇编的使用
未經允許不得轉載:GoMCU » [轉] 08章 基类与派生类的构造函数