write a short note on Overloading of Binary Operator?

Answer Posted / rupinder

You overload a binary unary operator with either a nonstatic
member function that has one parameter, or a nonmember
function that has two parameters. Suppose a binary operator
@ is called with the statement t @ u, where t is an object
of type T, and u is an object of type U. A nonstatic member
function that overloads this operator would have the
following form:

return_type operator@(T)

A nonmember function that overloads the same operator would
have the following form:

return_type operator@(T, U)

An overloaded binary operator may return any type.

The following example overloads the * operator:

struct X {

// member binary operator
void operator*(int) { }
};

// non-member binary operator
void operator*(X, float) { }

int main() {
X x;
int y = 10;
float z = 10;

x * y;
x * z;
}

The call x * y is interpreted as x.operator*(y). The call x
* z is interpreted as operator*(x, z).

Is This Answer Correct ?    9 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can we create object of interface?

601


What is the diamond problem in inheritance?

577


What are functions in oop?

582


How to use CMutex, CSemaphore in VC++ MFC

4328


What is meant by multiple inheritance?

736






What are the advantages of polymorphism?

573


What is new keyword in oops?

589


Can we create object of abstract class?

575


They started with the brief introduction followed by few basic C++ questions on polumorphism, inheritance and then virtual functions. What is polymorphims? How you will access polymorphic functions in C? How virtual function mechanism works?

1393


Can private class be inherited?

615


What is polymorphism what is it for and how is it used?

572


Do you know about multiple inheritance?

637


What is cohesion in oop?

621


Can main method override?

583


What is polymorphism give a real life example?

557