C++ Inheritance

Inheritance is one of the key features of Object-orientated programming in C++. It lets us create a new class (derived class) from a current magnificence (base magnificence).

The derived elegance inherits the functions from the base class and can have extra functions of its own. For instance, Here, the Dog magnificence is derived from the Animal’s magnificence. Since Dog is derived from Animals, members of Animals are accessible to dogs. Notice the use of the keyword public even as inheriting Dog from Animal. We can also use the key phrases non-public and protected instead of public. Later in this tutorial, We will learn about the variations between personal, public, and guarded use. Is-a datingBInheritance is an is-a courting. We use inheritance only if an is-a courting is present among the two lessons. Here are a few examples: An automobile is a car.
Orange is a fruit.
A surgeon is a physician.
A canine is an animal.

Example 1: Simple Example of C++ Inheritance

Here, dog1 (the item of derived magnificence Dog) can access participants of the bottom elegance Animal. It’s because Dog is inherited from Animals.

C++ protected Members

The get right of entry to modifier blanketed is particularly applicable in terms of C++ inheritance. Like private contributors, protected members are inaccessible outdoors of the magnificence. However, they can be accessed by derived training and friend training/capabilities. We need blanketed contributors if we need to cover the information of a category, however, nonetheless need those records to be inherited with the aid of its derived lessons. To study extra about included, seek advice from our C++ Access Modifiers tutorial.

Example 2: C++ included Members

Here, the variable kind is covered and is hence on hand from the derived magnificence Dog. We can see this as we’ve initialized the kind inside the Dog class with the use of the characteristic setType().On the other hand, the private variable coloration can’t be initialized in Dog. Also, the reason that the protected keyword hides records, we cannot get the right of entry to type at once from an item of Dog or Animal elegance.

Access Modes in C++ Inheritance

In our previous tutorials, we discovered approximately C++ gets admission to specifiers which include public, private, and guarded. So a long way, we have used the public keyword so that you can inherit a category from a previously-current base magnificence. However, we can also use the non-public and guarded key phrases to inherit lessons. For example, The diverse approaches we will derive training are referred to as get entry to modes. These rights of entry to modes have the following impact:public: If a derived class is declared in public mode, then the members of the bottom class are inherited using the derived class just as they may be.
Non-public: In this case, all the contributors of the base class come to be private in the derived magnificence.
Protected: The public members of the base elegance turn out to be protected members in the derived magnificence.
The personal participants of the bottom magnificence are always private within the derived magnificence. To analyze extra, go to our C++ public, private, blanketed inheritance educational.

Member Function Overriding in Inheritance

Suppose, base class and derived elegance have member features with the same name and arguments. If we create an object of the derived magnificence and try and get the right of entry to that member feature, the member characteristic within the derived magnificence is invoked in preference to the only inside the base elegance. The member feature of derived elegance overrides the member characteristic of base elegance. Learn more about Function overriding in C++.

Leave a Comment