Thread: function polymorphism

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    function polymorphism

    Code:
    int   myInt =6500;
              long  myLong =65000;
    Code:
    int Double(int original)
    {
    	cout << " In Double(int)\n";
    	return 2 * original;
    }
    long Double(long original)
    {
    	cout <<"In Double(long)\n";
    	return 2 * original;
    }
    is original refering to the values assigned? and how?

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    403
    This is overloading, not polymorphism. The compiler can tell which version to call from the parameter types. Think of it like it is generating internally 2 functions Double_int and Double_long (this would be how you would have to do it were there no overloading in C++) then when it sees a call to Double with a long parameter, it calls Double_long, and the same w/ int. Original is the name of the parameter, just like if you had int Double(int n), the return would be return 2*n;

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    The best definition of polymorphism that I've heard is that it's when one piece of code can be executed for different datatypes. In that case if you wrote
    Code:
    template<class T>
    T Double(T original)
    {
          return 2 * original;
    }
    People refer to this as static polymorphism because it is done at compile time. It will most likely generate the same two function calls you had above, kind of begging the question of what the code for your function is.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    function overloading is also called function polymorphism, i understand what the prog is doing, but not how is doing it. how does it know that original which is not defined anywhere has 2 different values in different types, when int double is called and when long double is called, original has a different value.
    Last edited by InvariantLoop; 04-20-2004 at 08:54 PM.

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    just think of your code this way:

    you compiler compiles your code and sees that you have 2 functions declared with the same name, it checks to make sure all of the syntax is correct and then makes a note in its memory for later that there are two functions with the same name.

    Now, when you call function Double, depending on what you pass to the function, the compiler will automatically choose which function to call. For example, if you did Double((int)34) it would call the integer version of the function, but if you called Double((long)34) it would call the long version.

    That make any sense?

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    class Object
    {
       public:
         Object() {};
         virtual void Draw();
    };
    
    
    class Square:public Object
    {
       public:
          Square() {};
          virtual void Draw();
    }
    This is polymorphism. The draw function is redefined in each class - object knows how to draw object and square knows how to draw square.

    The compiler will call Square::Draw when called from objects of type square...

    ...and will call Object::Draw when called from objects of type Object.

    However, calling function overloading the same as function polymorphism...yeah....I'll buy it because I see the point....but polymorphism is more than that.


    There is also something else at work here. Object could theoretically call the draw function of all who derive from it. So to draw an object you could simply call one function which would draw each object instead of having to call square->draw(), circle->draw(), etc. But that's for another thread.




    Of course this is a stupid example but you get the idea.
    Last edited by VirtualAce; 04-20-2004 at 10:08 PM.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Yes, you're right. Were're not used to polymorphism being applied to function overloading.
    http://en.wikipedia.org/wiki/C_Plus_Plus#Polymorphism

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    C++ uses a name mangling scheme. When the C++ compiler compiles code such as
    int f(int i)
    {
    }

    int f(double f, int j)
    {

    }


    it encodes the function name using the function name in the C++ file, the parameters, and a few other things but not the return type.

    In this code, the C++ compiler could write out assembly code that had functions named __f_int and __f_double_int

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I much prefer dynamic polymorphism. It saves a lot of coding as opposed to writing a function for every case or data type -> hence we have templates as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM