Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes.

  The constructors of inherited classes are called in the same order in which they are inherited. For example, in the following program, B’s constructor is called before A’s constructor.

 1 #include<iostream>
2 using namespace std;
3
4 class A
5 {
6 public:
7 A()
8 {
9 cout << "A's constructor called" << endl;
10 }
11 };
12
13 class B
14 {
15 public:
16 B()
17 {
18 cout << "B's constructor called" << endl;
19 }
20 };
21
22 class C: public B, public A // Note the order
23 {
24 public:
25 C()
26 {
27 cout << "C's constructor called" << endl;
28 }
29 };
30
31 int main()
32 {
33 C c;
34 return 0;
35 }

  Output:

  B's constructor called
  A's constructor called
  C's constructor called
  

  The destructors are called in reverse order of constructors.

  The diamond problem
  The diamond problem occurs when two superclasses of a class have a common base class.

  For example, in the following diagram, the TA class gets two copies of all attributes of Person class, this causes ambiguities.

  For example, consider the following program.

 1 #include<iostream>
2 using namespace std;
3 class Person
4 {
5 // Data members of person
6 public:
7 Person(int x)
8 {
9 cout << "Person::Person(int ) called" << endl;
10 }
11 };
12
13 class Faculty : public Person
14 {
15 // data members of Faculty
16 public:
17 Faculty(int x):Person(x)
18 {
19 cout<<"Faculty::Faculty(int ) called"<< endl;
20 }
21 };
22
23 class Student : public Person
24 {
25 // data members of Student
26 public:
27 Student(int x):Person(x)
28 {
29 cout<<"Student::Student(int ) called"<< endl;
30 }
31 };
32
33 class TA : public Faculty, public Student
34 {
35 public:
36 TA(int x):Student(x), Faculty(x)
37 {
38 cout<<"TA::TA(int ) called"<< endl;
39 }
40 };
41
42 int main()
43 {
44 TA ta1(30);
45 }

  Output:

  Person::Person(int ) called
  Faculty::Faculty(int ) called
  Person::Person(int ) called
  Student::Student(int ) called
  TA::TA(int ) called
  

  In the above program, constructor of ‘Person’ is called two times. Destructor of ‘Person’ will also be called two times when object ‘ta1′ is destructed. So object 'ta1' has two copies of all members of ‘Person’, this causes ambiguities(歧义). The solution to this problem is ‘virtual’ keyword. We make the classes ‘Faculty’ and ‘Student’ as virtual base classes to avoid two copies of ‘Person’ in ‘TA’ class.

  For example, consider the following program.

 1 #include<iostream>
2 using namespace std;
3 class Person {
4 public:
5 Person(int x)
6 {
7 cout << "Person::Person(int ) called" << endl;
8 }
9 Person()
10 {
11 cout << "Person::Person() called" << endl;
12 }
13 };
14
15 class Faculty : virtual public Person
16 {
17 public:
18 Faculty(int x):Person(x)
19 {
20 cout<<"Faculty::Faculty(int ) called"<< endl;
21 }
22 };
23
24 class Student : virtual public Person
25 {
26 public:
27 Student(int x):Person(x)
28 {
29 cout<<"Student::Student(int ) called"<< endl;
30 }
31 };
32
33 class TA : public Faculty, public Student
34 {
35 public:
36 TA(int x):Student(x), Faculty(x)
37 {
38 cout<<"TA::TA(int ) called"<< endl;
39 }
40 };
41
42 int main()
43 {
44 TA ta1(30);
45 }

  Output:

  Person::Person() called
  Faculty::Faculty(int ) called
  Student::Student(int ) called
  TA::TA(int ) called
  

  In the above program, constructor of ‘Person’ is called once. One important thing to note in the above output is, the default constructor of ‘Person’ is called. When we use ‘virtual’ keyword, the default constructor of grandparent class is called by default even if the parent classes explicitly call parameterized constructor.

  How to call the parameterized constructor of the ‘Person’ class? The constructor has to be called in ‘TA’ class.

  For example, see the following program.

 1 #include<iostream>
2 using namespace std;
3 class Person {
4 public:
5 Person(int x)
6 {
7 cout << "Person::Person(int ) called" << endl;
8 }
9 Person()
10 {
11 cout << "Person::Person() called" << endl;
12 }
13 };
14
15 class Faculty : virtual public Person
16 {
17 public:
18 Faculty(int x):Person(x)
19 {
20 cout<<"Faculty::Faculty(int ) called"<< endl;
21 }
22 };
23
24 class Student : virtual public Person
25 {
26 public:
27 Student(int x):Person(x)
28 {
29 cout<<"Student::Student(int ) called"<< endl;
30 }
31 };
32
33 class TA : public Faculty, public Student
34 {
35 public:
36 TA(int x):Student(x), Faculty(x), Person(x) //the difference
37 {
38 cout<<"TA::TA(int ) called"<< endl;
39 }
40 };
41
42 int main()
43 {
44 TA ta1(30);
45 }

  Output:

  Person::Person(int ) called
  Faculty::Faculty(int ) called
  Student::Student(int ) called
  TA::TA(int ) called

  In general, it is not allowed to call the grandparent’s constructor directly, it has to be called through parent class. It is allowed only when ‘virtual’ keyword is used.

  

  As an exercise, predict the output of following programs.

  Question 1

 1 #include<iostream>
