0% found this document useful (0 votes)
17 views38 pages

PPT7 Polymorphism

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views38 pages

PPT7 Polymorphism

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

UTA 018: OOPs

Poly-morphism

1
Contents
• Polymorphism
• Function Overloading
• Default Function Arguments
• Ambiguity in Function Overloading
Polymorphism
• The word polymorphism means having many
forms. In simple words, we can define
polymorphism as the ability of a message to
be displayed in more than one form.

• Real life example of polymorphism, a person at


a same time can have different characteristic.
Like a man at a same time is a father, a
husband, a employee.

• So a same person posses different behavior in


different situations. This is called
polymorphism.
Types of Polymorphism
• Compile Time Polymorphism
• Run Time Polymorphism
Compile Time Polymorphism
• Early binding refers to events that occur at compile time. In essence, early
binding occurs when all information needed to call a function is known at
compile time. (Put differently, early binding means that an object and a function
call are bound during compilation).

• Examples of early binding include normal function calls (including standard


library functions), overloaded function calls, and overloaded operators.

• The main advantage to early binding is efficiency. Because all necessary


information to call a function is determined at compile time, these types of
function calls are very fast.
Run Time Polymorphism
• The opposite of early binding is late binding.
• As it relates to C++, late binding refers to function calls that are not
resolved until run time.
• As function calls are not determined at compile time, the object and the
function are not linked until run time.
Run Time Polymorphism
• The main advantage to late binding is flexibility.
• Unlike early binding, late binding allows you to create programs that
can respond to events occurring while the program executes without
having to create a large amount of "contingency code."
• Keep in mind that because a function call is not resolved until run time,
late binding can make for somewhat slower execution times.
• Virtual functions are used to achieve late binding.
Function Overloading
• Function overloading is the process of using the same name for
two or more functions.
• The secret to overloading is that each redefinition of the function
must use either different types of parameters or a different number
of parameters.
• It is only through these differences that the compiler knows which
function to call in any given situation.
Example
#include <iostream> double myfunc(double i)
using namespace std;
{
int myfunc(int i); // these differ in types of parameters
return i;
double myfunc(double i);
int main()
}
{ int myfunc(int i)
cout << myfunc(10) << " "; // calls myfunc(int i) {
cout << myfunc(5.4); // calls myfunc(double i) return i;
return 0;
}
}

Output:
10 5.4
Example 2
#include <iostream> int myfunc(int i)
using namespace std; {
int myfunc(int i); // these differ in number of parameters return i;
int myfunc(int i, int j);
}
int main()
{
int myfunc(int i, int j)
cout << myfunc(10) << " "; // calls myfunc(int i) {
cout << myfunc(4, 5); // calls myfunc(int i, int j) return i*j;
return 0; }
}

Output:
10 20
Key Points
• The key point about function overloading is that the functions must differ in regard to the types
and/or number of parameters. Two functions differing only in their return types cannot be
overloaded.
• For example, this is an invalid attempt to overload myfunc( ):
int myfunc(int i);
float myfunc(int i);
// Error: differing return types are insufficient when overloading.
• Sometimes, two function declarations will appear to differ, when in
fact they do not. For example, consider the following declarations.
void f(int *p);
void f(int p[]); // error, *p is same as p[]
• Remember, to the compiler *p is the same as p[ ]. Therefore, although the two prototypes appear
to differ in the types of their parameter, in actuality they do not.
Example 3
Example 4 (Function overloading in classes)
Issue of Name Hiding
In C++, when you declare a function in the derived class that has the
same name as a function in the base class, all functions with that
name in the base class become hidden in the derived class, regardless
of their parameter types.
Inheritance based Polymorphism

Function Overriding through class Inheritance:

It is a language feature that allows a subclass overrides (replaces) the implementation


