English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ inheritance access control (public, protected, private)

In this article, you will learn about C ++using public inheritance, protected inheritance, and private inheritance. You will learn through examples where and how to use it.

You can derive a derived class from a base class with different access specificationsclass, that is, publicInheritance, protected inheritance, or private inheritance.

#include <iostream>
using namespace std;
class base
{
.... ... ....
};
class derived : access_specifier base
{
.... ... ....
};

Note:public, protected, or private keywords can be used instead of the access_specifier used in the above code.Access specifier)Term.

Access control and inheritance

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

We can summarize different access types based on access permissions, as shown below:

accessinheritance is usually usedprotectedprivate
the same classyesyesyes
derived classyesyesno
external classesyesnono

A derived class inherits all the methods of the base class, but there are exceptions as follows:

  • constructors, destructors, and copy constructors of the base class.

  • overloaded operators of the base class.

  • friend functions of the base class.

inheritance type

When a class derives from a base class, the base class can be inherited as public, protected or private several types. The inheritance type is determined by the access specifiers access-specifier to specify.

We almost never use protected or private specifier to specify. inheritance is usually used public

  • inheritance. When using different types of inheritance, follow the following rules:A class derives frompublicof the base class whenpublicpublic inheritance (public):publicmembers are alsoprotectedpublic inheritance (public):protectedmembers are alsoPrivatemembers, the base class'spublicandprotectedmembers can be accessed by calling the base class's

  • Protected inheritance (protected): A class derives fromprotectedof the base class whenpublicandprotectedmembers will becomeprotectedmembers.

  • Private inheritance (private):A class derives fromPrivateof the base class whenpublicandprotectedmembers will becomePrivatemembers.

Multiple inheritance

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

C++ A class can inherit members from multiple classes, the syntax is as follows:

class <derived class name>:<inheritance method1><base class name1>,<inheritance method2><base class name2>,...
{
<derived class class body>
};

The inheritance method of access specifiers is public, protected or private One of them is used to modify each base class, and the base classes are separated by commas, as shown above. Now let's take a look at 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();
   
   // Output Object's Area
   cout << "Total Area: " << Rect.getArea() << endl;
 
   // Output Total Cost
   cout << "Total 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 Cost: $2450