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

Write a note on encapsulation?

589


What are the problems faced by the developer using object oriented programming language?

554


How to make a class accessible as a web service?

531


What is hybrid inheritance?

571


What are all the operators that cannot be overloaded?

554






What is the function of pure virtual functions?

524


What is meant by overloading functions and operators?

584


What is a subclass?

560


How can we restrict inheritance for a class so that no class can be inherited from it?

666


How does abstraction differ from encapsulation.

574


What is dynamic or run time polymorphism?

563


What is the difference between an error and an exception?

521


Differentiate between an abstract class and an interface?

563


What is Overloading ? Is it similar to overriding ?

586


Explain what is meant by polymorphism?

577