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

What are the 3 pillars of oop?

725


What is oops concept with example?

657


How do you define a class in oop?

730


What is the real life example of polymorphism?

711


Why do we use oops?

672






Why is static class not inherited?

690


What is pointer in oop?

628


Is enum a class?

687


What is interface? When and where is it used?

1755


What is encapsulation oop?

666


Are polymorphisms mutations?

796


What is a null tree?

734


Is this job good for future? can do this job post grduate student?

1793


What are different types of JVM's? for example we use dalvik jvm for android then what about the remaining operating systems?

1747


What is the difference between procedural programming and oops?

647