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 from only one class. 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 directly accessed in the derived class, while protected memberscan be directly accessed. For example, Classes B, C and D all contain the variables x, y and z in below example. It is just question of access.
In this example StaffAcc is a base class name while Programmer is the derived class. Since mode of inheritance is public so public member StaffSal of base class will also be public member of derived class along with Programmer class. Therefore object Prog1 of Programmer class can access both public member in Programmer class in main function as below.
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...
Superbly explain
ReplyDeleteWell explained ma'am
ReplyDeleteClearly explained every thing...It's very effective for me thank you for this blog mam��......
ReplyDeleteThanku so much ma'am .we really need this
ReplyDelete