Cplusplus 简明教程

C++ Inheritance

面向对象编程中最重要的概念之一是继承。继承允许我们根据另一个类定义一个类,这使得创建和维护应用程序变得更容易。这也提供了重用代码功能和快速实现时间的机会。

One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.

在创建类时,编程人员可以指定新类应该继承现有类的成员,而不是编写全新的数据成员和成员函数。这个现有的类叫做 base 类,而新类被称为 derived 类。

When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.

继承的思想实现了 is a 关系。例如,哺乳动物 IS-A 动物,狗 IS-A 哺乳动物,因此狗 IS-A 动物,以此类推。

The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.

Base and Derived Classes

一个类可以从多个类派生,这意味着它可以从多个基类继承数据和函数。要定义派生类,我们使用类派生列表来指定基类。类派生列表命名一个或多个基类,形式为 -

A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form −

class derived-class: access-specifier base-class

其中访问说明符是 public, protected,private 之一,基类是先前定义类的名称。如果未使用访问说明符,那么它默认是私有的。

Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.

考虑基类 Shape 及其派生类 Rectangle ,如下所示 -

Consider a base class Shape and its derived class Rectangle as follows −

#include <iostream>

using namespace std;

// Base class
class Shape {
   public:
      void setWidth(int w) {
         width = w;
      }
      void setHeight(int h) {
         height = h;
      }

   protected:
      int width;
      int height;
};

// Derived class
class Rectangle: public Shape {
   public:
      int getArea() {
         return (width * height);
      }
};

int main(void) {
   Rectangle Rect;

   Rect.setWidth(5);
   Rect.setHeight(7);

   // Print the area of the object.
   cout << "Total area: " << Rect.getArea() << endl;

   return 0;
}

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

Total area: 35

Access Control and Inheritance

派生类可以访问其基类的所有非私有成员。因此对于不应该对派生类的成员函数可访问的基类成员,应当在基类中声明为私有。

A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

我们可以对不同访问类型按照下面方式进行总结——谁可以访问它们?

We can summarize the different access types according to - who can access them in the following way −

Access

public

protected

private

Same class

yes

yes

yes

Derived classes

yes

yes

no

Outside classes

yes

no

no

派生类继承所有基类方法,但有以下例外——

A derived class inherits all base class methods with the following exceptions −

  1. Constructors, destructors and copy constructors of the base class.

  2. Overloaded operators of the base class.

  3. The friend functions of the base class.

Type of Inheritance

派生类可继承从基类,这继承方式可以是 public, protected私有 继承。继承类型由访问说明符指定,如上所解释。

When deriving a class from a base class, the base class may be inherited through public, protected or * private* inheritance. The type of inheritance is specified by the access-specifier as explained above.

我们很少使用 protected私有 继承,但 public 继承比较常用。在使用不同类型的继承时,应用以下规则——

We hardly use protected or * private* inheritance, but public inheritance is commonly used. While using different type of inheritance, following rules are applied −

  1. Public Inheritance − When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class’s private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.

  2. Protected Inheritance − When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.

  3. Private Inheritance − When deriving from a private base class, public and protected members of the base class become private members of the derived class.

Multiple Inheritance

一个 C++ 类可以从多个类继承成员,其扩展语法如下 −

A C++ class can inherit members from more than one class and here is the extended syntax −

class derived-class: access baseA, access baseB....

其中访问之一是 public, protected,private ,并且将给予每个基类,并且它们将按逗号分隔,如上所示。让我们尝试以下示例 −

Where access is one of public, protected, or private and would be given for every base class and they will be separated by comma as shown above. Let us try the following example −

#include <iostream>

using namespace std;

// Base class Shape
class Shape {
   public:
      void setWidth(int w) {
         width = w;
      }
      void setHeight(int h) {
         height = h;
      }

   protected:
      int width;
      int height;
};

// Base class PaintCost
class PaintCost {
   public:
      int getCost(int area) {
         return area * 70;
      }
};

// Derived class
class Rectangle: public Shape, public PaintCost {
   public:
      int getArea() {
         return (width * height);
      }
};

int main(void) {
   Rectangle Rect;
   int area;

   Rect.setWidth(5);
   Rect.setHeight(7);

   area = Rect.getArea();

   // Print the area of the object.
   cout << "Total area: " << Rect.getArea() << endl;

   // Print the total cost of painting
   cout << "Total paint cost: $" << Rect.getCost(area) << endl;

   return 0;
}

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

Total area: 35
Total paint cost: $2450