在C++中子類繼承和呼叫父類的建構函式方法

lostinai發表於2014-06-10
構造方法用來初始化類的物件,與父類的其它成員不同,它不能被子類繼承(子類可以繼承父類所有的成員變數和成員方法,但不繼承父類的構造方法)。因此,在建立子類物件時,為了初始化從父類繼承來的資料成員,系統需要呼叫其父類的構造方法。

    如果沒有顯式的建構函式,編譯器會給一個預設的建構函式,並且該預設的建構函式僅僅在沒有顯式地宣告建構函式情況下建立。

構造原則如下:

    1. 如果子類沒有定義構造方法,則呼叫父類的無引數的構造方法。

    2. 如果子類定義了構造方法,不論是無引數還是帶引數,在建立子類的物件的時候,首先執行父類無引數的構造方法,然後執行自己的構造方法。

    3. 在建立子類物件時候,如果子類的建構函式沒有顯示呼叫父類的建構函式,則會呼叫父類的預設無參建構函式。

    4. 在建立子類物件時候,如果子類的建構函式沒有顯示呼叫父類的建構函式且父類自己提供了無參建構函式,則會呼叫父類自己的無參建構函式。

    5. 在建立子類物件時候,如果子類的建構函式沒有顯示呼叫父類的建構函式且父類只定義了自己的有參建構函式,則會出錯(如果父類只有有引數的構造方法,則子類必須顯示呼叫此帶參構造方法)。

    6. 如果子類呼叫父類帶引數的構造方法,需要用初始化父類成員物件的方式,比如:


<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln" style="color:#000000;">      </span><span class="com" style="color:#8800;">#include</span><span class="pln" style="color:#000000;"> </span><span class="str" style="color:#0880;"><iostream.h></span>

<span class="pln" style="color:#000000;">  </span><span class="kwd" style="color:#0088;">class</span><span class="pln" style="color:#000000;"> animal</span>
<span class="pln" style="color:#000000;">  </span><span class="pun" style="color:#66660;">{</span>
<span class="pln" style="color:#000000;">  </span><span class="kwd" style="color:#0088;">public</span><span class="pun" style="color:#66660;">:</span>
<span class="pln" style="color:#000000;">    animal</span><span class="pun" style="color:#66660;">(</span><span class="kwd" style="color:#0088;">int</span><span class="pln" style="color:#000000;"> height</span><span class="pun" style="color:#66660;">,</span><span class="pln" style="color:#000000;"> </span><span class="kwd" style="color:#0088;">int</span><span class="pln" style="color:#000000;"> weight</span><span class="pun" style="color:#66660;">)</span>
<span class="pln" style="color:#000000;">    </span><span class="pun" style="color:#66660;">{</span>
<span class="pln" style="color:#000000;">      cout</span><span class="pun" style="color:#66660;"><<</span><span class="str" style="color:#0880;">"animal construct"</span><span class="pun" style="color:#66660;"><<</span><span class="pln" style="color:#000000;">endl</span><span class="pun" style="color:#66660;">;</span>
<span class="pln" style="color:#000000;">    </span><span class="pun" style="color:#66660;">}</span>
<span class="pln" style="color:#000000;">    </span><span class="pun" style="color:#66660;">…</span>
<span class="pln" style="color:#000000;">  </span><span class="pun" style="color:#66660;">};</span>

