CrazyEngineers
  • Polymorphism

    Updated: Oct 26, 2024
    Views: 5.6K
    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);
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • [Prototype]

    MemberSep 4, 2014

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • harmanveer kaur

    MemberSep 4, 2014

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • Ramani Aswath

    MemberSep 4, 2014

    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-#

    <a href="https://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29" target="_blank" rel="nofollow noopener noreferrer">Polymorphism %28Computer Science%29</a>
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberSep 5, 2014

    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
    Are you sure? This action cannot be undone.
    Cancel
  • [Prototype]

    MemberSep 10, 2014

    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).
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register