0% found this document useful (0 votes)
27 views11 pages

INHeritance

Inheritance allows a derived class to inherit properties and characteristics from a base class. The derived class inherits all public and protected members of the base class but can add new features without modifying the base class. Inheritance is used when there is an "is-a" relationship between two classes, such as a car being a vehicle. The syntax for inheritance involves defining a derived class that inherits from a base class using the colon symbol.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views11 pages

INHeritance

Inheritance allows a derived class to inherit properties and characteristics from a base class. The derived class inherits all public and protected members of the base class but can add new features without modifying the base class. Inheritance is used when there is an "is-a" relationship between two classes, such as a car being a vehicle. The syntax for inheritance involves defining a derived class that inherits from a base class using the colon symbol.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

INHERITANCE

OOP
What is Inheritance?
 The capability of a class to derive properties and
characteristics from another class is called Inheritance
 It allows us to create a new class (derived class) from an
existing class (base class)
 The new class created is called “derived class” or “child
class” and the existing class is known as the “base class” or
“parent class”
 Sub Class: The class that inherits properties from another
class is called Subclass or Derived Class.
 Super Class: The class whose properties are inherited by a
subclass is called Base Class or Superclass.
What is Inheritance?
 The derived class inherits all
the properties of the base
class, without changing the
properties of base class
 It may add new features to its
own
 These new features in the
derived class will not affect
the base class
Why and when to use inheritance?
Why and when to use inheritance?
is-a relationship

 Inheritance is an is-a relationship


 We use inheritance only if an is-a relationship is
present between the two classes
 For example
• A car is a vehicle.
• Orange is a fruit.
• A surgeon is a doctor.
• A dog is an animal.
Inheritance (Syntax)
 Syntax:
class <derived_class_name> : <access-specifier> <base_class_name> { //body }
class — keyword to create a new class
derived_class_name — name of the new class, which will
inherit the base class
access-specifier — either of private, public or protected. If
neither is specified, PRIVATE is taken as default
base-class-name — name of the base class
Note: A derived class doesn’t inherit access to private data
members. However, it does inherit a full parent object, which
contains any private members which that class declares.
Modes of Inheritance
 Public Mode: If we derive a subclass from a public base class. Then
the public member of the base class will become public in the
derived class and protected members of the base class will become
protected in the derived class.
 Protected Mode: If we derive a subclass from a Protected base

class. Then both public members and protected members of the base
class will become protected in the derived class.
 Private Mode: If we derive a subclass from a Private base class.

Then both public members and protected members of the base class
will become Private in the derived class.
Note: The private members in the base class cannot be directly
accessed in the derived class, while protected members can be directly
accessed.
Modes of Inheritance
Modes of Inheritance
Example
 Example:
1. class ABC : private XYZ //private derivation
{ }
2. class ABC : public XYZ //public derivation
{ }
3. class ABC : protected XYZ //protected
derivation
{ }
4. class ABC: XYZ //private derivation
by default
{ }

You might also like