-->

Inheritance in oop


 Object-Oriented Programming: Inheritance


The second most important feature of the OOP. The existing class code is used for the new class. this is save time to write code from scratch. 

In Inheritance, the new class can access the existing class's data (member function and member). 

The new class is called the derived or child class. 

The existing class is called the base or parent class. 

Inheritance is used for embellishment and refinement of the code.

In Inheritance, we easily reused the code. 

For example, a software company may use and access previous classes written by another person or company. 

Difference between the is-a relationship and the has-a relationship. 

  • has a relationship means:  an object contains one or more objects of other classes as members.
  • Is-a relationship means inheritance.
Example of Derived Class and Base Class:


The given example consists of two classes: a based class (Counter) and a derived class (CountDn). 
In the derived class, there is class CountDn: public Count, =>  (:) This is a single colon used for inheritance, and the keyword public, means to access all the public and protected members in the derived class.  


Generalization: Generalization means creating a basic class that has members fitting for other classes derived from it. 
Specialization: Specialization is when a derived class becomes more specific or specialized compared to its base or parent class.  


Access Specifier 

One Class (used private and public)



Access specifier with inheritance






Derived Class Constructors

To call the base class constructor, you must define the constructor of the derived class. Otherwise, it won't work to call the base class constructor.


Overriding Member Functions

In a derived class you override the same member functions that are defined in the base class it is called overriding. 

There are two functions with the same name, in base and derived class. But the rule defined, that when that same member function means the same name on both classes, and you want to call the function, the function in the derived class will be executed. 



Levels of Inheritance
Classes can be derived from classes that are themselves subclass of another class. 

 class A { };
 class B : public A { };
 class C : public B { };


Multiple Inheritance
A class can be derived from multiple base classes in multiple inheritance. 

class A // 
base class A { }; class B  // base class B { };
 class C : public A, public B //   C is derived from A and B { };

Member Functions in Multiple Inheritance


Constructors in Multiple Inheritance




Ambiguity in Multiple Inheritance


In such a scenario, when a class is derived from two base classes that each have functions with the same name but the derived class does not have a function with that name, the function calls can be ambiguous. This ambiguity arises because the compiler doesn't know which version of the function to call.


The issue is addressed by employing the scope-resolution operator to indicate the class within which the function is defined. Consequently,
objC.A::show();
refers to the show() function within the A class, whereas
objC.B::show();