0% found this document useful (0 votes)
21 views56 pages

Unit 4

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)
21 views56 pages

Unit 4

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/ 56

Unit-4

Polymorphism, Inheritance and Virtual


Functions
Polymorphism
• The word polymorphism means having many forms. Polymorphism is a feature of OOP that allows the object
to behave differently in different conditions
• In C++ polymorphism is mainly divided into two types:
1) Compile time Polymorphism – This is also known as static (or early) binding
2) Runtime Polymorphism – This is also known as dynamic (or late) binding
Compile Time Polymorphism
• This type of polymorphism is achieved by function overloading or operator overloading.

• Function Overloading:

When there are multiple functions with same name but different number of arguments.

Based on how many parameters are passed during function call determines which
function is to be called, this is why it is considered as an example of polymorphism
because in different conditions the output is different.

Since, the call is determined during compile time that is why it is called compile time
polymorphism.
Operator Overloading
• Operator overloading is the technique using which a different meaning is given to the existing operators in
C++.
• Most of the operators in C++ are overloaded or given special meaning so that they can work on user-
defined data types. Note that while overloading, the basic operation of operators is not altered.
Overloading just gives the operator an additional meaning by keeping their basic semantic same.
• Though most of the operators can be overloaded in C++, there are some operators which cannot be
overloaded:
• Scope resolution operator(::)
• Sizeof
• member selector(.)
• member pointer selector(*)
• ternary operator(?:)
Defining Operator Overloading
Overloading Unary Operators (using member function)
++ increment the value of basic data type but if we overload this, it can increment each data member of an object.
Overloading Unary Operators (using friend function)
Rules for overloading operators
• Only existing operators can be overloaded
• The basic meaning of operator can’t be changed
• Overloaded operators must follow the syntax of original operator. For example: for binary operator
overloading: operand1 op operand 2 (a+b)
• Unary operator overloaded through a member function takes no explicit arguments and return no explicit
values, but those overloaded through a friend function takes one explicit argument, take one reference
argument (object of the relevant class)
• Binary arithmetic operators (+,-,*,/) must explicitly return a value
• Binary operators overloaded through member function take one explicit argument and those overloaded
through a friend function takes two explicit arguments
• When using binary operators overloaded through a member function, the left-hand operand must be an
object of the relevant class
• There are some operators which can not be overloaded (::,.,?:,*)
Overloading Binary operator using member function
Overloading Binary operator
using friend function
• Friend function can also be used in place of member functions for
overloading a binary operator, the only difference being that a friend
function requires two arguments to be explicitly passed to it, while a
member function requires only one.
• In the complex number program discussed in the previous section,
the statement:
t3=t1+t2;
is equivalent to:
t3= operator+(t1,t2);
Overloading Assignment operator
• The assignment operator must be overloaded as a member function
Overloading Relational Operator (<)
In stream/ out stream operator
overloading
• We can input and output the built-in data types using << and >>
operators. The stream insertion and stream extraction operators can
be overloaded to perform input and output for objects also.
• In this case, it is important to make operator overloading function a
friend of the class as it would be called without creating an object and
two parameters compulsorily needs to be passed to it.
In stream/ out stream operator overloading
Run Time Polymorphism
• Consider a situation where function name and prototype is same in both base class and derived classes. For example, consider the following
class definitions:
Pointers to objects
• A pointer can point to an object created by a class. Consider the following statement:
item x;
• Similarly, we can define a pointer ptr of type item as follows:
item *ptr;
• Object pointers are useful in creating objects at run time. We can also use an object pointer to access public members of
an object. Consider a class item defined as follows:
Pointers to objects
Abstract Class and Pure Virtual Function
Pointers to Derived classes
Virtual Functions
Real world example of virtual
function

Function display() can be used in all the classes to display the class contents
Declare display() as virtual in base class: media
Rules for virtual functions
• When virtual functions are created for implementing late binding, following rules
must be satisfied:
Pure Virtual Functions
Real-Life Example to Understand the Implementation
of Virtual Function

• Consider employee management software for an organization.


Let the code has a simple base class Employee, the class contains virtual
functions like raiseSalary(), transfer(), promote(), etc. Different types of
employees like Managers, Engineers, etc., may have their own
implementations of the virtual functions present in base class Employee.
• In our complete software, we just need to pass a list of employees
everywhere and call appropriate functions without even knowing the type of
employee. For example, we can easily raise the salary of all employees by
iterating through the list of employees. Every type of employee may have its
own logic in its class, but we don’t need to worry about them because
if raiseSalary() is present for a specific employee type, only that function
would be called.
Virtual Destructor
• Deleting a derived class object using a pointer of base class type that has a non-virtual destructor results in undefined behavior. To
correct this situation, the base class should be defined with a virtual destructor. For example, following program results in undefined
behavior.
Virtual Destructor
Making base class destructor virtual guarantees that the object of derived class is destructed properly, i.e., both
base class and derived class destructors are called. For example,
Inheritance
• Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain
an application. This also provides an opportunity to reuse the code functionality and fast implementation time.
• When creating a class, instead of writing completely new data members and member functions, the
programmer can designate that the new class should inherit the members of an existing class. This existing
class is called the base class, and the new class is referred to as the derived class.
Base and Derived Classes
• A class can be derived from more than one classes, which means it can inherit data and functions from
multiple base classes. To define a derived class, we use a class derivation list to specify the base class(es). A
derived class can be defined by specifying its relationship with base class in addition to its own details. The
general form of defining a derived class is:
class derived-class: access-specifier base-class
where access-specifier is one of public, protected, or private, and base-class is the name of a previously
defined class. If the access-specifier is not used, then it is private by default.
Forms of Inheritance
Access Control and Inheritance
Modes of Inheritance
• When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance.
The type of inheritance is specified by the access-specifier as explained below.
• Public Inheritance − When deriving a class from a public base class, public members of the base class become public
members of the derived class and protected members of the base class become protected members of the derived class. A
base class's private members are never accessible directly from a derived class, but can be accessed through calls to the
public and protected members of the base class.
• Protected Inheritance − When deriving from a protected base class, public and protected members of the base class
become protected members of the derived class.
• Private Inheritance − When deriving from a private base class, public and protected members of the base class become
private members of the derived class.
Single Inheritance
Let us consider the case of private derivation
Multilevel Inheritance
2. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a
class can inherit from more than one classes. i.e one sub class is inherited
from more than one base classes.

Syntax:
class subclass_name : access_mode base_class1, access_mode
base_class2, ....
{
//body of subclass
};
3. Multilevel Inheritance: In this type of inheritance, a derived class is
created from another derived class.
4. Hierarchical Inheritance: In this type of inheritance, more than one sub
class is inherited from a single base class. i.e. more than one derived class
is created from a single base class.
5. Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by
combining more than one type of inheritance. For example: Combining
Hierarchical inheritance and Multiple Inheritance.
Below image shows the combination of hierarchical and multiple inheritance:

You might also like