answersLogoWhite

0


Best Answer

Unary operators declared as member function take NO arguments; if declared as global function, then take only one argument.

Binary operators declared as member functions take one argument; if declared as global function, then would take two arguments.

Note: The first argument for member function overloaded is always the class type; the class in which the operator is declared.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

It is important to note that you cannot override operators with respect to primitive data types, nor can you define new operators. You can only override operators with respect to complex objects (non-primitives).

Unary operators, such as the prefix and postfix increment operators, and binary operators, such as the addition and subtraction operators, can be overloaded by declaring them members of a class. The l-value of all class operators is implicitly an instance of the class itself. The r-value (in the case of binary operators) can be any type, including primitives. The return value may be any type, but will typically be an instance of the l-value.

You cannot overload class operators for which you do not have access to the source. However, you can cater for this eventuality by using external functions which overload the operator itself. These operators require an l-value (an instance of the class for which you do not have access), and an r-value (an instance of the class for which an overload does not yet exist). Again, the return value will typically be an instance of the l-value. External operator overloads are often declared friends of the r-value class, however it is important to note that this is only necessary when a suitable interface does not exist within the r-value and the only solution is to allow the operator private access to the class members. Although friends are often said to undermine encapsulation this is errant nonsense. Nevertheless, friend access should be kept to an absolute minimum and should only be employed when there is no suitable interface. If exposing an interface would itself undermine encapsulation, then friend access would, in fact, re-enforce that encapsulation.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

A picture paints a thousand words, so here's a code sample:

class MyClass {private:int value;

public:MyClass(int initValue) : value(initValue) {}

MyClass operator-() const {MyClass result(-value);return result;}

int getValue() const {return value;}};

Note that operator-() returns a new MyClass object; the original object is unmodified.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Differentiate between overloading unary operator and overloading binary operator?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why to take two arguments in binary operator overloading using friend function?

Binary operators require two operands (l-value and r-value) and therefore require two arguments when overloading via external functions. When overloading class member operators, the l-value is the class instance itself (the implicit this pointer), therefore only the r-value need be given as an argument.


What is a binary operator in the C language?

In programming languages, a binary operator is an operator which takes two operands. For example, the divide-by sign between divident and divisor is a binary operator:x = a / bOther binary operators include + - * & | ^, among others.Note that the operator is binary, not the character representing it. Take, for example, the minus sign. The minus sign represents the binary subtraction operator when used between two arithmetic expressions (e.g. x = a - b). However, when used left of an arithmetic expression, it indicates a negative sign (e.g. x = -a). Parentheses may be required to avoid ambiguity or enhance readibility of both effects are combined (e.g. x = a - (-b)).


What is plus operator is it unary or binary?

There is no unary plus in C, but if there were, it would have only one operand, unlike the binary plus which has two: x = a + b; /* binary plus */ x = + b; /* unary plus -- not in C*/ x = a - b; /* unary plus */ x = - b; /* unary minus */


Explain the Difference between bitwise operator ' and ' and address operator ' and ' of pointer?

The bitwise logical operator and (&) calculates the bitwise logical and of two integral values. It is a binary operator.The address of (&) operator returns the address of the value to its right. It is a unary operator.The distinction between the two is one of context. The logical and operator will follow (and be preceeded by) a value, while the address of operator will follow an operator.


What is difference between conditional operator and bitwise operator?

A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:1 + 2The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:1 + 2The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:1 + 2The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:1 + 2The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.

Related questions

Distinguish between operator overloading and function overloading?

in C++ there is no real difference as operators are overloaded by implementing them as functions. However, while we differentiate between function overloads by the function signature (the number and type of parameters), operator overloads are distinguished only by the parameter types. The parameters are interpreted as operands, and the number of operands will depend upon whether the operator is unary, binary or ternary. That is, for any given operator, the number of operands will be the same for each overload you implement. The only exceptions are the unary increment (++) and decrement (--) operators as they each have postfix and prefix variants. In order to differentiate their signatures, an unreferenced or dummy parameter must be passed to the postfix variants.


Why to take two arguments in binary operator overloading using friend function?

Binary operators require two operands (l-value and r-value) and therefore require two arguments when overloading via external functions. When overloading class member operators, the l-value is the class instance itself (the implicit this pointer), therefore only the r-value need be given as an argument.


What is a binary operation?

A binary operator is simply an operator that works with two operands (for example, two numbers). The binary operator is usually written between the two operands. Examples include the familiar operations of addition, subtraction, multiplication, or division - for example, in: 2 + 3 the "plus" is the binary operator, which works on the two numbers written on either side of it. What is an operator: Basically a function (calculation rule), written in a special way.


What is the difference between unary and binary?

In programming a unary and binary operator defines how many components make up an expression.


What is binary operation?

A binary operator is simply an operator that works with two operands (for example, two numbers). The binary operator is usually written between the two operands. Examples include the familiar operations of addition, subtraction, multiplication, or division - for example, in: 2 + 3 the "plus" is the binary operator, which works on the two numbers written on either side of it. What is an operator: Basically a function (calculation rule), written in a special way.


Give me the answer Difference between unary and binary operator?

A unary operator is one which operates on just one number, e.g. x2 or -x. A binary operator is one which takes two numbers, e.g. x + y or xy. Note that this has nothing to do with binary vs. decimal arithmetic. This term is commonly used to distinguish between the two buttons which have a '-' sign on a calculator. The unary '-' operator button is used to change the sign of a number entered, and the binary '-' operator button is used to subtract two numbers, which is quite a different thing. The unary '-' button is commonly labelled '+/-'.


What is a binary operator in the C language?

In programming languages, a binary operator is an operator which takes two operands. For example, the divide-by sign between divident and divisor is a binary operator:x = a / bOther binary operators include + - * & | ^, among others.Note that the operator is binary, not the character representing it. Take, for example, the minus sign. The minus sign represents the binary subtraction operator when used between two arithmetic expressions (e.g. x = a - b). However, when used left of an arithmetic expression, it indicates a negative sign (e.g. x = -a). Parentheses may be required to avoid ambiguity or enhance readibility of both effects are combined (e.g. x = a - (-b)).


How do you find the product of 16?

"Product" is a binary operator. A binary operator takes two numbers as input and combines them into an output. Your question gives only one number as input and so a sensible answer is impossible. "Product" is a binary operator. A binary operator takes two numbers as input and combines them into an output. Your question gives only one number as input and so a sensible answer is impossible. "Product" is a binary operator. A binary operator takes two numbers as input and combines them into an output. Your question gives only one number as input and so a sensible answer is impossible. "Product" is a binary operator. A binary operator takes two numbers as input and combines them into an output. Your question gives only one number as input and so a sensible answer is impossible.


What is 0.001 0.007 in word form?

The answer will depend on the unspecified binary operator between the two numbers which are given in the question.


What is plus operator is it unary or binary?

There is no unary plus in C, but if there were, it would have only one operand, unlike the binary plus which has two: x = a + b; /* binary plus */ x = + b; /* unary plus -- not in C*/ x = a - b; /* unary plus */ x = - b; /* unary minus */


Explain the Difference between bitwise operator ' and ' and address operator ' and ' of pointer?

The bitwise logical operator and (&) calculates the bitwise logical and of two integral values. It is a binary operator.The address of (&) operator returns the address of the value to its right. It is a unary operator.The distinction between the two is one of context. The logical and operator will follow (and be preceeded by) a value, while the address of operator will follow an operator.


What is the commutative property of a fact?

Commutativity is a property of binary operations. A fact is not a binary operator.