A63152219 Polymorphism
A63152219 Polymorphism
Polymorphism
• Polymorphism is the ability of an object or
reference to take many different forms at
different instances.
• These are of two types :
– compile time polymorphism
– run-time polymorphism
Polymorphism
TYPES OF POLYMORPHISM
• Compile time polymorphism: In this method
object is bound to the function call at the
compile time itself. Also called as early binding
or static binding.
• Run time polymorphism: In this method
object is bound to the function call only at the
run time. Also called as dynamic binding and
late binding.
EXAMPLE: Complile time
#include <iostream.h> int main ()
class Value {
{ protected: C cb;
int val; Value * ptr = &cb;
public: ptr->set_values (10);
void set_values (int a) cout << "The cube of 10 is::"
{ << cb.cube() << endl;
val=a; return 0;
} }; }
class C: public Value
{ public: OUTPUT:
The cube of 10 is::
int cube() 1000
{ return (val*val*val);
} };
#include<iostream.h>
EXAMPLE: Run time
class tape: public media
class media {
{ float time;
protected: public:
char title[50];
tape(char *s,float a, float t):media(s,a)
float price;
public: {
media(char *s, float a) time=t;
{ }
strcpy(title, s); void display();
price = a;
};
}
virtual void display() { } // empty virtual void book:: display()
function {
}; cout<<“\n title:”<<title;
class book: public media cout<<“\n pages:”<<pages;
{
int pages; cout<<“\n price:”<<price;
public: }
book(char *s, float a, int p): media(s,a) void tape:: display()
{ {
pages = p; cout<<“\n title:”<<title;
}
void display();
cout<<“\n ”<<time<<“mins”;
}; cout<<“\n price:”<<price;
int main() media* list[2]; //polymorphism
{ list[0] =&book1;
char *title = new char[30]; list[1] = &tape1;
float price, time; cout<<“ Media details”;
int pages; cout<<“\n….book…”;
// book details list[0] -> display(); // display book details
cout<<“enter book details:”; cout<<“\n…..tape….”;
list[1] -> display(); // display tape details
cout<<“title:”; cin>>title;
return 0;
cout<<“price:”; cin>>price; }
cout<<“pages:”; cin>>pages;
book book1(title, price, pages);
// tape details
cout<<“enter tape details:”;
cout<<“title:”; cin>>title;
cout<<“price:”; cin>>price;
cout<<“play time(mins):”;
cin>>time;
tape tape1(title, price, time);
OUTPUT:
Enter book details
title: Programming in C
price: 188
pages: 400
Media details
……Book…….
title: Programming in C
price: 188
pages: 400
……Tape……
title: Computing concepts
price: 99
play time(mins): 55
EXAMPLE
#include <iostream>
int area ()
class Shape {
{ cout << "Rectangle class area :" <<endl;
protected: return (width * height);
int width, height; } };
class Triangle: public Shape
public:
{
Shape( int a=0, int b=0) public:
{ Triangle( int a=0, int b=0):Shape(a, b)
width = a; {}
height = b; int area ()
{
}
cout << "Triangle class area :" <<endl;
int area() return (width * height / 2);
{ } };
cout << "Parent class area :" <<endl; // Main function for the program
return 0; int main( )
{
} };
Shape *shape;
class Rectangle: public Shape Rectangle rec(10,7);
{ Triangle tri(10,5); // store the address of Rectangle
public: shape = &rec; // call rectangle area.
shape->area(); // store the address of Triangle
Rectangle( int a=0, int b=0): Shape(a, b)
shape = &tri; // call triangle area.
{} shape->area();
return 0;
}
• When the above code is compiled and executed, it produces
the following result:
Parent class area
Parent class area
• The reason for the incorrect output is that the call of the
function area() is being set once by the compiler as the
version defined in the base class.
datatype (* function_name)()
Example
int (*funptr)();
#include<iostream.h>
Typedef void (*funptr)(int,int);
Void Add(int I, int j)
{
Cout<< i<<“+”<<j<<“=”<<i+j;
}
Void sub(int I, int j)
{
Cout<< i<<“+”<<j<<“=”<<i+j;
}
Void main()
{
Funptr ptr;
Ptr=&Add;
Ptr(1,2);
Ptr=⊂
Ptr(3,2);
Getch();
}
Virtual function
• When the same function name is used in the base and
derived classes, the function in base class is declared as
virtual using the keyword virtual preceding its normal
declaration.
• Virtual means existing in appearance but not in reality.
• Virtual functions are used in late binding or dynamic
binding.
• The concept of pointers play important role in the
virtual functions.
• When a function is virtual, C++ determines which
function to use at run time based on the type of object
pointed to by the base pointer rather than the type of
pointer.
• The selection of the function to be called at
any given point in the program to be based on
the kind of object for which it is called. This
sort of operation is referred to as dynamic
linkage, or late binding.
Rules for virtual function
• It must be members of some class.
• They can not be static.
• They are accessed by using object pointers.
• It can be friend of another class.
• Virtual function in base must be defined even
though it is not used.
• Incrementing decrementing base pointer does
not effect the derive class.
• If virtual function is defined in base class, not
necessary to redefine in derive class.
• Early binding or complie time binding or static
binding
• Late binding or runtime binding or dynamic
binding.
Static Binding
• Binding means linking.
• In c++, programs choosing the function in a
normal way during the compilation time is
called the static binding or static linking or
compile time binding or early binding.
• In this, compiler determines which function is
to be used based upon the parameters passed
to the function.
Late Binding or Dynamic Binding
• The concept of late binding is dependent on
the virtual function.
• The linking of the functions at run time is
called run time binding.
Pure Virtual Function
• A pure virtual function is a virtual function which has
no body i.e no arguments, no variables and no
expressions or statements inside it.