WEB DEVELOPER SITE
PYTHONPHP7JAVA
 

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 it easier to create and maintain an application. In doing so, it also achieves the effect of reusing code functions and improving execution efficiency.

When you create a class, you don't need to rewrite the new data members and member functions, just specify that the newly created class inherits the members of an existing class. This existing class is called base class, and the new class is called derive class.

Inheritance represents the is a relationship. For example, a mammal is an animal, a dog is a mammal, and therefore, a dog is an animal, and the like.

Base class & Derived class

A class can be derived from multiple classes, which means 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, in the form:

class derived-class: access-specifier base-class

When the access modifier access-specifier is one of public, protected or private, the base-class is the name of a previously defined class. If the access modifier access-specifier is not used, it defaults to private.

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

Instance

#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); // Output area 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 class can access all non-private members of 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 private in the Base class.

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

Visitpublicprotectedprivate
same classyesyesyes
Derived classyesyesno
External classyesnono

A Derived class inherits all Base class methods except in the following cases:

  • Base class constructor, destructor, and copy constructor.
  • The overloaded operator of the base class.
  • The friend function of the base class.

Inheritance type

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

We rarely use protected or private inheritance, usually using public inheritance. When using different types of inheritance, follow these rules:

  • Public inheritance (public): When a class is derived from a publicBase class, the public member of the base class is also a Derived class< b>public members, the protection members of the base class are also protected members of the Derived class, and the private members of the base class cannot be directly Derived class access, but can be accessed by calling the public and protect members of the Base class.
  • Protected: When a class is derived from the ProtectBase class, the public and protection of the Base class< The /b> member will be a protection member of the Derived class.
  • Private inheritance (private): When a class is derived from a privateBase class, the Base class public and protected< /b>Members will become private members of the Derived class.

Multiple inheritance

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

C++ classes can inherit members from multiple classes with the following syntax:

class <Derived >:<Inheritance1><Base Class name1>,<Inheritance2><Base Class name2>,…
{
<Derived Class body>
};

The access modifier inheritance method is one of public, protected or private, which is used to decorate each Base class, separated by commas. ,As shown above. Let us now take a look at the following example:

Instance

#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 area 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