C++ inheritance


One of the most important concepts in object-oriented programming is inheritance. Inheritance allows us to define a class based on another class, which makes creating and maintaining an application easier. This also achieves the effect of reusing code functions and improving execution time.

When creating a class, you do not need to rewrite new data members and member functions. You only need to specify that the new class inherits members of an existing class. This existing class is called base class, and the new class is called derived class.

Inheritance represents the is a relationship. For example, mammals are animals, dogs are mammals, therefore, dogs are animals, and so on.

Base Class & Derived Class

A class can be derived from multiple classes, which means that it can inherit data and functions from multiple base classes. To define a derived class, we use a class derived list to specify the base class. The class derived list is named after one or more base classes and has the following form:

class derived-class: access-specifier base-class

where the access modifier access-specifier is public, protected or private where One, base-class is the name of a class previously defined. If the access-specifier is not used, the default is private.

Suppose there is a base class Shape, Rectangle is its derived class, as shown below:

#include <iostream>
 
using namespace std;

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

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

int main(void)
{
   Rectangle Rect;
 
   Rect.setWidth(5);
   Rect.setHeight(7);

   // 输出对象的面积
   cout << "Total area: " << Rect.getArea() << endl;

   return 0;
}

When the above code is compiled and When executed, it produces the following results:

Total area: 35

Access Control and Inheritance

Derived classes can access all non-private members of the base class. Therefore, if base class members do not want to be accessed by member functions of derived classes, they should be declared private in the base class.

We can summarize different access types based on access rights, as follows:

Accesspublicprotectedprivate
Same classyesyesyes
Derived classyesyesno
External classyesnono

A derived class inherits all base class methods, except in the following cases:

  • The constructor, destructor and copy constructor of the base class.

  • Overloaded operators of base classes.

  • Friend function of the base class.

Inheritance Type

When a class is derived from a base class, the base class can be inherited as public, protected or private Several types. Inherited types are specified through the access-specifier explained above.

We almost never use protected or private inheritance, and usually use public inheritance. When using different types of inheritance, follow the following rules:

  • Public inheritance (public): When a class is derived from public When it is a base class, the public members of the base class are also the public members of the derived class, and the protected members of the base class are also the protected members of the derived class. Members, the private members of the base class cannot be directly accessed by the derived class, but can be accessed by calling the public and protected members of the base class.

  • Protected inheritance (protected): When a class is derived from a protected base class, the public of the base class and protected members will become protected members of the derived class.

  • Private inheritance (private): When a class is derived from a private base class, the public of the base class And protected members will become private members of the derived class.

Multiple inheritance

Multiple inheritance means that a subclass can have multiple parent classes, and it inherits the characteristics of multiple parent classes.

C++ classes can inherit members from multiple classes. The syntax is as follows:

class <派生类名>:<继承方式1><基类名1>,<继承方式2><基类名2>,…
{
<派生类类体>
};

Among them, the access modifier inheritance method is public, protected or private One of them is used to modify each base class, separated by commas, as shown above. Now let's take a look at the following example:

#include <iostream>
 
using namespace std;

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

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

// 派生类
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();
   
   // 输出对象的面积
   cout << "Total area: " << Rect.getArea() << endl;

   // 输出总花费
   cout << "Total paint cost: $" << Rect.getCost(area) << endl;

   return 0;
}

When the above code is compiled and executed, it will produce the following results:

Total area: 35
Total paint cost: 50