派生类的构造函数及其对象的初始化

◆由于构造函数不能被继承,因此,派生类的构造函数中除了对派生类中数据成员进行初始化外,还必须通过调用直接基类的构造函数来对基类中数据成员初 始化,一般地将,对派生类中数据成员初始化放在该派生类构造函数的函数体内,而调用基类构造函数的基类中数据成员初始化放在该构造函数的成员初始化表中。 派生类构造函数的格式如下表示:
<派生类构造函数名>(<参数表>) : <成员初始化表>
{
  <派生类构造函数的函数体>
}
其中,<派生类构造函数名>同该派生类的类名。<成员初始化表>中包含如下的初始化项:
①基类的构造函数,用来给基类中数据成员初始化;
②子对象的类的构造函数,用来给派生类中子对象的数据成员初始化;
③派生类中常成员的初始化。
<派生类构造函数的函数体>用来给派生类中的数据成员初始化。
派生类构造函数的调用顺序如下:
①基类构造函数;
②子对象的构造函数;
③成员初始化表中其他初始化项;
④派生类构造函数的函数体。
在基类中有默认构造函数时,派生类的构造函数中可隐含调用基类中的默认构造函数。

派生类中析构函数
由于析构函数也不能继承,因此派生类的析构函数中将调用直接基类的析构函数。执行派生类析构函数的顺序正好与指向派生类的构造函数的顺序相反。先调用派生类的析构函数,再调用子对象类的析构函数,最后调用直接基类的析构函数。

例如:分析下列程序的输出结果,掌握派生类构造函数的定义格式和执行顺序,以及派生类析构函数的调用方法。

 1 None.gif#include<iostream>
 2 None.gifusing namespace std;
 3 None.gifclass A
 4 ExpandedBlockStart.gif ContractedBlock.gif{
 5 InBlock.gifpublic:
 6 InBlock.gif    A()
 7 ExpandedSubBlockStart.gif ContractedSubBlock.gif    {
 8 InBlock.gif         a=0;
 9 InBlock.gif         cout<<"Default constructor called. A\n";
10 ExpandedSubBlockEnd.gif    }
11 InBlock.gif    A(int i)
12 ExpandedSubBlockStart.gif ContractedSubBlock.gif    {
13 InBlock.gif        a=i;
14 InBlock.gif        cout<<"Constructor called. A\n";
15 ExpandedSubBlockEnd.gif    }
16 InBlock.gif    ~A()
17 ExpandedSubBlockStart.gif ContractedSubBlock.gif    {
18 InBlock.gif        cout<<"Destructor called. A\n";
19 ExpandedSubBlockEnd.gif    }
20 InBlock.gif    void Print()
21 ExpandedSubBlockStart.gif ContractedSubBlock.gif    {
22 InBlock.gif        cout<<a<<",";
23 ExpandedSubBlockEnd.gif    }
24 InBlock.gif    int Geta()
25 ExpandedSubBlockStart.gif ContractedSubBlock.gif    {
26 InBlock.gif        return a;
27 ExpandedSubBlockEnd.gif    }
28 InBlock.gifprivate:
29 InBlock.gif    int a;
30 ExpandedBlockEnd.gif};
31 None.gifclass B:public A
32 ExpandedBlockStart.gif ContractedBlock.gif{
33 InBlock.gifpublic:
34 InBlock.gif    B()
35 ExpandedSubBlockStart.gif ContractedSubBlock.gif    {
36 InBlock.gif        b=0;
37 InBlock.gif        cout<<"Default constructor called. B\n";
38 ExpandedSubBlockEnd.gif    }
39 InBlock.gif    B(int i,int j,int k);
40 InBlock.gif    ~B()
41 ExpandedSubBlockStart.gif ContractedSubBlock.gif    {
42 InBlock.gif        cout<<"Destructor called. B\n";
43 ExpandedSubBlockEnd.gif    }
44 InBlock.gif    void Print()
45 ExpandedSubBlockStart.gif ContractedSubBlock.gif    {
46 InBlock.gif        A::Print();
47 InBlock.gif        cout<<b<<","<<aa.Geta()<<endl;
48 ExpandedSubBlockEnd.gif    }
49 InBlock.gifprivate:
50 InBlock.gif    int b;
51 InBlock.gif    A aa;
52 ExpandedBlockEnd.gif};
53 None.gif
54 None.gifB::B(int i,int j,int k):A(i),aa(j),b(k)
55 ExpandedBlockStart.gif ContractedBlock.gif{
56 InBlock.gif    cout<<"Constructor called. B\n";
57 ExpandedBlockEnd.gif}
58 None.gifint main()
59 ExpandedBlockStart.gif ContractedBlock.gif{
60 InBlock.gif    B bb[2];
61 InBlock.gif    bb[0]=B(8,3,9);
62 InBlock.gif    bb[1]=B(17,-18,19);
63 InBlock.gif    int i;
64 InBlock.gif    for(i=0;i<2;i++)
65 InBlock.gif        bb[i].Print();
66 InBlock.gif    return 0;
67 ExpandedBlockEnd.gif}
68 None.gif

