Inheritance 

What is the purpose of Inheritance?

Purpose of inheritance is that it increases code reusability of program.

Rule of Inheritance

Only public and protected  member can be inherited. Private member of Base class  never be inherited.

Base Class : It is the class which derived in derived class.

Derived Class : It is the class derived from Base class that means whatever public and protected member of base class can be inherited in derived class based on mode of inheritance. So, need not be declare in derived class again if those members are already declared in base class. 

Syntax:
class  derived_classname : access_mode base_classname
{
  //body of subclass
};

Here, access mode can be public, private or protected.



Modes of Inheritance
Public mode: Mode of inheritance public means public member of base class becomes public in derived class and protected member of base class becomes protected in derived class.  
Public member of base class will become public member in derived class
protected member of base class will become protected member in derived class 
Protected mode:   Mode of Inheritance protected means  both public member and protected members of the base class will become protected in derived class.
Both public and protected member of Base class will become protected in derived class
Private mode:  Mode of Inheritance Private means  both public member and protected members of the base class will become Private in derived class.
Both public and protected member of the Base class will become Private member in derived class
Types of Inheritance:
There are following types of Inheritance:
1) Single Inheritance:  Single inheritance allowed to inherit one level of inheritance. That means there will be only one base class and one derived class only as shown in below fig
Note : The private members in the base class cannot be inherit in the derived class, while protected members can be inherited but protected member act as privately in that class. 

Ex 1:
class  B1
public: 
    int m; 
protected: 
    int n; 
private: 
    int x; 
}; 
  
class D1 : public B1 
    // m is public 
    //  n is protected 
    // x is not accessible from B1 class  
}; 
  Ex 2:
class  B1
public: 
    int m; 
protected: 
    int n; 
private: 
    int x; 
}; 

class D2 : protected B1 
    // m is protected 
    //  n is protected 
    // x is not accessible from C 
}; 
  Ex3:
class  B1
public: 
    int m; 
protected: 
    int n; 
private: 
    int x; 
}; 

class D3: private B1    // 'private' is default for classes 
    // m  is private 
    // n  is private 
    // x is not accessible from B1 
}; 





Comments

  1. Thank you ma'am for awesome explanation of inheritance

    ReplyDelete
  2. Thanks mam...you are really great and ur explanation is too good. so, I easily understand and keep all it topic in mind...u use technical words which is much helpful for me to attempt good marks...in semester exam...thnx once again mam..

    ReplyDelete
  3. Thanks a lot ma'am for sharing us notes nd we really benified to got it

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Thanks you mam. This is very useful.

    ReplyDelete
  6. Well explained concept with video it's cleared all my concepts and doubts thank you for this blog😊

    ReplyDelete

Post a Comment