Ex8_3.cpp
上传用户:wuzhousb
上传日期:2022-07-12
资源大小:380k
文件大小:1k
源码类别:

书籍源码

开发平台:

Visual C++

  1. //【例8.3】在采用虚基类的多重继承中,构造与析构的次序。
  2. #include<iostream>
  3. using namespace std;
  4. class Object{
  5. public:
  6. Object(){cout<<"constructor Objectn";}
  7. ~Object(){cout<<"deconstructor Objectn";}
  8. };
  9. class Bclass1{
  10. public:
  11. Bclass1(){cout<<"constructor Bclass1n";}
  12. ~Bclass1(){cout<<"deconstructor Bclass1n";}
  13. };
  14. class Bclass2{
  15. public:
  16. Bclass2(){cout<<"constructor Bclass2n";}
  17. ~Bclass2(){cout<<"deconstructor Bclass2n";}
  18. };
  19. class Bclass3{
  20. public:
  21. Bclass3(){cout<<"constructor Bclass3n";}
  22. ~Bclass3(){cout<<"deconstructor Bclass3n";}
  23. };
  24. class Dclass:public Bclass1,virtual Bclass3,virtual Bclass2{
  25. Object object;
  26. public:
  27. Dclass():object(),Bclass2(),Bclass3(),Bclass1(){cout<<"派生类建立!n";}
  28. ~Dclass(){cout<<"派生类析构!n";}
  29. };
  30. int main(){
  31. Dclass dd;
  32. cout<<"主程序运行!n";
  33. return 0;
  34. }