Diff btwn Function overloading and overriding

Discussion in 'C++' started by iceColdFire, May 14, 2005.

  1. iceColdFire

    iceColdFire Guest

    Hi,
    What is the Diff btwn Function overloading and overriding

    thanks,

    a.a.cpp
     
    iceColdFire, May 14, 2005
    #1
    1. Advertising

  2. iceColdFire

    Ron Natalie Guest

    iceColdFire wrote:
    > Hi,
    > What is the Diff btwn Function overloading and overriding
    >

    Overloading refers to the selection of multiple signatures of functions
    of the same name:
    A(int x);
    A(std::string s);
    A(double d);
    is overloading the function name A.

    Overriding refers to functions that have the same signature as a
    virtual function in the base class:
    class B {
    virtual void V();
    };

    class D : public B {
    viod V();
    };

    D::V overrides B::V.
     
    Ron Natalie, May 14, 2005
    #2
    1. Advertising

  3. iceColdFire

    Rolf Magnus Guest

    iceColdFire wrote:

    > Hi,
    > What is the Diff btwn Function overloading and overriding


    Yes. They are two totally different concepts. Overloading means that you can
    have several different functions with the same name.
    Overriding means that out of several functions, the 'right' one is selected
    at run-type depending on the dynamic type of an object.
     
    Rolf Magnus, May 14, 2005
    #3
  4. iceColdFire

    Jaspreet Guest

    Hi

    Function overloading is when you have same function names with
    different signatures.
    Remember the signature just refers to the number and type of arguments.
    The return type does not matter.
    Soo,

    int area(int side);
    int area(int length, int breadth);
    int area(int length, int breadth, int height);

    are valid examples of overloading.

    However,
    int area(int side);
    float area(int length);

    are not valid examples of overloading since the signature is the same
    and the only difference is in the return type.
     
    Jaspreet, May 16, 2005
    #4
  5. iceColdFire

    Ron Natalie Guest

    Jaspreet wrote:

    >
    > However,
    > int area(int side);
    > float area(int length);
    >
    > are not valid examples of overloading since the signature is the same
    > and the only difference is in the return type.
    >


    It's a perfectly valid form of overloading, it just may be ill-formed
    or ambiguous depending on the context.
     
    Ron Natalie, May 16, 2005
    #5
  6. iceColdFire

    Rolf Magnus Guest

    Ron Natalie wrote:

    > Jaspreet wrote:
    >
    >>
    >> However,
    >> int area(int side);
    >> float area(int length);
    >>
    >> are not valid examples of overloading since the signature is the same
    >> and the only difference is in the return type.
    >>

    >
    > It's a perfectly valid form of overloading,


    No, it's not.

    > it just may be ill-formed or ambiguous depending on the context.


    There is no context in which C++ allows function overloading only by return
    type.
     
    Rolf Magnus, May 16, 2005
    #6
  7. iceColdFire

    Jaspreet Guest

    > However,
    > int area(int side);
    > float area(int length);
    > are not valid examples of overloading since the signature is the same


    > and the only difference is in the return type.


    It's a perfectly valid form of overloading, it just may be ill-formed
    or ambiguous depending on the context.

    I am using gcc 3.2.2 and it gives me the following error:
    >>>>>>>>>new declaration `float area(int)'
    >>>>>>>>>ambiguates old declaration `int area(int)'
     
    Jaspreet, May 17, 2005
    #7
  8. iceColdFire

    Ron Natalie Guest

    Jaspreet wrote:
    >>However,
    >>int area(int side);
    >>float area(int length);
    >>are not valid examples of overloading since the signature is the same

    >
    >
    >>and the only difference is in the return type.

    >
    >
    > It's a perfectly valid form of overloading, it just may be ill-formed
    > or ambiguous depending on the context.
    >
    > I am using gcc 3.2.2 and it gives me the following error:
    >
    >>>>>>>>>>new declaration `float area(int)'
    >>>>>>>>>>ambiguates old declaration `int area(int)'

    >
    >

    And this counters what I said how?
     
    Ron Natalie, May 17, 2005
    #8
  9. iceColdFire

    Jaspreet Guest

    Ron Natalie wrote:
    > Jaspreet wrote:
    >
    > >
    > > However,
    > > int area(int side);
    > > float area(int length);
    > >
    > > are not valid examples of overloading since the signature is the

    same
    > > and the only difference is in the return type.
    > >

    >
    > It's a perfectly valid form of overloading, it just may be ill-formed
    > or ambiguous depending on the context.


    Hi Ron
    You mentioned it is a **perfectly valid form of overloading**. It is
    not.
     
    Jaspreet, May 17, 2005
    #9
  10. iceColdFire

    Ron Natalie Guest

    Jaspreet wrote:
    > Ron Natalie wrote:
    >
    >>Jaspreet wrote:
    >>
    >>
    >>>However,
    >>>int area(int side);
    >>>float area(int length);
    >>>
    >>>are not valid examples of overloading since the signature is the

    >
    > same
    >
    >>>and the only difference is in the return type.
    >>>

    >>
    >>It's a perfectly valid form of overloading, it just may be ill-formed
    >>or ambiguous depending on the context.

    >
    >
    > Hi Ron
    > You mentioned it is a **perfectly valid form of overloading**. It is
    > not.
    >

    I said it can either be ill-formed or ambiguous DEPENDING ON THE
    CONTEXT. In your example it's ill-formed. There are other cases
    where it's not ill-formed to declare such, but it gets ambiguous
    when you try to actually use it. [Put the two area's in different
    namespaces and then bring them into the current namespace with USING].
     
    Ron Natalie, May 17, 2005
    #10
  11. iceColdFire

    Rolf Magnus Guest

    Ron Natalie wrote:

    > Jaspreet wrote:
    >>>However,
    >>>int area(int side);
    >>>float area(int length);
    >>>are not valid examples of overloading since the signature is the same

    >>
    >>
    >>>and the only difference is in the return type.

    >>
    >>
    >> It's a perfectly valid form of overloading, it just may be ill-formed
    >> or ambiguous depending on the context.
    >>
    >> I am using gcc 3.2.2 and it gives me the following error:
    >>
    >>>>>>>>>>>new declaration `float area(int)'
    >>>>>>>>>>>ambiguates old declaration `int area(int)'

    >>

    > And this counters what I said how?


    It doesn't. However, how about giving an example that proves your claim?
     
    Rolf Magnus, May 17, 2005
    #11
  12. iceColdFire

    Jaspreet Guest

    Ron Natalie wrote:
    > I said it can either be ill-formed or ambiguous DEPENDING ON THE
    > CONTEXT. In your example it's ill-formed. There are other cases
    > where it's not ill-formed to declare such, but it gets ambiguous
    > when you try to actually use it. [Put the two area's in different
    > namespaces and then bring them into the current namespace with

    USING].

    Hi

    I should have seen that. I forgot for once about using namespace.
    Thanks..
     
    Jaspreet, May 18, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cyril Vi?ville

    diff Process under diff users

    Cyril Vi?ville, Jun 29, 2004, in forum: Perl
    Replies:
    1
    Views:
    523
    Joe Smith
    Jun 29, 2004
  2. Berrucho
    Replies:
    2
    Views:
    668
    Infant Newbie
    Dec 5, 2003
  3. A Traveler

    Diff CSS styles for diff INPUT TYPE='s?

    A Traveler, Aug 31, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,958
    Steve Fulton
    Aug 31, 2004
  4. Shinya Koizumi

    Difference btwn Servlet and Web Service

    Shinya Koizumi, Apr 29, 2006, in forum: Java
    Replies:
    3
    Views:
    15,346
  5. glm
    Replies:
    0
    Views:
    94
Loading...

Share This Page