What is the use of Operator Overloading?

Answer Posted / ben jacob

Operator Overloading helps users/developers use a particular
class in an intuitive manner for different kind of
operations logically possible on the class.
It's used for ease of code read and maintainability.

e.g. Date 'b' can be subtracted from another instance of
Date, say 'a', to get the difference in number of days
between the two days.
So, you would overload the subtraction operator '-' for the
Date class accordingly.
Date a("07/04/2008);
Date b("05/04/2008);

//operator overoading for '-' for Date
//returns number of days
int Date::operator-(const Date& rhsDate)
{
//wotever logic
return <number of days between the (this) date and rhsDate>
}

Is This Answer Correct ?    5 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between a class and an object?

530


Can we reduce the visibility of the inherited or overridden method ?

612


What is meant by inheritance? what are the advantages of inheritance?

542


What do you mean by abstract classes?

561


Why is java not fully objective oriented?

594






Can you tell something about GDI objects?

601


Explain the usage of encapsulation?

602


Explain what a method is?

615


Why are outlet connections required in object-oriented programming?

551


Can you create an instance of an abstract class?

566


What is an interface?

593


What is the function of dynamic typing?

565


What is the use of finalize method?

584


Explain persistence?

574


What is the main difference between a class and an object?

559