2. INHERITANCE
The mechanism
of deriving a new
class from an old
one is called
Inheritance.
The new class is
a specialized
version of the
existing class
3. C++ and inheritance
Base Class (or superclass): the class being
inherited from
Derived Class (or subclass): the class that
inherits
4. The "is a" Relationship
Inheritance establishes an "is a" relationship
between classes.
A poodle is a dog
A car is a vehicle
A flower is a plant
A football player is an athlete
6. Advantages of inheritance
When a class inherits from another class, there are
three benefits:
(1) You can reuse the methods and data of the
existing class.
(2) You can extend the existing class by adding
new data and new methods.
(3) You can modify the existing class by
overloading its methods with your own
implementations.
7. Benefits of Inheritance
Code is reused
grad_student uses tested code from student
Reflects relationship in problem domain
Special grouping grad_ student outgrowth of real
world and treatment of this group
Polymorphic mechanisms allow client code to treat
inherited class as subtype of base class
Simplifies code, maintains subtype distinctions ##
10. When a subclass inherit only from one base class,it is known as
single inheritance.
Super Class
Derived Class
Super Class
(Base Class)
Sub Class
(Derived Class)
11. Syntax:
Class Derived-class : Mode base-class
{
St1;
St2;
};
Colon gives that derived class-name is
derived from base-class.
Mode is either private or public.By
default mode is private
12. Base Class & Derived Class
The new class is called Derived Class and old one is called Base class.
Class Student
{
Public: int Roll;
};
Class Ipstudent : public Student
{
Private : int ipmarks;
Public : void info( )
{ cout<< Roll;
cout<<ipmarks;
}
};
Main( )
{ ipstudent ip;
ip.Roll=1;
Ip.info( );
}
13. When many subclasses inherit from a single base class ,it
is known as hierarchical inheritance.
14. Syntax:
Class Base-class : Mode Derived-class 1,Mode Derived-class 2
{
St1;
St2;
};
Mode is either private or public.By
default mode is private
Name of different Derived-class
15. Super Class
Sub Class 2
Sub Class 1
When a subclass inherits from a class that itself
inherits from another class it is known as
multilevel inheritance.
16. When one subclass inheriting from many base class ,it is
known as hierarchical inheritance. Syntax:
17. Syntax:
Class Derived-class : Mode base-class1,Mode base-class2
{
St1;
St2;
};
Mode is either private or public.By
default mode is private
Name of different base class
18. When a subclass inherits from multiple base classes and all of its base
classes inherit from a single base class,this form of inheritance is
known as hybrid inheritance..
21. Visibility Mode
Private When base class is privately accessed by
derived class then
Public member Private
Protected member Private
Private member Not inherited
Base Class Derived Class
become
22. Visibility Mode
Public When base class is publicly accessed by
derived class then
• Private member Not inherited
• Public member Public
• Protected member Protected
Base Class Derived Class
23. Visibility Mode
Protected When base class is accessed protectly
by derived class then
Private member Not inherited
Public member Public
Protected member Protected
Base Class Derived Class
24. The significance of visibility modes
Private :-When the derived class require to use
some attributes of the base class and these inherited
features are intended not to be inherited further .
Public :-When the situation wants the derived class
to have all the attributes of the base class,plus some
extra attributes.
Protected:- When the features are required to be
hidden from the outside world and at same time
required to be inheritable.
25. Accessibility of base class member
Access
Specifier
Accessible
from own class
Accessible from
derived
class(inheritable)
Accessible from
object outside
class
Public Yes Yes Yes
Protected Yes Yes No
Private Yes No No