首页 > 编程 > C++ > 正文

深入解析C++中派生类的构造函数

2020-05-23 14:14:06
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了深入解析C++中派生类的构造函数,是C++入门学习中的基础知识,需要的朋友可以参考下

基类的构造函数不能被继承,在声明派生类时,对继承过来的成员变量的初始化工作也要由派生类的构造函数来完成。所以在设计派生类的构造函数时,不仅要考虑派生类新增的成员变量,还要考虑基类的成员变量,要让它们都被初始化。

解决这个问题的思路是:在执行派生类的构造函数时,调用基类的构造函数。

下面的例子展示了如何在派生类的构造函数中调用基类的构造函数。

 

 
  1. #include<iostream> 
  2. using namespace std; 
  3. //基类 
  4. class People{ 
  5. protected: 
  6. char *name; 
  7. int age; 
  8. public: 
  9. People(char*, int); 
  10. }; 
  11. People::People(char *name, int age): name(name), age(age){} 
  12. //派生类 
  13. class Student: public People{ 
  14. private: 
  15. float score; 
  16. public: 
  17. Student(char*, int, float); 
  18. void display(); 
  19. }; 
  20. //调用了基类的构造函数 
  21. Student::Student(char *name, int age, float score): People(name, age){ 
  22. this->score = score; 
  23. } 
  24. void Student::display(){ 
  25. cout<<name<<"的年龄是"<<age<<",成绩是"<<score<<endl; 
  26. } 
  27. int main(){ 
  28. Student stu("小明", 16, 90.5); 
  29. stu.display(); 
  30. return 0; 
  31. } 

运行结果为:

小明的年龄是16,成绩是90.5

请注意代码第23行:

 

 
  1. Student::Student(char *name, int age, float score): People(name, age) 

这是派生类 Student 的构造函数的写法。冒号前面是派生类构造函数的头部,这和我们以前介绍的构造函数的形式一样,但它的形参列表包括了初始化基类和派生类的成员变量所需的数据;冒号后面是对基类构造函数的调用,这和普通构造函数的参数初始化表非常类似。

实际上,你可以将对基类构造函数的调用和参数初始化表放在一起,如下所示:

 

 
  1. Student::Student(char *name, int age, float score): People(name, age), score(score){} 

基类构造函数和初始化表用逗号隔开。

需要注意的是:冒号后面是对基类构造函数的调用,而不是声明,所以括号里的参数是实参,它们不但可以是派生类构造函数总参数表中的参数,还可以是局部变量、常量等。如下所示:

 

 
  1. Student::Student(char *name, int age, float score): People("李磊", 20) 

基类构造函数调用规则

事实上,通过派生类创建对象时必须要调用基类的构造函数,这是语法规定。也就是说,定义派生类构造函数时最好指明基类构造函数;如果不指明,就调用基类的默认构造函数(不带参数的构造函数);如果没有默认构造函数,那么编译失败。

请看下面的例子:

 

 
  1. #include<iostream> 
  2. using namespace std; 
  3. //基类 
  4. class People{ 
  5. protected: 
  6. char *name; 
  7. int age; 
  8. public: 
  9. People(); 
  10. People(char*, int); 
  11. }; 
  12. People::People(){ 
  13. this->name = "xxx"; 
  14. this->age = 0; 
  15. } 
  16. People::People(char *name, int age): name(name), age(age){} 
  17. //派生类 
  18. class Student: public People{ 
  19. private: 
  20. float score; 
  21. public: 
  22. Student(); 
  23. Student(char*, int, float); 
  24. void display(); 
  25. }; 
  26. Student::Student(){ 
  27. this->score = 0.0; 
  28. } 
  29. Student::Student(char *name, int age, float score): People(name, age){ 
  30. this->score = score; 
  31. } 
  32. void Student::display(){ 
  33. cout<<name<<"的年龄是"<<age<<",成绩是"<<score<<endl; 
  34. } 
  35. int main(){ 
  36. Student stu1; 
  37. stu1.display(); 
  38. Student stu2("小明", 16, 90.5); 
  39. stu2.display(); 
  40. return 0; 
  41. } 

