Multiple Inheritance in C++ - C++ 中的多重继承

Multiple Inheritance in C++ - C++ 中的多重继承

1. Multiple Inheritance in C++

Inheritance can be done in a number of ways.
继承可以通过多种方式完成。

The different types of inheritances which we have come across are:
我们遇到的不同类型的继承是:

1.1 Single Inheritance

在这里插入图片描述

In single inheritance, a class inherits another class.
在单继承中,一个类继承另一个类。

1.2 Multilevel Inheritance

在这里插入图片描述

In this type of inheritance, one class inherits from another class. This base class inherits from some other class.
在这种继承类型中,一个类从另一类继承。该基类继承自其他一些类。

1.3 Hierarchical Inheritance

在这里插入图片描述

In hierarchical inheritance, more than one class inherit from a base class.
在分层继承中,一个以上的类从基类继承。

1.4 Multiple Inheritance

在这里插入图片描述

In this chapter, we will be studying about multiple inheritance.
在本章中,我们将研究多重继承。

In multiple inheritance, a class can inherit from more than one classes. In simple words, a class can have more than one parent classes. This type of inheritance is not present in Java.
在多重继承中,一个类可以从多个类中继承。简单来说,一个类可以有多个父类。Java 中不存在这种继承。

Suppose we have to make two classes A and B as the parent classes of class C, then we have to define class C as follows.
假设我们必须将两个类 A 和 B 设为类 C 的父类,必须如下定义类 C。

class C: public A, public B
{
        // code
};

Let’s see an example of multiple inheritance.
让我们看一个多重继承的例子。

//============================================================================
// Name        : std::class
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

using namespace std;

class Area
{
public:
	int getArea(int l, int b)
	{
		return l * b;
	}
};

class Perimeter
{
public:
	int getPerimeter(int l, int b)
	{
		return 2 * (l + b);
	}
};

class Rectangle: public Area, public Perimeter
{
	int length;
	int breadth;

public:
	Rectangle()
	{
		length = 7;
		breadth = 4;
	}

	int area()
	{
		return Area::getArea(length, breadth);
	}

	int perimeter()
	{
		return Perimeter::getPerimeter(length, breadth);
	}
};

int main()
{
	Rectangle rt;

	cout << "Area : " << rt.area() << endl;
	cout << "Perimeter : " << rt.perimeter() << endl;

	return 0;
}

Output

Area : 28
Perimeter : 22

In this example, class Rectangle has two parent classes Area and Perimeter. Class Area has a function getArea(int l, int b) which returns area. Class Perimeter has a function getPerimeter(int l, int b) which returns the perimeter.
在此示例中,类 Rectangle 具有两个父类 Area and Perimeter。类别 Area 具有函数 getArea(int l, int b) 返回 area。类 Perimeter 具有函数 getPerimeter(int l, int b),该函数返回 perimeter

When we created the object rt of class Rectangle, its constructor got called and assigned the values 7 and 4 to its data members length and breadth respectively. Then we called the function area() of the class Rectangle which returned getArea(length, breadth) of the class Area, thus calling the function getArea(int l, int b) and assigning the values 7 and 4 to l and b respectively. This function returned the area of the rectangle of length 7 and breadth 4.
当我们创建类 Rectangle 的对象 rt 时,其构造函数被调用并分别为其数据成员 lengthbreadth 分配值 7 和 4。然后,我们调用了矩形类 Rectangle 的函数area(),该函数返回了 Area 类的 getArea(length, breadth),因此调用了函数 getArea(int l, int b) 并分配了值 7 和 4 分别对应于 l 和 b。此函数返回长度为 7 和宽度为 4 的矩形区域。

Similarly, we returned the perimeter of the rectangle by the class Perimeter.
同样,我们通过类 Perimeter 返回了矩形的周长。

perimeter [pəˈrɪmɪtə(r)]:n. 周长,周界,视野计
//============================================================================
// Name        : std::class
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

using namespace std;

class P1
{
public:
	P1()
	{
		cout << "Constructor of P1" << endl;
	}
};

class P2
{
public:
	P2()
	{
		cout << "Constructor of P2" << endl;
	}
};

class A: public P2, public P1
{
public:
	A()
	{
		cout << "Constructor of A" << endl;
	}
};

int main()
{
	A a;
	return 0;
}

Output

Constructor of P2
Constructor of P1
Constructor of A

Here, when we created the object a of class A, its constructor got called. As seen before, the compiler first calls the constructor of the parent class. Since class A has two parent classes P1 and P2, so the constructors of both these classes will be called before executing the body of the constructor of A. The order in which the constructors of the two parent classes are called depends on the following code.
在这里,当我们创建类 A 的对象 a 时,其构造函数被调用。如前所示,编译器首先调用父类的构造函数。由于类 A 具有两个父类 P1 and P2,因此将在执行 A的构造函数的主体之前调用这两个类的构造函数。调用两个父类的构造函数的顺序取决于以下代码。

class A : public P2, public P1

The order in which the constructors are called depends on the order in which their respective classes are inherited. Since we wrote public P2 before public P1, therefore the constructor of P2 will be called before that of P1.
调用构造函数的顺序取决于继承它们各自的类的顺序。由于我们在 public P1 之前编写了 public P2,因此 P2 的构造函数将在 P1 的构造函数之前调用。

Words may lie but actions will always tell the truth.
言语可能会撒谎,但行动总会说实话。

inheritance [ɪnˈherɪtəns]:n. 继承,遗传,遗产
class:类
derived class:继承类,派生类
subclass:子类
base class:基类
superclass:超类,父类
passion [ˈpæʃn]:n. 激情,热情,酷爱,盛怒

References

https://www.codesdope.com/cpp-multiple-inheritance/

发布了454 篇原创文章 · 获赞 1733 · 访问量 103万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104408322