<span class="pln" style="color:#000000;">  </span><span class="kwd" style="color:#0088;">class</span><span class="pln" style="color:#000000;"> fish</span><span class="pun" style="color:#66660;">:</span><span class="kwd" style="color:#0088;">public</span><span class="pln" style="color:#000000;"> animal</span>
<span class="pln" style="color:#000000;">  </span><span class="pun" style="color:#66660;">{</span>
<span class="pln" style="color:#000000;">  </span><span class="kwd" style="color:#0088;">public</span><span class="pun" style="color:#66660;">:</span>
<span class="pln" style="color:#000000;">    fish</span><span class="pun" style="color:#66660;">():</span><span class="pln" style="color:#000000;">animal</span><span class="pun" style="color:#66660;">(</span><span class="lit" style="color:#06666;">400</span><span class="pun" style="color:#66660;">,</span><span class="lit" style="color:#06666;">300</span><span class="pun" style="color:#66660;">)</span>
<span class="pln" style="color:#000000;">    </span><span class="pun" style="color:#66660;">{</span>
<span class="pln" style="color:#000000;">      cout</span><span class="pun" style="color:#66660;"><<</span><span class="str" style="color:#0880;">"fish construct"</span><span class="pun" style="color:#66660;"><<</span><span class="pln" style="color:#000000;">endl</span><span class="pun" style="color:#66660;">;</span>
<span class="pln" style="color:#000000;">    </span><span class="pun" style="color:#66660;">}</span>
<span class="pln" style="color:#000000;">    </span><span class="pun" style="color:#66660;">…</span>
<span class="pln" style="color:#000000;">  </span><span class="pun" style="color:#66660;">};</span>
<span class="pln" style="color:#000000;">  </span><span class="kwd" style="color:#0088;">void</span><span class="pln" style="color:#000000;"> main</span><span class="pun" style="color:#66660;">()</span>
<span class="pln" style="color:#000000;">  </span><span class="pun" style="color:#66660;">{</span>
<span class="pln" style="color:#000000;">    fish fh</span><span class="pun" style="color:#66660;">;</span>
<span class="pln" style="color:#000000;">  </span><span class="pun" style="color:#66660;">}</span>
</p>

  在fish類的建構函式後,加一個冒號(:),然後加上父類的帶引數的建構函式。這樣,在子類的建構函式被呼叫時,系統就會去呼叫父類的帶引數的建構函式去構造物件。這種初始化方式,還常用來對類中的常量(const)成員進行初始化,如下面的程式碼所示:

  

<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="kwd" style="color:#0088;">class</span><span class="pln" style="color:#000000;"> point</span>
<span class="pln" style="color:#000000;">  </span><span class="pun" style="color:#66660;">{</span>
<span class="pln" style="color:#000000;">  </span><span class="kwd" style="color:#0088;">public</span><span class="pun" style="color:#66660;">:</span>
<span class="pln" style="color:#000000;">     point</span><span class="pun" style="color:#66660;">():</span><span class="pln" style="color:#000000;">x</span><span class="pun" style="color:#66660;">(</span><span class="lit" style="color:#06666;">0</span><span class="pun" style="color:#66660;">),</span><span class="pln" style="color:#000000;">y</span><span class="pun" style="color:#66660;">(</span><span class="lit" style="color:#06666;">0</span><span class="pun" style="color:#66660;">)</span>
<span class="pln" style="color:#000000;">  </span><span class="kwd" style="color:#0088;">private</span><span class="pun" style="color:#66660;">:</span>
<span class="pln" style="color:#000000;">     </span><span class="kwd" style="color:#0088;">const</span><span class="pln" style="color:#000000;"> </span><span class="kwd" style="color:#0088;">int</span><span class="pln" style="color:#000000;"> x</span><span class="pun" style="color:#66660;">;</span>
<span class="pln" style="color:#000000;">     </span><span class="kwd" style="color:#0088;">const</span><span class="pln" style="color:#000000;"> </span><span class="kwd" style="color:#0088;">int</span><span class="pln" style="color:#000000;"> y</span><span class="pun" style="color:#66660;">;</span>
<span class="pln" style="color:#000000;">  </span><span class="pun" style="color:#66660;">};</span>
</p>

  當然,類中普通的成員變數也可以採取此種方式進行初始化,然而,這就沒有必要了。

來源:http://hi.baidu.com/fwxh101/blog/item/bb9ef38051474d8cf603a68b.html

相關文章