C++子類對(duì)象—子類對(duì)象的構(gòu)造和析構(gòu)
2020-05-23 14:25:31
供稿:網(wǎng)友
對(duì)象在使用之前,始終是要經(jīng)歷“構(gòu)造”這個(gè)過(guò)程的。在第15章,我們了解到當(dāng)一個(gè)對(duì)象的成員數(shù)據(jù)是另一個(gè)對(duì)象的時(shí)候,就先運(yùn)行成員對(duì)象的構(gòu)造函數(shù),再運(yùn)行父對(duì)象的構(gòu)造函數(shù)。但是繼承的出現(xiàn),會(huì)引入子類的構(gòu)造函數(shù)。這時(shí)候,這些構(gòu)造函數(shù)的運(yùn)行順序又是怎樣的呢?
子類對(duì)象的構(gòu)造
討論子類對(duì)象的構(gòu)造,就是在討論子類對(duì)象的生成方式。它是先生成父類對(duì)象的成員,再對(duì)其進(jìn)行擴(kuò)展呢,還是先生成子類對(duì)象的成員,然后再對(duì)其進(jìn)行補(bǔ)充?我們還是修改一下程序17.3.2,用事實(shí)來(lái)解決這個(gè)問(wèn)題:(程序17.4.1)
//node.h和linklist.h同程序17.3.2
//stack.h
#include "linklist.h"
class Stack:private Linklist//私有繼承鏈表類
{
public:
bool push(int i,char c);
bool pop(int &i,char &c);
void show();
Stack(int i,char c);
Stack();
};
Stack::Stack(int i,char c):Linklist(i,c)//將子類構(gòu)造函數(shù)的參數(shù)傳遞給父類的構(gòu)造函數(shù)
{
cout <<"Stack constructor with parameter is running..." <<endl;
}
Stack::Stack()//子類構(gòu)造函數(shù)
{
cout <<"Stack constructor is running..." <<endl;
}
bool Stack::push(int i,char c)
{
while (pcurrent->next!=NULL)
pcurrent=pcurrent->next;
return Insert(i,c);
}
bool Stack::pop(int &i,char &c)
{
while (pcurrent->next!=NULL)
pcurrent=pcurrent->next;
i=pcurrent->idata;
c=pcurrent->cdata;
return Delete();
}
void Stack::show()
{
Show();
}
//main.cpp
#include <iostream>
#include "stack.h"
int main()
{
Stack ss(1,'4');//調(diào)用帶參數(shù)的構(gòu)造函數(shù)
cout <<"Stack ss constructed" <<endl;
ss.show();
Stack zz; //調(diào)用不帶參數(shù)的構(gòu)造函數(shù)
cout <<"Stack zz constructed" <<endl;
zz.show();
return 0;
}
運(yùn)行結(jié)果:
Node constructor is running...
Linklist constructor is running...
Stack constructor with parameter is running...
Stack ss constructed
1 4
Node constructor is running...
Linklist constructor is running...
Stack constructor is running...
Stack zz constructed
0 0
Linklist destructor is running...
Node destructor is running...
Linklist destructor is running...
Node destructor is running...
這個(gè)程序中有三個(gè)類,其中Stack類是Linklist類的子類,Node類的對(duì)象是Linklist類的成員數(shù)據(jù)。根據(jù)程序的運(yùn)行結(jié)果,我們可以確定,父類的成員對(duì)象仍然是最先構(gòu)造的,接著是運(yùn)行父類的構(gòu)造函數(shù),最后運(yùn)行子類的構(gòu)造函數(shù)。也就是說(shuō)子類對(duì)象是在父類對(duì)象的基礎(chǔ)上擴(kuò)展而成的。
另外,如果我們希望把子類的構(gòu)造函數(shù)的參數(shù)傳遞給父類的構(gòu)造函數(shù)時(shí),可以在子類的構(gòu)造函數(shù)定義中用以下格式調(diào)用父類的構(gòu)造函數(shù):
子類名::構(gòu)造函數(shù)名(參數(shù)表):父類名(參數(shù)表)
如程序17.4.1就是用上述方法實(shí)現(xiàn)子類和父類的構(gòu)造函數(shù)參數(shù)傳遞。這樣的方法不僅使子類對(duì)象的初始化變得簡(jiǎn)單,并且使子類和父類的構(gòu)造函數(shù)分工明確,易于維護(hù)。
子類對(duì)象的析構(gòu)
在第15章中介紹析構(gòu)函數(shù)的時(shí)候,我們就說(shuō)它的運(yùn)行順序往往是和構(gòu)造函數(shù)的運(yùn)行順序相反的。那么使用了繼承之后,是否依然是這樣的規(guī)律呢?我們繼續(xù)修改程序17.4.1,嘗試驗(yàn)證我們的猜想。
//node.h和linklist.h同程序17.3.2
//stack.h
#include "linklist.h"
class Stack:private Linklist
{
public:
bool push(int i,char c);
bool pop(int &i,char &c);
void show();
Stack(int i,char c);
Stack();
~Stack();//析構(gòu)函數(shù)
};
Stack::Stack(int i,char c):Linklist(i,c)
{
cout <<"Stack constructor with parameter is running..." <<endl;
}
Stack::Stack()
{
cout <<"Stack constructor is running..." <<endl;
}
Stack::~Stack()
{
cout <<"Stack destructor is running..." <<endl;
}
bool Stack::push(int i,char c)
{
while (pcurrent->next!=NULL)
pcurrent=pcurrent->next;
return Insert(i,c);
}
bool Stack::pop(int &i,char &c)
{
while (pcurrent->next!=NULL)
pcurrent=pcurrent->next;
i=pcurrent->idata;
c=pcurrent->cdata;
return Delete();
}
void Stack::show()
{
Show();
}
//main.cpp
#include <iostream>
#include "stack.h"
int main()
{
Stack zz;
cout <<"Stack zz constructed" <<endl;
zz.show();
return 0;
}
運(yùn)行結(jié)果:
Node constructor is running...
Linklist constructor is running...
Stack constructor is running...
Stack zz constructed
0 0
Stack destructor is running...
Linklist destructor is running...
Node destructor is running...
根據(jù)運(yùn)行結(jié)果,我們可以確認(rèn):使用了繼承之后,析構(gòu)函數(shù)的運(yùn)行順序依然恰好與構(gòu)造函數(shù)的運(yùn)行順序相反。