运行结果:

 

 
  1. xxx的年龄是0,成绩是0 
  2. 小明的年龄是16,成绩是90.5 

创建对象 stu1 时,执行派生类的构造函数 Student::Student(),它并没有指明要调用基类的哪一个构造函数,从运行结果可以很明显地看出来,系统默认调用了不带参数的构造函数,也就是 People::People()。

创建对象 stu2 时,执行派生类的构造函数 Student::Student(char *name, int age, float score),它指明了基类的构造函数。

在第31行代码中,如果将 People(name, age) 去掉,也会调用默认构造函数,stu2.display() 的输出结果将变为:

xxx的年龄是0,成绩是90.5

如果将基类 People 中不带参数的构造函数删除,那么会发生编译错误,因为创建对象 stu1 时没有调用基类构造函数。

总结:如果基类有默认构造函数,那么在派生类构造函数中可以不指明,系统会默认调用;如果没有,那么必须要指明,否则系统不知道如何调用基类的构造函数。

构造函数的调用顺序

为了搞清这个问题,我们不妨先来看一个例子:

 

 
  1. #include<iostream> 
  2. using namespace std; 
  3. //基类 
  4. class People{ 
  5. protected: 
  6. char *name; 
  7. int age; 
  8. public: 
  9. People(); 
  10. People(char*, int); 
  11. }; 
  12. People::People(): name("xxx"), age(0){ 
  13. cout<<"PeoPle::People()"<<endl; 
  14. } 
  15. People::People(char *name, int age): name(name), age(age){ 
  16. cout<<"PeoPle::People(char *, int)"<<endl; 
  17. } 
  18. //派生类 
  19. class Student: public People{ 
  20. private: 
  21. float score; 
  22. public: 
  23. Student(); 
  24. Student(char*, int, float); 
  25. }; 
  26. Student::Student(): score(0.0){ 
  27. cout<<"Student::Student()"<<endl; 
  28. } 
  29. Student::Student(char *name, int age, float score): People(name, age), score(score){ 
  30. cout<<"Student::Student(char*, int, float)"<<endl; 
  31. } 
  32. int main(){ 
  33. Student stu1; 
  34. cout<<"--------------------"<<endl; 
  35. Student stu2("小明", 16, 90.5); 
  36. return 0; 
  37. } 

运行结果:

 

 
  1. PeoPle::People() 
  2. Student::Student() 
  3. -------------------- 
  4. PeoPle::People(char *, int) 
  5. Student::Student(char*, int, float) 

从运行结果可以清楚地看到,当创建派生类对象时,先调用基类构造函数,再调用派生类构造函数。如果继承关系有好几层的话,例如:

A --> B --> C

那么则创建C类对象时,构造函数的执行顺序为:

A类构造函数 --> B类构造函数 --> C类构造函数

构造函数的调用顺序是按照继承的层次自顶向下、从基类再到派生类的。

C++有子对象的派生类的构造函数

类的数据成员不但可以是标准型(如int、char)或系统提供的类型(如string),还可以包含类对象,如可以在声明一个类时包含这样的数据成员:

 

 
  1. Student s1; //Student是已声明的类名,s1是Student类的对象 

这时,s1就是类对象中的内嵌对象,称为子对象(subobject),即对象中的对象。

那么,在对数据成员初始化时怎样对子对象初始化呢?请仔细分析下面程序,特别注意派生类构造函数的写法。

[例] 包含子对象的派生类的构造函数。为了简化程序以易于阅读,这里设基类Student的数据成员只有两个,即num和name。

 

  1. #include <iostream> 
  2. #include <string> 
  3. using namespace std; 
  4. class Student//声明基类 
  5. { 
  6. public: //公用部分 
  7. Student(int n, string nam ) //基类构造函数,与例11.5相同 
  8. { 
  9. num=n; 
  10. name=nam; 
  11. } 
  12. void display( ) //成员函数,输出基类数据成员 
  13. { 
  14. cout<<"num:"<<num<<endl<<"name:"<<name<<endl; 
  15. } 
  16. protected: //保护部分 
  17. int num; 
  18. string name; 
  19. }; 
  20. class Student1: public Student //声明公用派生类Student1 
  21. { 
  22. public: 
  23. Student1(int n, string nam,int n1, string nam1,int a, string ad):Student(n,nam),monitor(n1,nam1) //派生类构造函数 
  24. { 
  25. age=a; 
  26. addr=ad; 
  27. } 
  28. void show( ) 
  29. { 
  30. cout<<"This student is:"<<endl; 
  31. display(); //输出num和name 
  32. cout<<"age: "<<age<<endl; //输出age 
  33. cout<<"address: "<<addr<<endl<<endl; //输出addr 
  34. } 
  35. void show_monitor( ) //成员函数,输出子对象 
  36. { 
  37. cout<<endl<<"Class monitor is:"<<endl; 
  38. monitor.display( ); //调用基类成员函数 
  39. } 
  40. private: //派生类的私有数据 
  41. Student monitor; //定义子对象(班长) 
  42. int age; 
  43. string addr; 
  44. }; 
  45. int main( ) 
  46. { 
  47. Student1 stud1(10010,"Wang-li",10001,"Li-sun",19,"115 Beijing Road,Shanghai"); 
  48. stud1.show( ); //输出学生的数据 
  49. stud1.show_monitor(); //输出子对象的数据 
  50. return 0; 
  51. } 

运行时的输出如下:

 

 
  1. This student is: 
  2. num: 10010 
  3. name: Wang-li 
  4. age: 19 
  5. address:115 Beijing Road,Shanghai 
  6. Class monitor is: 
  7. num:10001 
  8. name:Li-sun 

请注意在派生类Student1中有一个数据成员:

 

 
  1. Student monitor; //定义子对象 monitor(班长) 

“班长”的类型不是简单类型(如int、char、float等),它是Student类的对象。我们知道, 应当在建立对象时对它的数据成员初始化。那么怎样对子对象初始化呢?显然不能在声明派生类时对它初始化(如Student monitor(10001, "Li-fun");),因为类是抽象类型,只是一个模型,是不能有具体的数据的,而且每一个派生类对象的子对象一般是不相同的(例如学生A、B、C的班长是A,而学生D、E、F的班长是F)。因此子对象的初始化是在建立派生类时通过调用派生类构造函数来实现的。

派生类构造函数的任务应该包括3个部分:

对基类数据成员初始化;

对子对象数据成员初始化;

对派生类数据成员初始化。

程序中派生类构造函数首部如下:

 

 
  1. Student1(int n, string nam,int n1, string nam1,int a, string ad): 
  2. Student(n,nam),monitor(n1,nam1) 

在上面的构造函数中有6个形参,前两个作为基类构造函数的参数,第3、第4个作为子对象构造函数的参数,第5、第6个是用作派生类数据成员初始化的。

归纳起来,定义派生类构造函数的一般形式为: 派生类构造函数名(总参数表列): 基类构造函数名(参数表列), 子对象名(参数表列)

{

派生类中新增数成员据成员初始化语句

}

执行派生类构造函数的顺序是:

调用基类构造函数,对基类数据成员初始化;

调用子对象构造函数,对子对象数据成员初始化;

再执行派生类构造函数本身,对派生类数据成员初始化。

派生类构造函数的总参数表列中的参数,应当包括基类构造函数和子对象的参数表列中的参数。基类构造函数和子对象的次序可以是任意的,如上面的派生类构造函数首部可以写成

 

 
  1. Student1(int n, string nam,int n1, string nam1,int a, string ad): monitor(n1,nam1),Student(n,nam) 

编译系统是根据相同的参数名(而不是根据参数的顺序)来确立它们的传递关系的。但是习惯上一般先写基类构造函数。

如果有多个子对象,派生类构造函数的写法依此类推,应列出每一个子对象名及其参数表列。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表