#Inheritance in C ++ :
- inheritance is nothing but accessing one class properties(data members and member functions) into another class.
- Inheritance is the process in which one object acquires all the properties(data members) and behaviors(member functions) of its parent object automatically.
- child/derived/sub class
- parent/base/super class :
- The class which inherits the members/properties of another class is called as child/derived/sub class, and the class whose members/properties are inherited is called as base/parent/super class.
Advantages: Code Re usability
class SuperClass{
//body
}
Derived Class:
A derived class is defined as the class derived from the base class.
class DerivedClass::visibility_mode SuperClass{
// body of child/derived class
}
DerivedClassname :
visibility_mode : access spefiers
Note:
- When the base class is privately inherited by the derived class, public members of the base class becomes private members of the derived class. therefore the public memebers of the base class are not accesible by the objects of derived class, so derived/sub class objects by can access the members of its own class only.
- When the base class is publicly inherited by the derived/child/sub class, public members of the base class also becomes public members of the child/derived class. therefore public members of the base class are accessible by the objects of the derived/sub class as well as by the members functions of the base class also.
- Note : default mode of visiblity is private.
- private members of the base class never be inherited.
Types of ineritance:
--------------------
Single inheritance
Multiple inheritance
hierarchical
multilevel inheritance
hybrid inheritance
1)Single inheritance:
--------------------
Single inheritance in which a derived class is inherited form only one base/parent class
5 сен 2024