Polymorphism

In polymorphism we can create functions with same name. How memory is allocated to functions? Do same names means same memory? Basically I want to know what is happening in memory area when i am creating two functions lets say
int add(int ,int);
int add(int, int,int);

Replies

  • [Prototype]
    [Prototype]
    I am not aware of actual thing but my common sense says they don't have same memory.

    I mean, I can write my function name as add and do some multiplication operation there. What I want to say is, just because the name is same, you cannot assume the contents will be same. add(int, int) and add(int,int,int) are two different independent function with their own contents. Hence they must have their own space in the memory.

    According to me the compiler puts the reference of appropriate function depending on the arguments at the compile time.
  • harmanveer kaur
    harmanveer kaur
    then whats the use of functions having same same? I mean why polymorphism? We can define two separate functions.
    Is it only for user convenience?
  • Ramani Aswath
    Ramani Aswath
    A square, a rectangle or a parallellogram is a subclass of the superclass quadrilateral. In programming it is sometimes advantageous to refer to a quadrilateral for all quadrilaterals and let the actual action be done specific to the special case at runtime.

    Some details are here:

    #-Link-Snipped-#

    #-Link-Snipped-#

    Polymorphism %28Computer Science%29
  • radha gogia
    radha gogia
    Harmanveer
    then whats the use of functions having same same? I mean why polymorphism? We can define two separate functions.
    Is it only for user convenience?
    see whenever u call a function in memory there is a data structure which is called a stack which is formed in the memory,and in that function u have the local variables which are ur actually arguments and the processing which needs to be done .
    whether u make 7 functions of same name it doesnt matter ,u will have a stack maintained of those 7 functions but obviously something would differentiate all of them,say fun(int) calls fun(int,float) so at the bottom of the stack i have fun and on top of stack i have fun(int,float).
    and i would just suggest u that i may not be clearing ur doubt properly so plz read on net about function calling which is implemented through stack
  • [Prototype]
    [Prototype]
    Harmanveer
    then whats the use of functions having same same? I mean why polymorphism? We can define two separate functions.
    Is it only for user convenience?
    What you asked is just a very basic form of polymorphism. There's much more to the polymorphic capabilities provided by the OOP, especially C++.

    Coming to the question, lets just say you created a library. Some other developer might be just using your functions. Don't you think it'll be handy to call a function with same name to perform a 'similar' function on different data types? For eg. Adding of two integers is called "addition" and the same is true for adding two floats. I find it cool that I can just say add(x,y) and compiler appropriate function gets called instead of making two functions with different add_int(x,y) and add_float(m,n).

You are reading an archived discussion.

Related Posts

It seems that Microsoft Corp. did not stop after announcing the wonderful Lumia series- Lumia 830, Lumia 730 Dual SIM and Lumia 735 at IFA 2014. The tech giant has...
Motorola, previously owned by Google, soon to be in hands of Lenovo has announced the new generation of its smartphones. The closed door global launch event was held yesterday in...
Amidst all the announcements about amazing smartphones and accessories, we believe there's a tiny device - Moto Hint, that deserves your due attention. It is a wearable discreet wireless earbud...
https://timesofindia.indiatimes.com/...-Indias-top-IT-firms/articleshow/41535783.cms Why do you think? I think, IT firms mostly use Java or .NET and its difficulty to find quality programmers anyway.
Moving fast in an attempt to catch up with the tablet-specialist ARM processors, Intel has launched the first of the Core M (codenamed Broadwell-Y) range of processors. This announcement came...