C++之继承与派生(2)

上一节,主要讲解了有关派生类继承方式的内容。那么今天就来说说派生类的构造函数和析构函数,以及怎么样在派生类中显式访问积累成员。大家都知道,基类的构造函数和析构函数是不能被继承的,因此我们必须在派生类的构造函数中对基类的构造函数所需要的参数进行设置。同样,对于派生类对象的清理工作也需要加入新的析构函数。

1.那么该如何构造呢?对于简单的派生类,即只有一个基类,且直接派生(多继承将在后续几节中做详细讲解),来讲,如果基类的构造函数没有参数,或者没有显式定义构造函数,那么派生类可以不向基类传递参数,甚至可以不定义构造函数。但是一旦基类含有带参数的构造函数时,派生类必须定义构造函数,并把参数传递给基类构造函数。其一般格式:

派生类名(参数总表)::基类名(参数子表)

{

派生类新增成员的初始化语句;

}

而析构函数可以有用户自己定义,由于其是不带参数的,所以在派生类中是否要自定义析构函数与它所属基类的析构函数无关。在执行派生类的构造函数时,系统会自动调用基类的析构函数,进行对象清理。下面举例来说明一下简单派生类的构造和析构:
C++之继承与派生(2)C++之继承与派生(2)View Code

1 #include "stdafx.h" 2 #include <iostream> 3 #include <string> 4  5 class C 6 { 7 private: 8         std::string c; 9 public:10         C(){};11         C(std::string c);12 ~C();13 void showC();14 };15 16 C::C(std::string c)17 {18 this->c=c;19     std::cout<<"构造对象c"<<std::endl;20 }21 22 C::~C()23 {24     std::cout<<"清理对象c"<<std::endl;25 }26 void C::showC()27 {28     std::cout<<"c="<<this->c<<std::endl;29 }30 31 32 class D:private C33 {34 private:35         std::string d;36 public:37         D(std::string d,std::string c);38 ~D();39 void showD();40 };41 42 D::D(std::string d,std::string c):C(c) //参数传递给基类,也可以不写C(c),那么就调用了C类中无参构造函数43 {44 this->d=d;45     std::cout<<"构造对象d"<<std::endl;46 }47 48 D::~D()49 {50     std::cout<<"清理对象d"<<std::endl;51 }52 53 void D::showD()54 {55     showC();56     std::cout<<"d="<<this->d<<std::endl;57 }58 59 60 61 int main()62 {63     {64         D d("ddd","ccc");65         d.showD();66     }67 68 return0;69 }

结果:

C++之继承与派生(2)

在结果里我们可以看到派生类构造函数和析构函数的调用顺序,当创建派生类对象时,首先调用基类的构造函数,再调用派生类构造函数,而当清理对象时,则刚好相反。

那么如果当派生类中存在成员对象,那么构造函数和析构函数的调用顺序又如何呢?在解答这个问题之前,我们先看一下当派生类中含有子对象时,构造函数的一般格式:

派生类名(参数总表):基类名(参数子表),对象名1(参数子表1),对象名2(参数子表2)

{

派生类新增成员的初始化语句;

}

还是,我们用一个示例来说明,并通过实际的运行结果来解答上述的问题:
C++之继承与派生(2)C++之继承与派生(2)View Code

1 #include "stdafx.h" 2 #include <iostream> 3 #include <string> 4  5 class C 6 { 7 private: 8         std::string c; 9 public:10         C(){};11         C(std::string c);12 ~C();13 void showC();14 };15 16 C::C(std::string c)17 {18 this->c=c;19     std::cout<<"构造c值为"<<c<<"的对象c"<<std::endl;20 }21 22 C::~C()23 {24     std::cout<<"清理c值为"<<c<<"的对象c"<<std::endl;25 }26 void C::showC()27 {28     std::cout<<"c="<<this->c<<std::endl;29 }30 31 32 class D:private C33 {34 private:35         std::string d;36         C c;37 public:38         D(std::string d,std::string c1,std::string c2);39 ~D();40 void showD();41 };42 43 D::D(std::string d,std::string c1,std::string c2):c(c2),C(c1) //参数传递给基类,也可以不写C(c1)或c(c2),那么就调用了C类中无参构造函数44 {45 this->d=d;46     std::cout<<"构造对象d"<<std::endl;47 }48 49 D::~D()50 {51     std::cout<<"清理对象d"<<std::endl;52 }53 54 void D::showD()55 {56     showC();57     c.showC();58     std::cout<<"d="<<this->d<<std::endl;59 }60 61 62 63 int main()64 {65     {66         D d("ddd","ccc1","ccc2");67         d.showD();68     }69 70 return0;71 }

结果:

C++之继承与派生(2)

从结果中很清楚的说明了其构造函数和析构函数的调用顺序。另外读者也可以在类D成员对象c后面再声明一个基类对象,运行看看,修改过后其构造和析构的调用顺序又如何?最后一点要补充的是,如果派生类的基类也是派生类,那么每个派生类只需要负责其直接的基类数据成员的初始化

2.在定义派生类时,C++里是允许派生类中的成员名和基类中的成员名相同,出现这种情况,我们称派生类成员覆盖了基类中使用相同名称的成员。也就是说当你在派生类中或用对象访问该同名成员时,你所访问只是派生类中的成员,基类中的就自动被忽略。但如果我确实要访问到基类中的同名成员那怎么办,C++中这样规定必须在成员名前加上基类名和作用域标识符"::"。

3.最后还是一样,我将用一个示例来总结一下今天所讲的内容(开发工具:vs2010):
C++之继承与派生(2)C++之继承与派生(2)View Code

1 #include "stdafx.h" 2 #include <iostream> 3 #include <string> 4  5 class C 6 {     7 public: 8         std::string c; 9         C(){};10         C(std::string c);11 ~C();12 void showC();13 };14 15 C::C(std::string c)16 {17 this->c=c;18     std::cout<<"构造c值为"<<c<<"的对象c"<<std::endl;19 }20 21 C::~C()22 {23     std::cout<<"清理c值为"<<c<<"的对象c"<<std::endl;24 }25 void C::showC()26 {27     std::cout<<"c="<<this->c<<std::endl;28 }29 30 31 class D:public C32 {33 private:34         std::string c;//同名数据成员35         std::string d;36         C c2;37         C c3;38 public:39         D(std::string d,std::string c1,std::string c2);40 ~D();41 void showC();//同名成员函数42 void updateC(std::string c);43 };44 45 D::D(std::string d,std::string c2,std::string c3):c2(c2),c3(c3),C("ccc1") //参数传递给基类,也可以不写C(c1)或c(c2),那么就调用了C类中无参构造函数46 {47 this->d=d;48 this->c=c2;//覆盖了基类的数据成员c,访问的是派生类的数据数据成员c49     std::cout<<"构造对象d"<<std::endl;50 }51 52 D::~D()53 {54     std::cout<<"清理对象d"<<std::endl;55 }56 57 void D::showC()//同名成员函数58 {59     C::showC();//显式访问基类成员函数showC();60     c2.showC();61     std::cout<<"D::c="<<c<<std::endl;//覆盖了基类的数据成员c62     std::cout<<"C::c="<<C::c<<std::endl;//显式访问基类数据成员c63     std::cout<<"d="<<this->d<<std::endl;64 }65 66 void D::updateC(std::string c)67 {68     C::c=c;//显式访问基类数据成员c69 }70 71 72 73 int main()74 {75     {76         D d("ddd","ccc2","ccc3");77         d.showC();//访问的是派生类中函数成员showC()78         d.updateC("ccc4");79         d.showC();//同理80     }81 82 return0;83 }

结果:

C++之继承与派生(2)
原文链接: https://www.cnblogs.com/CaiNiaoZJ/archive/2011/08/09/2131942.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/30177

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午7:32
下一篇 2023年2月8日 上午7:32

相关推荐