2 using namespace std;
3
4 class A
5 {
6 int x;
7 public:
8 void setX(int i)
9 {
10 x = i;
11 }
12 void print()
13 {
14 cout << x;
15 }
16 };
17
18 class B: public A
19 {
20 public:
21 B()
22 {
23 setX(10);
24 }
25 };
26
27 class C: public A
28 {
29 public:
30 C()
31 {
32 setX(20);
33 }
34 };
35
36 class D: public B, public C
37 {43 };
44
45 int main()
46 {
47 D d;
48 d.print();
49 return 0;
50 }

  编译错误: 'D::print' is ambiguous

  Question 2

 1 #include<iostream>
2 using namespace std;
3
4 class A
5 {
6 int x;
7 public:
8 void setX(int i)
9 {
10 x = i;
11 }
12 void print()
13 {
14 cout << x;
15 }
16 };
17
18 class B: virtual public A
19 {
20 public:
21 B()
22 {
23 setX(10);
24 }
25 };
26
27 class C: virtual public A
28 {
29 public:
30 C()
31 {
32 setX(20);
33 }
34 };
35
36 class D: public B, public C
37 {
38
39 };
40
41 int main()
42 {
43 D d;
44 d.print();
45 return 0;
46 }

  Output:  20

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
  

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  2013-11-26  20:11:35

Multiple Inheritance in C++的更多相关文章

  1. Multiple inheritance in Go

    原文:http://golangtutorials.blogspot.com/2011/06/multiple-inheritance-in-go.html --------------------- ...

  2. 面向对象程序设计-C++ Inheritance & Multiple inheritance & RTTI【第十三次上课笔记】

    Sadly, 这节课带过去的笔记本没电了 T^T 导致没有一行 Code, Sorry 笔记如下: Shape * p1; //使用指针创建对象的方法 p = new Circle (2.0); Sh ...

  3. 条款40:明智而审慎地使用多重继承(use multiple inheritance judiciously)

    NOTE: 1.多重继承比单一继承复杂.它可能导致新的歧义性,以及对virtual继承的需要. 2.virtual 继承会增加大小 速度 初始化(及赋值)复杂度等等成本.如果virtual base ...

  4. Memory Layout for Multiple and Virtual Inheritance

    Memory Layout for Multiple and Virtual Inheritance(By Edsko de Vries, January 2006)Warning. This art ...

  5. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  6. [置顶] c++类的继承(inheritance)

    在C++中,所谓"继承"就是在一个已存在的类的基础上建立一个新的类.已存在的类(例如"马")称为"基类(base class )"或&quo ...

  7. Classical Inheritance in JavaScript

    JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance in ...

  8. (转) Friendship and inheritance

    原地址: http://www.cplusplus.com/doc/tutorial/inheritance/ Friend functions In principle, private and p ...

  9. <Effective C++>读书摘要--Inheritance and Object-Oriented Design<二>

    <Item 36> Never redefine an inherited non-virtual function 1.如下代码通过不同指针调用同一个对象的同一个函数会产生不同的行为Th ...

随机推荐

  1. fiddler 手机+浏览器 抓包

    用fiddler对手机上的程序进行抓包   前提: 1.必须确保安装fiddler的电脑和手机在同一个wifi环境下 备注:如果电脑用的是台式机,可以安装一个随身wifi,来确保台式机和手机在同一wi ...

  2. 性能工具之代码级性能测试工具ContiPerf

    前言 做性能的同学一定遇到过这样的场景:应用级别的性能测试发现一个操作的响应时间很长,然后要花费很多时间去逐级排查,最后却发现罪魁祸首是代码中某个实现低效的底层算法.这种自上而下的逐级排查定位的方法, ...

  3. Qt 使用大神插件快速创建树状导航栏

    前言 本博客仅仅记录自己的采坑过程以及帮助网友避坑,方便以后快速使用自定义控件,避免重复出错. 下载插件 大神 Github Qt 自定义控件项目地址:https://github.com/feiya ...

  4. 合并代码操作 | git fetch 与 git pull

    前言 首先我们要说简单说git的运行机制.git分为本地仓库和远程仓库,我们一般情况都是写完代码,commit到本地仓库(生成本地仓的commit ID,代表当前提交代码的版本号),然后push到远程 ...

  5. ES6基础知识(Map用法)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Java JDBC 理论笔记

    一.JDBC常用接口.类介绍 JDBC提供对独立于数据库统一的API,用以执行SQL命令.API常用的类.接口如下: DriverManager 管理JDBC驱动的服务类,主要通过它获取Connect ...

  7. [gym102511K]Traffic Blights

    为了方便,对于集合$S$,称$k\equiv S(mod\ M)$当且仅当存在$x\in S$使得$k\equiv x(mod\ M)$ 枚举红绿灯,对每一个点即限制$k$对$g_{i}+r_{i}$ ...

  8. [atARC070E]NarrowRectangles

    记$len_{i}=r_{i}-l_{i}$,即第$i$个区间的长度 用$f_{i,j}$表示前$i$个区间合法,第$i$个区间位于$[j,j+len_{i}]$的最小代价,暴力dp的时间复杂度为$o ...

  9. [loj2478]林克卡特树

    原题等价于选择恰好$k+1$条不相交(无公共点)的路径使得边权和最大 证明:对于原题中的最优解,一定包含了k条0边权的边(否则可以将未使用的边删掉,然后将这条路径的末尾与不在同一个连通块内的点连边), ...

  10. nginx安装与配置3-反向代理两台

    1.nginx 反向代理 两台tomcat 2.8080.8081 启动tomcat 记住每个tomcat都有两个端口不要出现tomcat端口占用情况 3.启动项目访问,不报错可以访问 4.在每个to ...