0% found this document useful (0 votes)
12 views18 pages

CPP UNIT 3

The document discusses key concepts of inheritance and polymorphism in C++. It explains inheritance as a mechanism for reusing and extending classes, with types including single, multilevel, multiple, hierarchical, and hybrid inheritance. It also covers polymorphism, defining it as the ability for different classes to invoke different functions based on object type, with static and dynamic binding as its two main types.

Uploaded by

vasanthaxukai
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)
12 views18 pages

CPP UNIT 3

The document discusses key concepts of inheritance and polymorphism in C++. It explains inheritance as a mechanism for reusing and extending classes, with types including single, multilevel, multiple, hierarchical, and hybrid inheritance. It also covers polymorphism, defining it as the ability for different classes to invoke different functions based on object type, with static and dynamic binding as its two main types.

Uploaded by

vasanthaxukai
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/ 18

UNIT-3

Inheritance & Polymorphism


What is the Inheritance
 In C++, inheritance is a process in which one
object acquires all the properties and behaviors
of its parent object automatically. In such way,
you can reuse, extend or modify the attributes
and behaviors which are defined in other class
OR
 Inheritance is a mechanism of reusing and
extending existing classes without modifying
them, thus producing hierarchical relationships
between them i.e. Inheritance is almost like
embedding an object into a class.
Constructor:-
 Constructor is a class member
function with the same name as
the class. The main job of the
constructor is to allocate
memory for class objects.
Constructor is automatically
called when the object is
created.
Specialization:-
 specialization means creating new subclasses from an
existing class. If it turns out that certain attributes,
associations, or methods only apply to some of the objects
of the class, a subclass can be created. The most inclusive
class in a generalization/specialization is called the super
class and is generally located at the top of the diagram. The
more specific classes are called subclasses and are generally
placed below the super class.

 Specialization is the process of dividing a parent-level entity


into narrower categories accordingly to all the possible child
categories. By having the behavior of the opposite of the
generalization process, specialization requires the separation
of entities based on certain uncommon attributes
Generalization:-
 Generalization is the process of extracting shared
characteristics from two or more classes, and
combining them into a generalized super class.
Shared characteristics can be attributes, associations,
or methods.

 Generalization relationships are used in class,


component, deployment, and use-case diagrams to
indicate that the child receives all of the
attributes, operations, and relationships that are
defined in the parent
Types Of Inheritance:-

 There are Five types..,

 Single inheritance
 Multilevel inheritance
 Multiple inheritance
 Hierarchical inheritance
 Hybrid inheritance
Single inheritance:-

 Single inheritance is
defined as the inheritance in
which a derived class is
inherited from the only one
base class.

 Where 'A' is the base class,


and 'B' is the derived class.
Multilevel inheritance:-
 Multilevel inheritance is a process
of deriving a class from another
derived class.

When one class inherits another


class which is further inherited
by another class, it is known as
multi level inheritance in C++.
Inheritance is transitive so the
last derived class acquires all the
members of all its base classes.
Multiple inheritance:-
 A class can be derived from more than
one base class.
 Multiple inheritance is the process of
deriving a new class that inherits the
attributes from two or more classes.
Hierarchical inheritance:-
 Hierarchical inheritance is defined as the
process of deriving more than one class from
a base class.
 more than one subclass is inherited from a
single base class. i.e. more than one derived
class is created from a single base class.
Hybrid inheritance
 Combining various types of inheritance like multiple,
simple, and hierarchical inheritance is known as
hybrid inheritance.
 In simple inheritance, one class is derived from a
single class which is its base. In multiple inheritances,
a class is derived from two classes, where one of the
parents is also a derived class. In hierarchical
inheritance, more than one derived class is created
from a single base class.
Polymorphism:-
 The term "Polymorphism" is the combination of
"poly" + "morphs" which means many forms. It is a
greek word. In object-oriented programming, we
use 3 main concepts: inheritance, encapsulation,
and polymorphism
 Polymorphism means "many forms", and it occurs
when we have many classes that are related to each
other by inheritance.
 C++ polymorphism means that a call to a member
function will cause a different function to be
executed depending on the type of object that
invokes the function
Types of
Polymorphism
Static (OR) Compile time
polymorphism:-
 Compile time polymorphism: The
overloaded functions are invoked by
matching the type and number of arguments.
This information is available at the compile
time and, therefore, compiler selects the
appropriate function at the compile time. It
is achieved by function overloading and
operator overloading which is also known as
static binding
Dynamic binding (OR) Run time
polymorphism:-

 Run time polymorphism: Run time


polymorphism is achieved when the
object's method is invoked at the run
time instead of compile time. It is
achieved by method overriding which is
also known as dynamic binding or late
binding

You might also like