-
June 12th, 2005, 01:53 AM
#1
runtime and compile time polymorphism!!!
hi,
any body, pls tell me what is runtime polymorphism and compile time polymorphism and the difference between both?
or pls suggest me any good website or books that can best solve my doubts regarding object oriented programming!
thank you all.
regards,
ss
-
June 12th, 2005, 05:21 AM
#2
Re: runtime and compile time polymorphism!!!
polymorphism is the fact that a piece of code can behave differently depending on the object type (unknown at compile time) treated.
Polymorphism is just the use of virtual functions on a base-class, that may apply to different derived classes and call the different virtual functions of these different derived classes.
Here is an example of polymorphism:
Code:
#include <iostream>
class DisplayableObject
{
public:
virtual void Display()=NULL;
};
class Point:public DisplayableObject
{
private:
int x,y;
public:
void Display();
Point(int x,int y):x(x),y(y) {}
};
class Rectangle:public DisplayableObject
{
private:
int x,y;
unsigned width,height;
public:
void Display();
Rectangle(int x,int y,int width,int height):x(x),y(y),width(width),height(height) {}
};
void Point::Display()
{
std::cout<<"(Point: "<<x<<", "<<y<<")";
}
void Rectangle::Display()
{
std::cout<<"[Rectangle: "<<x<<", "<<y<<" : "<<width<<", "<<height<<"]";
}
void DisplayManyTimes(DisplayableObject *pObject)
{
for(unsigned i=0;i<10;i++)
pObject->Display(); // this code is polymorphic
}
int main()
{
Point p(5,3);
Rectangle r(1,1,5,5);
DisplayManyTimes(&p);
DisplayManyTimes(&r);
}
A function is said polymorphic, if it returns a pointer to a base class, and can return different newly-allocated objects (of derived classes).
If a function is polymorphic, it is very, very probable that the destructor of the classes is virtual (or should be), because the object destruction should be done with a pointer to the base class.
Maybe there is a difference between runtime and compile-time polymorphism, but i always thougth that polymorphism is always, by definition, runtime.
Templates (and overloading) could be named compile-time polymorphism, but can be also considered (at least by me), as not polymorphic.
Last edited by SuperKoko; June 12th, 2005 at 05:32 AM.
-
June 12th, 2005, 05:26 AM
#3
Re: runtime and compile time polymorphism!!!
I can suggest you to follow these links (i have not read the content of these web pages, but it looks like it can be useful for you).
http://programmers-corner.com/viewTutorial.php/36
http://www.mycplus.com/cplus.asp?ID=7&CID=28
http://www.cprogramming.com/tutorial.html
http://www.gamedev.net/reference/art...rticle2015.asp
http://www.developer.com/java/other/article.php/966001
The three first links comes from programmersheaven.com, and i get the two last with google (searching "compile-time polymorphism").
You can look at other wab pages with google.
Last edited by SuperKoko; June 12th, 2005 at 05:39 AM.
-
June 12th, 2005, 05:33 AM
#4
Re: runtime and compile time polymorphism!!!
-
June 12th, 2005, 05:34 AM
#5
Re: runtime and compile time polymorphism!!!
Originally Posted by kanth
any body, pls tell me what is runtime polymorphism and compile time polymorphism and the difference between both?
or pls suggest me any good website or books that can best solve my doubts regarding object oriented programming!
Last edited by Siddhartha; June 12th, 2005 at 05:35 AM.
Reason: FAQ Link already posted...
-
June 12th, 2005, 11:23 AM
#6
Re: runtime and compile time polymorphism!!!
I think by compile-time polymorphism, he is probably referring to function or template overloads, especially when working with a template where, in the code, you don't know what type you have, but call a function with many different overloads.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|