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 single and multiple inheritance?

749


What is difference between class and interface?

751


What are abstract classes?

771


Explain about a class in oop?

740


Can a class in java be inherited from more than one class?

676


What do you mean by "explicitly implemeting an interface". Give an example?

728


What are all the languages which support oop?

716


What is dynamic or run time polymorphism?

737


Explain about the relationship between object oriented programming and databases?

703


What is static polymorphism?

865


How can I be thrown polymorphically?

743


What is the function of pure virtual functions?

686


Write a program to show the inheritance used in a class.

716


What is the difference between an interface and abstract class?

780


How do you prevent a class from being inherited by another class?

779