CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2004
    Posts
    10

    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

  2. #2
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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.

  3. #3
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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.

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: runtime and compile time polymorphism!!!


  5. #5
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: runtime and compile time polymorphism!!!

    Quote 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...

  6. #6
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    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
  •  





Click Here to Expand Forum to Full Width

Featured