NAME: R.PRASHANTHAN
CLASS: 1ST YEAR B.Ed. ‘B’
ROLL.NO: 54
SUBJECT: COMPUTER SCIENCE
TOPIC: INHERITANCE
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
C++ and inheritance
Base Class (or superclass): the class being
inherited from
Derived Class (or subclass): the class that
inherits
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
Need for Inheritance
Capability of inheriting features.
Transitive relationship.
Reusability of class.
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.
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 ##
Raj
Father
Rani Roshan
Base Class
Derived Class
Existing
class is
called Base
Class.
New class is called
derived class.
INHERITANCE
SINGLE INHERITANCE
MULTIPLE INHERITANCE
MULTILEVEL
INHERITANCE
HIERARCHICAL
INHERITANCE
HYBRID INHERITANCE
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)
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
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( );
}
When many subclasses inherit from a single base class ,it
is known as hierarchical inheritance.
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
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.
When one subclass inheriting from many base class ,it is
known as hierarchical inheritance. Syntax:
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
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..
Virtual Inheritance
The derived class AutoBoat…
Inherits Attributes and Properties
From
Automobile
MotorBoat
Vehicle
Public, Private, and Protected Inheritance
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
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
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
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.
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
Inheritance

Inheritance

  • 1.
    NAME: R.PRASHANTHAN CLASS: 1STYEAR B.Ed. ‘B’ ROLL.NO: 54 SUBJECT: COMPUTER SCIENCE TOPIC: INHERITANCE
  • 2.
    INHERITANCE  The mechanism ofderiving 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 BaseClass (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
  • 5.
    Need for Inheritance Capabilityof inheriting features. Transitive relationship. Reusability of class.
  • 6.
    Advantages of inheritance Whena 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 ##
  • 8.
    Raj Father Rani Roshan Base Class DerivedClass Existing class is called Base Class. New class is called derived class.
  • 9.
  • 10.
    When a subclassinherit 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 subclassesinherit 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 Class2 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 subclassinheriting 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 subclassinherits 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..
  • 19.
    Virtual Inheritance The derivedclass AutoBoat… Inherits Attributes and Properties From Automobile MotorBoat Vehicle
  • 20.
    Public, Private, andProtected Inheritance
  • 21.
    Visibility Mode  PrivateWhen 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  PublicWhen 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  ProtectedWhen 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 ofvisibility 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 baseclass 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