运行结果为:
None.gifDefault constructor called A.
None.gifDefault constructor called A.
None.gifDefault constructor called B.
None.gifDefault constructor called A.
None.gifDefault constructor called A.
None.gifDefault constructor called B.
None.gifConstructor called. A
None.gifConstructor called. A
None.gifConstructor called. B
None.gifDestructor called. B
None.gifDestructor called. A
None.gifDestructor called. A
None.gifConstructor called. A
None.gifConstructor called. A
None.gifConstructor called. B
None.gifDestructor called. B
None.gifDestructor called. A
None.gifDestructor called. A
None.gif8,9,3
None.gif17,19,-18
None.gifDestructor called. B
None.gifDestructor called. A
None.gifDestructor called. A
None.gifDestructor called. B
None.gifDestructor called. A
None.gifDestructor called. A
分析:

派生类B的构造函数定义如下:
B( int i, int j, int k):A(i),aa(j),b(k)
{....}

该构造函数的成员初始表有3项:A(i)是调用直接基类的一个参数的构造函数给基类数据成员初始化;aa(j)是调用子对象的构造函数给子对象数据成员初始化;b(k)是对派生类本事的数据成员b初始化,该项可放在函数体内。
主函数中先创建一个B类的数组bb,它有两个元素,每个元素创建时调用一次B类的默认构造函数,该构造函数隐含调用基类默认构造函数,子对象类默认构造函数和自身函数体,因此,显示3条信息。
程 序中的两条给数组元素bb[0]和bb[1]赋值的语句,共显示出12行信息。因为每调用一次派生类中带有3个参数的构造函数,则要显示输出3行信息,创 建一个临时对象,完成了赋值任务后,要将临时对象释放掉,调用一次派生类B的析构函数,又要产生3行信息,注意其顺序与调用构造函数时所显示的3行信息顺 序相反。
程序最后显示的6行信息是程序结束前将对象数组bb的两个元素释放掉所产生的。



读程序
None.gif #include < iostream >
None.gif
using   namespace  std;
None.gif
None.gif
class  Base1
ExpandedBlockStart.gif
{
InBlock.gif    
int d1;
InBlock.gif    
public:
InBlock.gif    Base1(
int i)
ExpandedSubBlockStart.gif    
{
InBlock.gif        d1
=i;
InBlock.gif        cout
<<"constructing Base1 "<<d1<<endl;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
;
None.gif
class  Base2
ExpandedBlockStart.gif
{
InBlock.gif    
int d2;
InBlock.gif    
public:
InBlock.gif    Base2(
int j)
ExpandedSubBlockStart.gif    
{
InBlock.gif        d2
=j;
InBlock.gif        cout
<<"constructing Base2 "<<d2<<endl;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
;
None.gif
class  Base3
ExpandedBlockStart.gif
{
InBlock.gif    
int d3;
InBlock.gif    
public:
InBlock.gif    Base3(
int k=0)
ExpandedSubBlockStart.gif    
{
InBlock.gif        d3
=k;
InBlock.gif        cout
<<"constructing Base3 "<<d3<<endl;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
;
None.gif
class  Derived: public  Base3, public  Base1, public  Base2
ExpandedBlockStart.gif
{
InBlock.gif    
int d4;
InBlock.gif    Base1 memberObj1;
InBlock.gif    Base3 memberObj3;
InBlock.gif    Base2 memberObj2;
InBlock.gif    
public:
InBlock.gif    Derived(
int a,int b,int c,int d,int e)
InBlock.gif           :memberObj2(d),memberObj3(b),memberObj1(c),Base2(a),Base1(b)
ExpandedSubBlockStart.gif    
{
InBlock.gif        d4
=e;
InBlock.gif        cout
<<"constructing Derived "<<d4<<endl;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
;
None.gif
int  main()
ExpandedBlockStart.gif
{
InBlock.gif    Derived obj(
1,2,3,4,5);
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif

结果显示:
constructing Base3 0
constructing Base1 2
constructing Base2 1
constructing Base1 3
constructing Base3 2
constructing Base2 4
constructing Derived 5
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值