Professional Documents
Culture Documents
Polymorph Is M
Polymorph Is M
Polymorph Is M
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:
int cube() The cube of 10 is:: 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.