in the super super class by providing a method that has same name, same parameters
and same return type as the method in parent class.
Example
Draw()
Extend the Logic
Virtual Functions
• A virtual function (also known as virtual methods) is a member function
that is declared within a base class and is re-defined (overridden) by a
derived class. When you refer to a derived class object using a pointer or a
reference to the base class, you can call a virtual function for that object
and execute the derived class’s version of the method.
• They are mainly used to achieve Runtime polymorphism.
• Functions are declared with a virtual keyword in a base class.
• The resolving of a function call is done at runtime.
• Be aware that the virtual function mechanism works only with pointers to
objects and, with references, not with objects themselves.
Rules for virtual function
• Virtual functions cannot be static.
• Virtual functions should be accessed using a pointer or reference of
base class type to achieve runtime polymorphism.
• The prototype of virtual functions should be the same in the base as
well as the derived class.
• They are always defined in the base class and overridden in a derived
class. It is not mandatory for the derived class to override (or re-
define the virtual function), in that case, the base class version of the
function is used.
Inheritance and Static Functions

• They are inherited into the derived class.


• If you redefine a static member function in derived class, all the other
overloaded functions in base class are hidden.
• Static Member functions can never be virtual.

24
Abstract base class - interface
• It describes the capabilities of a C++ class without committing to a
particular implementation
• A class is made abstract by declaring at least one of its functions
as pure virtual function i.e. by placing "= 0" in its declaration
class Animal{
public:
virtual void sound() = 0;
void sleeping() {cout<<"Sleeping"; }
};
class Dog: public Animal{
public:
void sound() {cout<<"Woof"<<endl;}
};
int main(){
Dog obj;
obj.sound(); // Output: Woof
obj.sleeping(); //Output: Sleeping
}
class Animal{
public:
virtual void sound() = 0;
void sleeping() {cout<<"Sleeping"; }
};
class Dog: public Animal{
public:
void sound() {cout<<"Woof"<<endl;}
};
int main(){
Animal *obj = new Dog;
obj->sound();
}
Abstract Class
• An abstract class is designed to act as base class. It is a design concept
in program development and provides a base upon which other
classes may be built.
• Abstract Class is a class which contains atleast one Pure Virtual
function in it. Abstract classes are used to provide an Interface for its
sub classes. Classes inheriting an Abstract Class must provide
definition to the pure virtual function, otherwise they will also
become abstract class.

28
Characteristics of Abstract Class
• Abstract class cannot be instantiated, but pointers and references of
Abstract class type can be created.
• Abstract class can have normal functions and variables along with a
pure virtual function.
• Classes inheriting an Abstract Class must implement all pure virtual
functions, or else they will become Abstract too.

29
//Abstract base class int main()
class Base { {
public: //Base obj; (Compile Time Error)
virtual void show() = 0; }; Base *b;
Derived d;
class Derived:public Base { b = &d;
public: b->show();
void show() return 0;
{ }
cout << "Implementation of Virtual
Function in Derived class";
}
};

30
Note:
• Pure Virtual functions can be given a small definition in the Abstract
class, which you want all the derived classes to have. Still you cannot
create object of Abstract class.
• Also, the Pure Virtual function must be defined outside the class
definition. If you will define it inside the class definition, complier will
give an error. Inline pure virtual definition is Illegal.

31
Overloading vs overriding

• Function overloading – same function name but different


arguments. Functions are defined in the same class.
• Function overriding – same function name and arguments.
Defined in different classes.
Compile time and Run time Polymorphism
• Compile time OR static polymorphism is executed
using function overloading.

• Run time polymorphism or dynamic/late binding is


done using function overriding and virtual functions.
Compile Time Polymorphism Run-Time Polymorphism
At Compile time, which functions to be At Runtime, which function to be
called is decided. called is decided.

Also known as early or static binding Also known as late or dynamic binding

It executes faster because the function It executes slower because the function
is resolved at compilation time only. is resolved at Run-time.

It is achieved through function and It is achieved through function


operator overloading overriding and virtual functions
Ambiguity in Function Overloading
• You can create a situation in which the compiler is unable to
choose between two (or more) overloaded functions.
• When this happens, the situation is said to be
ambiguous.
Ambiguity in Function Overloading
int myfunc(double d);
// ...
cout << myfunc('c');
// not an error, conversion applied
• Although automatic type conversions are
convenient, they are also a prime cause of
ambiguity.
Example
#include <iostream>
using namespace std;
float myfunc(float i);
double myfunc(double i);

int main()
{
cout << myfunc(10.1) << " "; // unambiguous, calls myfunc(double)
cout << myfunc(10); // ambiguous return 0;
}
Thanks

You might also like