c++面向对象基础教程————派生类中的析构函数和构造函数(一)

简介: c++面向对象基础教程————派生类中的析构函数和构造函数(一)

继承机制下的构造函数和析构函数


构造函数和析构函数我们在前面已经学过了,但是继承机制下的构造函数和析构函数又是如何调用以及定义的喃?上节我们没有讲构造函数和析构函数,就是这部分比较难,很多小伙伴不知道如何使用,所以我单独来讲解。


构造函数


在派生类的生成过程中,派生类继承基类的大部分成员,但不继承基类的构造函数和析构函数。(包括拷贝构造函数);


继承机制下的构造函数的调用顺序


派生类对象的数据结构与基类中声明的数据成员和派生类中声明的数据成员共同构成。由于构造函数不能被继承,所以,在定义派生类的构造函数时,除了对自己的对象初始化外,还要调用基类中的构造函数来初始化基类数据成员,这和初始化子对象有相似之处。派生之类的构造函数的一般格式为:

<派生类名>::<派生类>(<参数>):<基类>(<参数>),~

{

<派生类中成员的初始化>

}


单继承机制下构造函数的调用函数


当说明派生类的一个对象时,首先调用基类的构造函数,对基类成员进行初始化,然后再执行派生类的构造函数。如果某个基类仍是一个派生类,则这个过程递归进行。


#include<iostream>
using namespace std;
class Baseclass
{
public:
  Baseclass(int i);//基类的构造函数;
private:
  int a;
};
Baseclass::Baseclass(int i)
{
  a = i;
  cout << "constructing Baseclass a=" << a << endl;
}
class Derivedclass :public Baseclass
{
public:
  Derivedclass(int =0, int =0);
private:
  int b;
};
Derivedclass::Derivedclass(int i, int j) :Baseclass(i)//派生类的构造函数;
{
  b = j;
  cout << "constructing Derivedclass b=" << b << endl;
}
int main()
{
  Derivedclass x(5, 6);
  return 0;
}


cdff9407f1fe7dca00acc07b6722b65d_bf96c6fe229f40fdb791ab0eb994e273.png


通过上面的代码例子以及运行结果,我们可以发现,先是基类的构造函数被调用,然后是派生类构造函数。那我们是不死就可以得到继承关系下的构造函数调用的顺序规则;


继承关系下的构造函数的调用规则


如果派生类还包括子对象,则对子对象的构造函数的调用,仍然在初始化列表中完成。此时,当说明派生类的一个对象时,首先基类构造函数被调用,子对象所在的类构造函数次之,最后执行派生类构造函数,在有多个子对象的情况下,子对象的调用顺序取决于他们在派生类中被说明的顺序。

下面我们来看一个例子,在包括子对象的时候,其构造函数的调用顺序:


//#include<iostream>
//using namespace std;
//class Baseclass
//{
//public:
//  Baseclass(int i);//基类的构造函数;
//private:
//  int a;
//};
//Baseclass::Baseclass(int i)
//{
//  a = i;
//  cout << "constructing Baseclass a=" << a << endl;
//}
//class Derivedclass :public Baseclass
//{
//public:
//  Derivedclass(int =0, int =0);
//private:
//  int b;
//};
//Derivedclass::Derivedclass(int i, int j) :Baseclass(i)//派生类的构造函数;
//{
//  b = j;
//  cout << "constructing Derivedclass b=" << b << endl;
//}
//int main()
//{
//  Derivedclass x(5, 6);
//  return 0;
//}
#include<iostream>
using namespace std;
class Base1               //基类
{
public:
  Base1(int = 0);
private:
  int a;
};
Base1::Base1(int i)
{
  a = i;
  cout << "constructing Base1 a=" << a << endl;
}
class Base2            //子对象f所属类;
{
public:
  Base2(int = 0);
private:
  int b;
};
Base2::Base2(int i)
{
  b = i;
  cout << "constructing Base2 b=" <<b<< endl;
}
class Base3               //子对象g所属的类
{
public:
  Base3(int =0);
private:
  int c;
};
Base3::Base3(int i)
{
  c = i;
  cout << "constructing Base3 c=" << c << endl;
}
class Derivedclass :public Base1      //派生类
{
public:
  Derivedclass(int = 0, int = 0, int = 0,int =0);
private:
  int d;
  Base2 f;
  Base3 g;
};
Derivedclass::Derivedclass(int i, int j, int k, int m) :Base1(i), g(i), f(k)
{
  d = m;
  cout << "constructing Derivedclass d=" << d << endl;
}
int main()
{
  Derivedclass x(5, 6, 7, 8);
  return 0;
}


7eb85414630fc3bf8164c27a4378d9c6_99c857876a714800b263ed7233cfcf0f.png


上面的程序我们可以看出,在构造函数的调用顺序中,先调用的是基类的构造函数,然后才是子对象的调用函数,然后是派生类的构造函数的调用;

注意:子对象的调用顺序只取决于它们在派生类中被说明的顺序,与它们在成员初始化列表的顺序有关。


多继承机制下构造函数的调用顺序


多继承方式下派生类的构造函数序同时负责该派生类所有基类构造函数的调用。构造函数恶调用顺序是:先调用所有基类的构造函数,再调用派生类中子对象类的构造函数(如果派生类中有子对象),最后调用派生类的构造函数。处于同一层次的各基类构造函数的调用顺序取决于定义派生类所指定的基类顺序,与派生类构造函数中定义的成员初始化列表顺序无关。



#include<iostream>
using namespace std;
class Base1                  //基类
{
public:
  Base1(int = 0);//基类构造函数;
private:
  int a;
};
Base1::Base1(int i)
{
  a = i;
  cout << "constructing Base1 a=" <<a<<endl;
}
class Base2                  //基类
{
public:
  Base2(int = 0);           //基类构造函数;
private:
  int b;
};
Base2::Base2(int i)
{
  b = i;
  cout << "constructing Base2 b=" << b << endl;
}
class Derivedclass :public Base1, public Base2   //派生类;在派生类构造函数的调用顺序为定义的顺序;
{
public:
  Derivedclass(int = 0, int = 0, int = 0);
private:
  int d;
};
Derivedclass::Derivedclass(int i, int j, int k):Base2(i),Base1(j)
{
  d = k;
  cout << "constructing d=" << d << endl;
}
int main()
{
  Derivedclass x(4, 5, 6);
  return 0;
}


bf3fd594e5229c3654e80f453dfb7925_2c8c0cbea3fd43bbb621b4ab5f95154d.png


通过上面的程序,我们可以看出,在多继承的机制下,基类构造函数的调用顺序取决于在派生类中定义的顺序,和在初始化成员列表顺序无关。但是,如果在有虚拟基类的情况下,调用顺序又是怎样的?


虚拟基类的构造函数调用顺序

我们先来看一段代码吧。


#include<iostream>
using namespace std;
class Base1
{
public:
  Base1();
};
Base1::Base1()
{
  cout << "constructing Base1" << endl;
}
class Base2
{
public:
  Base2()
  {
  cout << "constructing Base2" << endl;
  }
};
class Derived1 :public Base2, virtual public Base1
{
public:
  Derived1()
  {
  cout << "constructing Derived1" << endl;
  }
};
class Derived2 :public Base2, virtual public Base1
{
public:
  Derived2()
  {
  cout << "constructing Derived2" << endl;
  }
};
class Derived3 :public Derived1, virtual public Derived2
{
public:
  Derived3()
  {
  cout << "construct Derived3" << endl;
  }
};
int main()
{
  Derived3 obj;
  return 0;
}


d4091b94460515afa9d0e300daaad07b_7e20fc278aa547ffa36776138d9a6554.png


如果派生类有一个虚基类作为祖先类,那么在派生类构造函数的初始化列表中需要列出对虚基类构造函数的调用,如果未列出则表明用的是虚基类的无参构造函数,不论成员初始化列表顺序如何,对虚基类的构造函数的调用总是先于普通基类的构造函数。

如果是在若干类层次中,从虚基类直接或间接派生出来的子类的构造函数初始化列表均有对该虚基类构造函数的调用,那么创建一个子类对象时只有该子类列出的虚基类的构造函数被调用,其他类列出的将被忽略,这样就保证虚基类的唯一副本只被初始化一次。

这个理解起来可能很难,我们可以通过上面的程序来理解,在派生类调用构造函数Derived1的时候,虚基类Base1没有被调用,表明,虚基类只能被调用一次,也就是副本只能被初始化一次。

下main,我们通过一张图来展示:


38b06586c8255559fb6c72209e1f5b4b_c2cd3ae79eef474892f65458a81162b4.png


相关文章
|
6天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
22 0
|
6天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
19 0
|
5天前
|
设计模式 Java C++
【C++高阶(八)】单例模式&特殊类的设计
【C++高阶(八)】单例模式&特殊类的设计
|
5天前
|
编译器 C++
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
|
9天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
11天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
1月前
|
存储 C++ 容器
C++入门指南:string类文档详细解析(非常经典,建议收藏)
C++入门指南:string类文档详细解析(非常经典,建议收藏)
39 0
|
1月前
|
存储 编译器 C语言
C++入门: 类和对象笔记总结(上)
C++入门: 类和对象笔记总结(上)
34 0
|
11天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”