Explain c++ inheritance in detail

  • 2020-10-23 21:07:41
  • OfStack

One of the most important concepts in object-oriented programming is inheritance. Inheritance allows us to define one class against another, which makes it easier to create and maintain one application. By doing so, it also achieves the effect of reusing code functions and improving execution efficiency.

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

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

The base class & Derived classes

A class can derive from multiple classes, which means that it can inherit data and functions from multiple base classes. Defining a derived class, we use a class derived list to specify the base class. The list of class derivations is named after one or more base classes, as follows:


class derived-class: access-specifier base-class

Where the access modifier ES15en-ES16en is one of public, protected, or private, and ES20en-ES21en is the name of a class that was previously defined. If the access modifier ES22en-ES23en is not used, the default is private.

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


#include <iostream>
 
using namespace std;
 
//  The base class 
class Shape 
{
  public:
   void setWidth(int w)
   {
     width = w;
   }
   void setHeight(int h)
   {
     height = h;
   }
  protected:
   int width;
   int height;
};
 
//  Derived classes 
class Rectangle: public Shape
{
  public:
   int getArea()
   { 
     return (width * height); 
   }
};
 
int main(void)
{
  Rectangle Rect;
 
  Rect.setWidth(5);
  Rect.setHeight(7);
 
  //  The area of the output object 
  cout << "Total area: " << Rect.getArea() << endl;
 
  return 0;
}

When the above code is compiled and 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, a base class member that does not want to be accessed by a member function of a derived class should be declared as private in the base class.

We can summarize the different access types according to the access permissions, as follows:

访问 public protected private
同1个类 yes yes yes
派生类 yes yes no
外部的类 yes no no

1 derived class inherits all of the base class methods except:

Base class constructors, destructors, and copy constructors. The overloaded operator of the base class. A friend function of a base class.

Derived types

When a class is derived from a base class, the base class can be inherited as public, protected, or private. The inheritance type is specified by the access modifier access-ES57en explained above.

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

Public inheritance (public) : When a class is derived from a public base class, the public member of the base class is also the public member of the derived class, and the protected member of the base class is also the protected member of the derived class. The private member 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 and protected members of the base class become the protected members of the derived class. Private inheritance (private) : When a class derives a selfish base class, the public and protected members of the base class become the private members of the derived class.

Multiple inheritance

Multiple inheritance means that a subclass can have multiple superclasses, which inherit the properties of multiple superclasses.

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


class < Derived class name >:< Inheritance way 1>< The base class name 1>,< Inheritance way 2>< The base class name 2>, ... 
{
< Derived class body >
};

The access modifier is inherited by either public, protected, or private, and is used to decorate each base class separated by commas, as shown above. Now let's take a look at the following example:


#include <iostream>
 
using namespace std;
 
//  The base class  Shape
class Shape 
{
  public:
   void setWidth(int w)
   {
     width = w;
   }
   void setHeight(int h)
   {
     height = h;
   }
  protected:
   int width;
   int height;
};
 
//  The base class  PaintCost
class PaintCost 
{
  public:
   int getCost(int area)
   {
     return area * 70;
   }
};
 
//  Derived classes 
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();
  
  //  The area of the output object 
  cout << "Total area: " << Rect.getArea() << endl;
 
  //  Total output cost 
  cout << "Total paint cost: $" << Rect.getCost(area) << endl;
 
  return 0;
}

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

[

Total area: 35
Total paint cost: $2450

]

Those are the details of c++ inheritance. For more information on c++ inheritance, please follow the other articles on this site!


Related articles: