Polymorphism
Polymorphism
Polymorphism is crucial feature of Object Oriented
Programming.
Polymorphism simply means one name having multiple forms.
Definition:
– Polymorphism is the ability to create a variable, a function or
an object that has more than one form.
– Polymorphism is the quality that allows one name to be used
for two or more related but technically different purposes
Types of Polymorphism
Polymorphism
Compile-time Run-time
Polymorphism Polymorphism
Function Operator Virtual
Overloading Overloading Function
Types of Polymorphism
• In compile time polymorphism, compiler is
able to select the appropriate function a
particular call at the compile time.
• In run time polymorphism, an appropriate
member function is selected while the program
is running.
Benefits of Polymorphism
Simplicity:
This makes your code easier for you to write and easier for others
to understand.
Extensibility:
Polymorphism design and implements system that are more
extensible.
Function Overriding
If we inherit a class into the derived class and provide a definition
for one of the base class's function again inside the derived class,
then that function is said to be overridden, and this mechanism is
called Function Overriding
Requirements for Overriding
• Inheritance should be there. Function overriding cannot be done
within a class. For this we require a derived class and a base
class.
• Function that is redefined must have exactly the same declaration
in both base and derived class, that means same name, same
return type and same parameter list.
Function Overriding Example
#include <iostream>
using namespace std; int main()
class Base {
{
Base b; //Base class object
public:
Derived d; //Derived class object
void show()
{
b.show();
cout << "Base class";
}
d.show();
}; return 0;
class Derived : public Base }
{
public:
void show()
{
cout << " Derived Class";
}
};
Virtual function
• Virtual Function is a function in base class, which is overridden in
the derived class, and which tells the compiler to perform Late
Binding on this function.
• Defining in a base class a virtual function, with another version in
a derived class, signals to the compiler that we don't want static
linkage for this function.
class Shape//Base class
{
Syntax is
public:
virtual double area_calc() { return 0;} Shape() {}
virtual double area_calc()
{ return 0; }
};
• Virtual Keyword is used to make a member function of the base
class Virtual.
Virtual function Example
#include <iostream> int main()
using namespace std; {
class Base
Base* b; //Base class pointer
{
public: Derived d; //Derived class
virtual void show() object
{ b = &d;
cout << "Base class"; b->show(); //Late Binding Occurs
}
}
};
class Derived:public Base
{
public:
void show()
{
cout << "Derived Class"<<endl;
} };
Static Binding & Dynamic Binding
Early Binding
• Connecting the function call to the function body is called
Binding. When it is done before the program is run, its called
Early Binding or Static Binding or Compile-time Binding.
Late Binding
• In Late Binding function call is resolved at runtime. Hence, now
compiler determines the type of object at runtime, and then binds
the function call. Late Binding is also called Dynamic Binding or
Runtime Binding.
Static Binding Example
#include <iostream>
using namespace std; int main()
class Base
{
{
public: Base b; //Base class object
void show() Derived d; //Derived class
{ object
cout << "Base class";
}
b.show();
};
class Derived : public Base
d.show(); //Early Binding
{ Ocuurs
public: return 0;
void show() }
{
cout << " Derived Class";
}
};
Dynamic Binding Example
#include <iostream>
using namespace std;
class Base
int main()
{
{
public:
virtual void show() Base* b; //Base class pointer
{ Derived d; //Derived class object
cout << "Base class"; b = &d;
} b->show(); //Late Binding Ocuurs
}; }
class Derived:public Base
{
public:
void show()
{
cout << "Derived Class"<<endl;
}
};
Static VS Dynamic Polymorphism
Statically bound methods are those methods that are bound to
their calls at compile time. Dynamic function calls are bound to the
functions during run-time.
Involves the additional step of searching the functions during
run-time. On the other hand, no run-time search is required for
statically bound functions.
Virtual keyword is used if the function needs to be dynamically
bounded.
#include <iostream> class C : public A{
using namespace std; public:
class A C() {}
{ };
public: int main() {
A() {}
void output() { cout << "Hi"<< endl; } A* ptr;
}; B b_object;
class B : public A C c_object;
{ ptr = &b_object;
public: ptr->output();
B() {} //=> Base class 'output' member function
void output() { ptr = &c_object;
cout << "Attempted re-write." << endl; } ptr->output();
}; return 0; }
Functions which are defined in classes can be defined using the virtual modifier. This means that the
behavior of the function can be overridden by derived classes, and thus when a base class pointer points
towards a derived object and uses a virtual member function on the object it's pointing to which the
derived class has over-written, the derived version will be used instead of the base one!
Virtual member function Example
#include <iostream> class C : public A
using namespace std; {
class A public:
{ C() {}
public: };
int main()
A() {}
{
virtual void output() { cout << "Hi"<< endl; }
A* ptr;
}; B b_object;
class B : public A C c_object;
{ ptr = &b_object;
public: ptr->output();
B() {} //=> Base class 'output' member function
void output() { ptr = &c_object;
ptr->output();
cout << "Attempted re-write." << endl; }
return 0;
};
}
Abstract Class
• Abstract Class is a class which contains at least 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.
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.
Example of Abstract Class
#include <iostream> class Triangle: public Polygon {
using namespace std; public:
int area (void)
class Polygon { { return (width * height / 2); }
protected: };
int width, height;
public: int main () {
void set_values (int a, int b) Rectangle rect;
{ width=a; height=b; } Triangle trgl;
virtual int area (void) =0; Polygon * ppoly1 = ▭
}; Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
class Rectangle: public Polygon { ppoly2->set_values (4,5);
public: cout << ppoly1->area() << '\n';
int area (void) cout << ppoly2->area() << '\n';
{ return (width * height); } return 0;
}; }
Pure virtual function
The compiler that the function has no body and above
virtual function will be called pure virtual function.
Syntax is
virtual double area_calc()=0;
class Shape//Base class
{
public:
Shape() {}
virtual double area_calc() =0;
};
Pure virtual member function Example
#include <iostream> class C : public A{
using namespace std; public:
class A C() {}
void output()
{
{ cout << " Hi" << endl; }
public:
};
A() {} int main()
virtual void output()=0; {
}; A* ptr;
class B : public A B b_object;
{ C c_object;
public: ptr = &b_object;
B() {} ptr->output();
//=> Base class 'output' member function
void output() {
ptr = &c_object;
cout << " Attempted re-write." << endl; } ptr->output();
}; return 0;
}