Inheritance in C + +

Posted by the_ut_tick on Sat, 08 Jan 2022 11:01:51 +0100

Inheritance in C + +

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 it easier to create and maintain an application. This also achieves the effect of reusing code functions and improving execution efficiency.
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 the members 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, so dogs are animals, and so on.

Example:

// Base class
class Animal 
{
    // eat() function
    // sleep() function
};


//Derived class
class Dog : public Animal 
{
    // bark() function
};

Base & derived classes

A class can be derived from multiple classes, which means that a derived class 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 derivation list is named after one or more base classes as follows:

class derived-class: access-specifier base-class

access-specifier: The access modifier can be public,protected or private One of them
base-class : Base class, the name of a defined class. If the access modifier is not used, the default is private. 
derived-class: The name of the derived class

Suppose there is a base class Basic and Derive is its derived class, as shown below:

#include <iostream>
using namespace std;
// Base class
class Basic
{
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }
   protected:
      int width;
      int height;
};
// Derived class
class Derive : public Basic
{
   public:
      int getArea()
      { 
         return (width * height); 
      }
};
int main(void)
{
   Derive Rect;
   Rect.setWidth(5);
   Rect.setHeight(7);
   // Area of output object
   cout << "Area of object: " << Rect.getArea() << endl;
 
   return 0;
}

Output:

Area of object: 35

Access control and inheritance

Derived classes can access all non private members in the base class. Therefore, if the base class member does not want to be accessed by the member function of the derived class, it should be declared as private in the base class.
We can summarize different access types according to access permissions, as shown below:

visitpublicprotectedprivate
Same class (class itself)yesyesyes
Derived class (class derived from base class)yesyesno
External classes (different classes)yesnono

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

  • Constructors, destructors, and copy constructors for base classes.
  • Overloaded operator of base class.
  • Friend function of the base class.

Inheritance type

When a class derives from a base class, the base class can be inherited into three types: public, protected, private and. The inheritance type is defined by the access modifier in front of the base class
protected private inheritance is hardly applicable, and public inheritance is usually used. When using different inheritance, follow the following principles

  • Public inheritance: when a class derives 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: when a class derives from a protected base class, the public and protected members of the base class will become the protected members of the derived class.
  • Private inheritance: when a class derives from a base class, the public and protected members of the base class will become private members of the derived class.

Multiple inheritance

Multiple inheritance means that a subclass can have multiple parent classes, which inherits the characteristics of multiple parent classes.
C + + classes can inherit members from multiple classes. The syntax is as follows:

class <Derived class name>:<Inheritance mode 1><Base class name 1>,<Inheritance mode 2><Base class name 2>,...
{
<Derived class body>
};

The access modifier inheritance method is one of public, protected or private. It is used to modify each base class. Each base class is separated by commas, as shown above.

#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;//create object
   int area;
 
   Rect.setWidth(5);
   Rect.setHeight(7);
 
   area = Rect.getArea();
   // Area of output object
   cout << "Area of object: " << Rect.getArea() << endl;
   // Total output cost
   cout << "Total cost: $" << Rect.getCost(area) << endl;
   return 0;
}

Output:

Area of object: 35
 Total cost: $2450

Topics: C++