Chinaunix首页 | 论坛 | 博客
  • 博客访问: 151316
  • 博文数量: 44
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 407
  • 用 户 组: 普通用户
  • 注册时间: 2015-11-10 13:28
个人简介

仰望星空

文章分类
文章存档

2016年(22)

2015年(22)

我的朋友

分类: C/C++

2016-03-10 09:22:34


点击(此处)折叠或打开

  1. /*
  2.  * 多继承的构造函数和析构函数
  3.  * 对于所有需要给予参数进行初始化的基类,都要显示给出基类名和参数表
  4.  */
  5.  
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. class Base1
  10. {
  11. private:
  12.    int b1;
  13. public:
  14.    Base1(){cout<<"Base1 缺省构造函数:"<<b1<<endl;}
  15.    Base1(int x):b1(x){cout<<"Base1 构造函数:"<<b1<<endl;}
  16.    ~Base1(){cout<<"Base1 析构函数:"<<b1<<endl;}
  17. };
  18.  
  19. class Base2
  20. {
  21. private:
  22.    int b2;
  23. public:
  24.    Base2(){cout<<"Base2 缺省构造函数:"<<b2<<endl;}
  25.    Base2(int x):b2(x){cout<<"Base2 构造函数:"<<b2<<endl;}
  26.    ~Base2(){cout<<"Base2 析构函数:"<<b2<<endl;}
  27. };
  28.  
  29. class Derive:public Base1, public Base2
  30. {
  31. private:
  32.    Base1 b1;
  33.    Base2 b2;
  34. public:
  35.    Derive(){cout<<"Derive 缺省构造函数:"<<endl;}
  36.    Derive(int x, int y,int i, int j):b1(x),b2(y),Base1(i),Base2(j)
  37.    {cout<<"Derive 构造函数:"<<endl;}
  38.    ~Derive(){cout<<"Derive 析构函数:"<<endl;}
  39. };
  40.  
  41. int main(void)
  42. {
  43.    Derive obj(1,2,3,4);
  44.    return 0;
  45. }

阅读(1093) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~