tags:

views:

147

answers:

6

hi..

what is main difference between operator overloading and operator overriding in c++? please explain with example?

Edit1 :- Any good web links that would help a novice programmer to improve and be confident about the language he/she is learning.

+3  A: 

I've never heard the latter term, though I'd assume it'd be the same as the first one. Operator overloading is where you provide a function for a class to behave when used in an operator context. For example, if I had a class point, and wanted to add them such as a + b, I'd have to create an operator+(point other) function to handle this.

Delan Azabani
i would image he meant function overriding, hopefully
Woot4Moo
+1  A: 

Nice homework question. I'll help you here... Overloading means 2 methods with the SAME Name and different signatures + return types. Overriding means 2 methods with the SAME name, wherein the sub method has different functionality. Examples fall on you for this exercise.

Woot4Moo
A: 

I guess "operator overriding" applies when you declare an operator inside a class and make it virtual. That is a very fragile thing to do - you're usually better off doing something else.

Arkadiy
+2  A: 

In general overloading is used to identify when there is more than one signature for a given function name. Each such defined function is an overload of the function name. On the other hand override is present only in polymorphic (virtual in C++) member functions, where a redefinition of the same signature in a derived method overrides the behavior provided in the base class.

struct base {
   virtual void foo(); 
   void foo(int);      // overloads void foo()
};
struct derived : base {
   void foo();         // overrides void base::foo()
   void foo(int);      // overloads void derived::foo()
                       // unrelated to void::base(int)
};
int main() {
   derived d;
   base & b = d;
   b.foo( 5 );   // calls void base::foo(int)
   b.foo();      // calls the final overrider for void base::foo()
                 // in this particular case void derived::foo()
   d.foo( 5 );   // calls void derived::foo(int)
}
David Rodríguez - dribeas
can you tell what `base ` this line does?
bunty
@bunty: It creates a _reference_ to `d`. While the reference's _static type_ is `base`, the _dynamic type_ of the object it refers to is `derived`. A virtual function called through a base class reference pointing to a derived class' object will be routed to an _overriding_ implementations of that virtual functions in the derived class, if one exists. So `b.foo()` will call `derived::foo()`, because that overload of `foo()` is a virtual function. OTOH, `b.foo(5)` is statically (at compile-time) bound to `base::foo(int)`, because that overload is not virtual.
sbi
+6  A: 

Some use the latter term to describe what's being done when you defined an own global operator new or operator delete. That's because your own definition can replace the default version in the library. The C++ Standard uses the words replaces and displaces for this. Using "override" is a bit confusing because that term is already used for virtual functions being overridden by a function in a derived class.

The term "overloading" is the general term used for defining your own operator functions. This term is used even if no actual overloading happens by the operator function. One way to approach this term is because it "overloads" the built-in meaning of certain operators.

Johannes Schaub - litb
I think it's possible to overload operators as virtual functions and then override them :)
Maciej Hehl
@Maciej yes that's possible too. In fact it adds to the confusion that when a destructor is virtual, calling delete on a base-class calls the `operator delete` of the most-derived class. So in some sense one might talk about class-specific `operator delete` as override the ones of base-classes too in a yet another meaning than I used in my post (even though operator delete is a static member function and so can't be virtual itself). I think people are very innovative when it comes to using terms :)
Johannes Schaub - litb
+1  A: 

if the question is regarding function overloading and function overriding then... @woot4moo has explained that thing . As far as my knowledge is concerned i have never heard of "operator overriding". operator overloading is done to provide your class's object with special functionality related to that operator , whenever that operator is to be used with the object . For example a class should provide an operator(=) to always prevent shallow coping.(well that is done in conjunction with a copy constructor).

CadetNumber1
No, the question is related to operator overloading and operator overriding.
bunty