C++ class帶參數構造函數的繼承方法


C++以及其它與之相似的編程語言的構造函數與類名相同,這個跟Delphi使用Create作為構造函數名稱的方式大相徑庭,因此在派生類中,如果要覆蓋基類的構造函數,就不能采用Delphi的方式,但是簡單使用傳遞與基類構造函數相同類型/順序的參數的話,則整個代碼無法編譯,編譯器會告訴你 “ no matching function for call to `OldSerial::OldSerial()' ”諸如此類的錯誤提示,提示你沒有函數調用基類的構造函數。

 

后來經過ASSISS的指點,需要在派生類的構造函數后面添加“:基類構造函數名(參數列表)”,用這個方式使得派生類的構造函數在初始化的時候,便調用基類的構造函數,並將參數傳遞給基類構造函數。目前尚不得知道,C++是否支持像Delphi那樣,在派生類的構造函數中,先執行自己的初始化代碼,然后再調用基類構造函數。

 

示例代碼:

OldSerial(int Rx, int Tx)  :  基類

NewSerial(int Rx, int Tx)  :  派生類

 

OldSerial頭文件

 1 // Class automatically generated by Dev-C++ New Class wizard
 2 
 3 #ifndef OLDSERIAL_H
 4 #define OLDSERIAL_H
 5 
 6 /*
 7  * No description
 8  */
 9 class OldSerial
10 {
11     public:
12         // class constructor
13         OldSerial(int Rx, int Tx);
14         // class destructor
15         ~OldSerial();
16             void showMessage();        
17     private:
18             int rx;
19             int tx;
20             
21 
22 };
23 
24 #endif // OLDSERIAL_H

 

OldSerial實施代碼

 1 // Class automatically generated by Dev-C++ New Class wizard
 2 
 3 #include "oldserial.h" // class's header file
 4 #include <cstdlib>
 5 #include <iostream>
 6 
 7 using namespace std;
 8 
 9 // class constructor
10 OldSerial::OldSerial(int Rx, int Tx)
11 {
12     // insert your code here
13     rx=Rx;
14     tx=Tx;
15 }
16 
17 // class destructor
18 OldSerial::~OldSerial()
19 {
20     // insert your code here
21 }
22 
23 void OldSerial::showMessage()
24 {
25      cout << rx <<endl;
26      cout << tx << endl;
27 }

 

NewSerial頭文件

 1 // Class automatically generated by Dev-C++ New Class wizard
 2 
 3 #ifndef NEWSERIAL_H
 4 #define NEWSERIAL_H
 5 
 6 #include "oldserial.h" // inheriting class's header file引用基類 
 7 
 8 /*
 9  * Jack Zhong
10  * @May 10, 2012
11  */
12 class NewSerial : public OldSerial
13 {
14     public:
15         // class constructor
16         NewSerial(int Rx, int Tx);//
17         // class destructor
18         ~NewSerial();
19 };
20 
21 #endif // NEWSERIAL_H

 

NewSerial實現代碼

 1 // Class automatically generated by Dev-C++ New Class wizard
 2 
 3 #include "newserial.h" // class's header file
 4 #include "oldserial.h"
 5 
 6 // class constructor
 7 NewSerial::NewSerial(int Rx, int Tx): OldSerial(Rx, Tx)
 8 {
 9     // insert your code here
10     Rx++;
11     Tx++;
12 }
13 
14 // class destructor
15 NewSerial::~NewSerial()
16 {
17     // insert your code here
18 }

 

main.cpp

main.cpp
 1 #include <cstdlib>
 2 #include <iostream>
 3 
 4 #include "oldserial.h"
 5 #include "newserial.h"
 6 
 7 using namespace std;
 8 
 9 
10       
11       
12       
13 int main(int argc, char *argv[])
14 {
15     NewSerial ns(2,3);
16     ns.showMessage();
17     
18     system("PAUSE");
19     return EXIT_SUCCESS;
20 }

 

執行結果截圖

創建的是NewSerial派生類的實例,但是displayMessage()是基類的方法,在派生類中,構造函數並沒有直接處理參數,而是通過傳遞給基類構造函數的方式,交由displayMessage()顯示出來。因此帶參數的構造函數的覆蓋目標基本實現。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM