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

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

派生类构造函数的规则


前面的例子中,都是在调用基类中的构造函数。实际上,在基类中定义有默认构造函数或者没有定义任何构造函数时,派生类构造函数中可以省略对基类构造函数的调用,既可以采用隐式调用。


派生类中有构造函数,基类没有


#include<iostream>
using namespace std;
class Baseclass
{
private:
  int a;
};
class Derivedclass :public Baseclass
{
public:
  Derivedclass();//派生类默认我无参构造函数;
  Derivedclass(int i);//派生类有参构造函数;
private:
  int b;
};
Derivedclass::Derivedclass()
{
  cout << "default constructor Derivedclass" << endl;
}
Derivedclass::Derivedclass(int i)
{
  b = i;
  cout << "constructint Derivedclass b=" << b << endl;
}
int main()
{
  Derivedclass x1(5);
  Derivedclass x2;
  return 0;
}


9d8ab8a3946c9ca0d9b234b6dae7c8af_9a7b70e03e414d65b72d8c33a2725215.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;
//}
//#include<iostream>
//using namespace std;
//class Baseclass
//{
//private:
//  int a;
//};
//class Derivedclass :public Baseclass
//{
//public:
//  Derivedclass();//派生类默认我无参构造函数;
//  Derivedclass(int i);//派生类有参构造函数;
//private:
//  int b;
//};
//Derivedclass::Derivedclass()
//{
//  cout << "default constructor Derivedclass" << endl;
//}
//Derivedclass::Derivedclass(int i)
//{
//  b = i;
//  cout << "constructint Derivedclass b=" << b << endl;
//}
//int main()
//{
//  Derivedclass x1(5);
//  Derivedclass x2;
//  return 0;
//}
#include<iostream>
using namespace std;
class Baseclass1
{
public:
  Baseclass1();//不能不定义;
  {
  cout << "default constructing Baseclass1" << endl;
  }
  Baseclass1(int =0);//可以没有;
private:
  int a;
};
/*Baseclass1::Baseclass1()
{
  cout << "default constructing Baseclass1" << endl;
}*/
Baseclass1::Baseclass1(int i)
{
  a = i;
  cout << "constructing Baseclass a=" << a << endl;
}
class Baseclass2 :public Baseclass1
{
private:
  int b;
};
int main()
{
  Baseclass2 x;
  return 0;
}


3b7762f7a07090466599399851a1c951_975018e97fb748e6b1343670b79ea9d6.png


派生类有构造函数,基类有默认构造函数


若派生类有构造函数,且基类有默认构造函数,则创建派生类对象的时候,基类的默认构造函数会自动调用。


#include<iostream>
using namespace std;
class Baseclass
{
public:
  Baseclass()                //无参构造函数;
  {
  cout << "default constructing Banseclass" << endl;
  }
  Baseclass(int i)               //有参构造函数;
  {
  a = i;
  cout << "constructing Baseclass a=" << a << endl;
  }
private:
  int a;
};
class Derivedclass :public Baseclass 
{
public:
  Derivedclass(int i);
  Derivedclass(int i, int j);
private:
  int b;
};
Derivedclass::Derivedclass(int i)    //调用基类默认构造函数;
{
  b = i;
  cout << "constructing Derivedclass b=" << b << endl;
}
Derivedclass::Derivedclass(int i, int j) :Baseclass(i)  //调用基类有参构造函数;
{
  b = j;
  cout << "constructing Derivedclass b=" << b << endl;
}
int main()
{
  Derivedclass x1(5, 6);
  Derivedclass x2(7);
  return 0;
}



我们可以看到,在基类有构造函数的时候,派生类定义对象的时候,基类构造函数也会被调用,他们的调用顺序就是我们前面讲的派生类与基类的调用关系,先调用基类构造函数。


派生类和基类都有构造函数,但是基类没有默认构造函数

若基类和派生类都有构造函数,但是基类没有默认构造函数的时候,派生类的每一个构造函数必须在其成员初始化列表中显示要被调用的基类构造函数,否则,基类构造函数不能准确获得执行机会;


#include<iostream>
using namespace std;
class Baseclass
{
public:
  Baseclass(int i)               //有参构造函数;
  {
  a = i;
  cout << "constructing Baseclass a=" << a << endl;
  }
private:
  int a;
};
class Derivedclass :public Baseclass 
{
public:
  Derivedclass(int i);
  Derivedclass(int i, int j);
private:
  int b;
};
Derivedclass::Derivedclass(int i)    //不能通过,没有显示调用基类构造函数;
{
  b = i;
  cout << "constructing Derivedclass b=" << b << endl;
}
Derivedclass::Derivedclass(int i, int j) :Baseclass(i)  //调用基类有参构造函数;
{
  b = j;
  cout << "constructing Derivedclass b=" << b << endl;
}
int main()
{
  Derivedclass x1(5, 6);
  Derivedclass x2(7);
  return 0;
}



讲完了构造函数,下面我们来讲一下析构函数;


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

由于析构函数也不能被继承,硬刺在执行派生类的析构函数的时候,也要调用其基类的析构函数。其执行顺序如下:

(1).先调用派生类的析构函数;

(2).再调用派生类中子对象的析构函数,(如果派生类中有子对象);

(3).再调用基类中的析构函数;

(4).最后是虚基类的析构函数;

我们会发现,析构函数的调用顺序刚好和构造函数的调用顺序相反。这也就验证了我们前面的类容,析构函数的调用是与构造函数的顺序相反的。


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


449fc9d0e5145e638367c8630faaf8b0_ef4326a8350e4a338b97d165e28b7ec8.png


其中有一个地方,base3的析构函数调用顺序在Base2之前,原因是虽然先定义的Base2,但是析构函数的调用顺序是相反的,其实我们可以借助其它的概念理解,建房子的时候从地基开始,但是,修房子的时候,从房顶开始拆。

说了这么多,可能还是有很多人不知道如何去使用,没有分清,下面我们借助一个应用实例来分析:


#include<iostream>
using namespace std;
class Point
{
public:
  Point(int myx, int myy) { x = myx, y = myy; }
  void displayxy()
  {
  cout << "The position of center" << endl;
  cout << "(" << x << "," << y << ")" << endl;
  }
protected:
  int x, y;            //不能定义为私有成员,最好为保护成员;
};
class Circle :public Point        //定义派生类,公有继承;
{
public:
  Circle(int myx, int myy, int myr) :Point(myx, myy)         //构造函数;
  {
  r = myr;
  }
  void displayr()
  {
  cout << "The radius of circle:" << r << endl;
  }
private:
  int r;
};
class Cylinder :public Circle            //定义派生类,公有继承;
{
public:
  Cylinder(int myx, int myy, int myr, int myh) :Circle(myx, myy, myr)
  {
  h = myh;
  }
  void displayh()
  {
  cout << "The height of cylinder:" << h << endl;
  }
private:
  int h;
};
//测试函数;
int main()
{
  Cylinder v(4, 5, 6, 7);//派生类对象;
  cout << "The data of cylinder" << endl;
  v.displayxy();
  v.displayr();
  v.displayh();
  return 0;
}



私有继承


私有继承的时候,基类中的公有成员不能被外模块使用,需要在派生类中增加新的接口;

例如:


#include<iostream>
using namespace std;
class Point
{
public:
  Point(int myx, int myy) { x = myx, y = myy; }
  void displayxy()
  {
  cout << "The position of center" << endl;
  cout << "(" << x << "," << y << ")" << endl;
  }
protected:
  int x, y;            //不能定义为私有成员,最好为保护成员;
};
class Circle :private Point        //定义派生类,公有继承;
{
public:
  Circle(int myx, int myy, int myr) :Point(myx, myy)         //构造函数;
  {
  r = myr;
  }
  void displayxy()
  {
  Point::displayxy();
  }
  void displayr()
  {
  cout << "The radius of circle:" << r << endl;
  }
private:
  int r;
};
class Cylinder :public Circle            //定义派生类,公有继承;
{
public:
  Cylinder(int myx, int myy, int myr, int myh) :Circle(myx, myy, myr)
  {
  h = myh;
  }
  void displayh()
  {
  cout << "The height of cylinder:" << h << endl;
  }
private:
  int h;
};
//测试函数;
int main()
{
  Cylinder v(4, 5, 6, 7);//派生类对象;
  cout << "The data of cylinder" << endl;
  v.displayxy();
  v.displayr();
  v.displayh();
  return 0;
}



上面的代码,把Point定义为私有继承,所以下面我们对程序也进行了增加接口的处理,私有继承,其实可以理解为,把基类Point作为派生类中的私有成员;

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