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