Diff btwn Function overloading and overriding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • iceColdFire

    Diff btwn Function overloading and overriding

    Hi,
    What is the Diff btwn Function overloading and overriding

    thanks,

    a.a.cpp

  • Ron Natalie

    #2
    Re: Diff btwn Function overloading and overriding

    iceColdFire wrote:[color=blue]
    > Hi,
    > What is the Diff btwn Function overloading and overriding
    >[/color]
    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.

    Comment

    • Rolf Magnus

      #3
      Re: Diff btwn Function overloading and overriding

      iceColdFire wrote:
      [color=blue]
      > Hi,
      > What is the Diff btwn Function overloading and overriding[/color]

      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.

      Comment

      • Jaspreet

        #4
        Re: Diff btwn Function overloading and overriding

        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.

        Comment

        • Ron Natalie

          #5
          Re: Diff btwn Function overloading and overriding

          Jaspreet wrote:
          [color=blue]
          >
          > 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.
          >[/color]

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

          Comment

          • Rolf Magnus

            #6
            Re: Diff btwn Function overloading and overriding

            Ron Natalie wrote:
            [color=blue]
            > Jaspreet wrote:
            >[color=green]
            >>
            >> 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.
            >>[/color]
            >
            > It's a perfectly valid form of overloading,[/color]

            No, it's not.
            [color=blue]
            > it just may be ill-formed or ambiguous depending on the context.[/color]

            There is no context in which C++ allows function overloading only by return
            type.

            Comment

            • Jaspreet

              #7
              Re: Diff btwn Function overloading and overriding

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

              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:[color=blue][color=green][color=darkred]
              >>>>>>>>>new declaration `float area(int)'
              >>>>>>>>>ambigu ates old declaration `int area(int)'[/color][/color][/color]

              Comment

              • Ron Natalie

                #8
                Re: Diff btwn Function overloading and overriding

                Jaspreet wrote:[color=blue][color=green]
                >>However,
                >>int area(int side);
                >>float area(int length);
                >>are not valid examples of overloading since the signature is the same[/color]
                >
                >[color=green]
                >>and the only difference is in the return type.[/color]
                >
                >
                > 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:
                >[color=green][color=darkred]
                >>>>>>>>>>new declaration `float area(int)'
                >>>>>>>>>>ambig uates old declaration `int area(int)'[/color][/color]
                >
                >[/color]
                And this counters what I said how?

                Comment

                • Jaspreet

                  #9
                  Re: Diff btwn Function overloading and overriding

                  Ron Natalie wrote:[color=blue]
                  > Jaspreet wrote:
                  >[color=green]
                  > >
                  > > However,
                  > > int area(int side);
                  > > float area(int length);
                  > >
                  > > are not valid examples of overloading since the signature is the[/color][/color]
                  same[color=blue][color=green]
                  > > and the only difference is in the return type.
                  > >[/color]
                  >
                  > It's a perfectly valid form of overloading, it just may be ill-formed
                  > or ambiguous depending on the context.[/color]

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

                  Comment

                  • Rolf Magnus

                    #10
                    Re: Diff btwn Function overloading and overriding

                    Ron Natalie wrote:
                    [color=blue]
                    > Jaspreet wrote:[color=green][color=darkred]
                    >>>However,
                    >>>int area(int side);
                    >>>float area(int length);
                    >>>are not valid examples of overloading since the signature is the same[/color]
                    >>
                    >>[color=darkred]
                    >>>and the only difference is in the return type.[/color]
                    >>
                    >>
                    >> 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:
                    >>[color=darkred]
                    >>>>>>>>>>>ne w declaration `float area(int)'
                    >>>>>>>>>>>ambi guates old declaration `int area(int)'[/color]
                    >>[/color]
                    > And this counters what I said how?[/color]

                    It doesn't. However, how about giving an example that proves your claim?

                    Comment

                    • Ron Natalie

                      #11
                      Re: Diff btwn Function overloading and overriding

                      Jaspreet wrote:[color=blue]
                      > Ron Natalie wrote:
                      >[color=green]
                      >>Jaspreet wrote:
                      >>
                      >>[color=darkred]
                      >>>However,
                      >>>int area(int side);
                      >>>float area(int length);
                      >>>
                      >>>are not valid examples of overloading since the signature is the[/color][/color]
                      >
                      > same
                      >[color=green][color=darkred]
                      >>>and the only difference is in the return type.
                      >>>[/color]
                      >>
                      >>It's a perfectly valid form of overloading, it just may be ill-formed
                      >>or ambiguous depending on the context.[/color]
                      >
                      >
                      > Hi Ron
                      > You mentioned it is a **perfectly valid form of overloading**. It is
                      > not.
                      >[/color]
                      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].

                      Comment

                      • Jaspreet

                        #12
                        Re: Diff btwn Function overloading and overriding


                        Ron Natalie wrote:[color=blue]
                        > 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[/color]
                        USING].

                        Hi

                        I should have seen that. I forgot for once about using namespace.
                        Thanks..

                        Comment

                        Working...