By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,711 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,711 IT Pros & Developers. It's quick & easy.

Diff btwn Function overloading and overriding

P: n/a
Hi,
What is the Diff btwn Function overloading and overriding

thanks,

a.a.cpp

Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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.
Jul 23 '05 #2

P: n/a
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.

Jul 23 '05 #3

P: n/a
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.

Jul 23 '05 #4

P: n/a
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.
Jul 23 '05 #5

P: n/a
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.

Jul 23 '05 #6

P: n/a
> 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)'


Jul 23 '05 #7

P: n/a
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?
Jul 23 '05 #8

P: n/a
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.

Jul 23 '05 #9

P: n/a
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?

Jul 23 '05 #10

P: n/a
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].
Jul 23 '05 #11

P: n/a

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

Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.