码迷,mamicode.com
首页 > 编程语言 > 详细

C++多重继承

时间:2020-02-09 12:04:23      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:vat   const   splay   ++   stream   创建   多重继承   void   code   

1.C++的派生类可继承多个基类.

2.多重继承语法:

  class D: public A, private B, protected C{
       //类D新增加的成员
       }

3.多重继承下的构造函数:

  D(形参列表): A(实参列表), B(实参列表), C(实参列表){
       //其他操作
  }

基类构造函数的调用顺序与其在派生类中出现的构造函数的调用顺序无关,只与在派生类创建时的顺序有关.

即:

  D(形参列表): B(实参列表), C(实参列表), A(实参列表){
     //其他操作
  }

也是先A后B再C.

举个例子:

  

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class A {
 6 public:
 7     A(int a,int b);
 8     ~A();
 9 protected:
10     int m_a, m_b;
11 };
12 A::A(int a,int b):m_a(a),m_b(b) { cout << "A construction" << endl; }
13 A::~A() { cout << "A destruction" << endl; }
14 
15 class B {
16 public:
17     B(int c,int d);
18     ~B();
19 protected:
20     int m_c, m_d;
21 };
22 B::B(int c,int d) :m_c(c),m_d(d) { cout << "B construction" << endl; }
23 B::~B() { cout << "B destruction" << endl; }
24 
25 /*class C : public B {
26 public:
27     C();
28     ~C();
29 };
30 C::C() { cout << "C construction" << endl; }
31 C::~C() { cout << "C destruction" << endl; }*/
32 
33 class D : public A, public B {
34 public:
35     D(int, int, int, int,int );
36     ~D() { cout << "D destruction" << endl; };
37     void display();
38 
39 private:
40     int m_e;
41 };
42 
43 D::D(int a,int b,int c,int d,int e):A(a,b),B(c,d),m_e(e){ cout << "D construction" << endl; }
44 
45 void D::display() {
46     cout << m_a << m_b << m_c << m_d << m_e << endl;
47 }
48 
49 int main()
50 {
51     std::cout << "Hello World!\n";
52     D obj(1,2,3,4,5);
53     obj.display();
54     return 0;
55 }

当多个基类有相同名字的成员变量时,会出现命名冲突,需要用域解析符::指定是哪个类的成员,消除二义性.

例子如下:

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class A {
 6 public:
 7     A(int a,int b);
 8     ~A();
 9         void show();
10 protected:
11     int m_a, m_b;
12 };
13 A::A(int a,int b):m_a(a),m_b(b) { cout << "A construction" << endl; }
14 A::~A() { cout << "A destruction" << endl; }
15 void A::show(){
16     cout<<m_a<<"   "<<m_b<<endl;
17 }
18 
19 class B {
20 public:
21     B(int c,int d);
22     ~B();
23         void show();
24 protected:
25     int m_c, m_d;
26 };
27 B::B(int c,int d) :m_c(c),m_d(d) { cout << "B construction" << endl; }
28 B::~B() { cout << "B destruction" << endl; }
29 void B::show(){
30         cout<<m_c<<"   "<<m_d<<endl;
31 }
32 
33 /*class C : public B {
34 public:
35     C();
36     ~C();
37 };
38 C::C() { cout << "C construction" << endl; }
39 C::~C() { cout << "C destruction" << endl; }*/
40 
41 class D : public A, public B {
42 public:
43     D(int, int, int, int,int );
44     ~D() { cout << "D destruction" << endl; };
45     void display();
46 
47 private:
48     int m_e;
49 };
50 
51 D::D(int a,int b,int c,int d,int e):A(a,b),B(c,d),m_e(e){ cout << "D construction" << endl; }
52 
53 void D::display() {
54     cout << m_a << m_b << m_c << m_d << m_e << endl;
55         A::show();    //调用A类的函数
56         B::show();    //调用B类的函数
57 }
58 
59 int main()
60 {
61     std::cout << "Hello World!\n";
62     D obj(1,2,3,4,5);
63     obj.display();
64     return 0;
65 }

 

转载自:http://c.biancheng.net/view/2277.html

C++多重继承

标签:vat   const   splay   ++   stream   创建   多重继承   void   code   

原文地址:https://www.cnblogs.com/ArrowToLanguage/p/12286474.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!