---
title: "Binary and unary operator overloading"
subject: "Object Oriented Programming"
chapter: 5
key: "semester_3/object_oriented_programming:binary-unary-overloading"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/binary-unary-overloading
site: BCT Engineering
---

# Binary and unary operator overloading

Object Oriented Programming · Chapter 5: Operator Overloading

- Asked in: 2070 Asar, 4 marks
- Video: [Unary Operator Overloading in C++ | Binary Operator Overloading in C++ | C++ Operator Overloading](https://www.youtube.com/watch?v=qZyOP9kT-9c) — 3RI Technologies
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/binary-unary-overloading
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-5

## Question

Explain the binary and unary operator overloading along with their syntax and example.

## Short answer

Unary overload: member takes no argument (operand is this), friend takes one. E.g., void operator-() negates. Binary overload: member takes one argument (right operand), friend takes two. E.g., Point operator+(Point) adds. Syntax: member – ret_type operator op(); friend – ret_type operator op(arg).

## Answer

#### Unary operator overloading
A unary operator works on a single operand (e.g. `-`, `++`, `--`, `!`). As a member function it takes **no argument**, because the operand is the calling object. As a friend function it takes one argument.

Syntax:
```cpp
return_type operator op ();                 // member
friend return_type operator op (Class &a);  // friend
```

Example - unary minus (negates a point):

```cpp
#include <iostream>
using namespace std;

class Point {
    int x, y;
public:
    Point(int a = 0, int b = 0) { x = a; y = b; }
    void operator-() { x = -x; y = -y; }     // unary minus
    void show() { cout << "(" << x << ", " << y << ")" << endl; }
};

int main() {
    Point p(3, -4);
    -p;                  // calls p.operator-()
    p.show();            // (-3, 4)
    return 0;
}
```

#### Binary operator overloading
A binary operator works on two operands (e.g. `+`, `-`, `*`, `==`). As a member function it takes **one argument** (the right operand; the left is the calling object). As a friend it takes two arguments.

Syntax:
```cpp
return_type operator op (Class b);                 // member
friend return_type operator op (Class a, Class b); // friend
```

Example - adding two points:

```cpp
#include <iostream>
using namespace std;

class Point {
    int x, y;
public:
    Point(int a = 0, int b = 0) { x = a; y = b; }
    Point operator+(Point p) {                // binary +
        return Point(x + p.x, y + p.y);
    }
    void show() { cout << "(" << x << ", " << y << ")" << endl; }
};

int main() {
    Point a(1, 2), b(3, 4), c;
    c = a + b;           // a.operator+(b)
    c.show();            // (4, 6)
    return 0;
}
```

| Point | Unary | Binary |
|---|---|---|
| Operands | One | Two |
| Member function arguments | None | One |
| Friend function arguments | One | Two |
| Examples | `-a`, `++a`, `!a` | `a + b`, `a == b` |

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. A unary operator is overloaded as a member function. How many arguments does it take?
   A. One
   B. Two
   C. None, because the operand is the calling object
   D. Any number
   Answer: C. For a unary member operator the single operand is the calling object, so no argument is needed. (Key point: Unary member arguments)

2. In c = a + b with operator+ as a member function, which object is the calling object and what is passed?
   A. b is the caller and a is passed
   B. a is the caller and b is passed
   C. c is the caller and a and b are passed
   D. Neither; both are passed as arguments
   Answer: B. a.operator+(b): the left operand calls the function and the right operand is the argument. (Key point: Binary member arguments)

3. How many arguments do friend versions of unary and binary operator functions take?
   A. Unary 0, binary 1
   B. Unary 1, binary 1
   C. Unary 2, binary 2
   D. Unary 1, binary 2
   Answer: D. A friend has no calling object, so every operand must be passed: one for unary, two for binary. (Key point: Friend argument count)

4. In the example, what does -p do to Point p(3, -4) when operator-() negates x and y?
   A. It gives (3, -4) unchanged
   B. It gives (-3, 4)
   C. It gives (-3, -4)
   D. It gives (3, 4)
   Answer: B. Both x and y change sign, so (3, -4) becomes (-3, 4). (Key point: Unary example effect)

## Related in Object Oriented Programming

- [Syntax of operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoperator-overloading-syntax) — Object Oriented Programming, chapter 5, asked 4×
- [City class with operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acity-class-operator-overload) — Object Oriented Programming, chapter 5
- [Binary operator member function one argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abinary-member-one-argument) — Object Oriented Programming, chapter 5
- [Operator overloading: need and definition](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoperator-overloading-why-define) — Object Oriented Programming, chapter 5, asked 3×
- [Disadvantage of operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoperator-overloading-disadvantage) — Object Oriented Programming, chapter 5
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3

## Related in other subjects

- [Operators; conditional operator](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aoperator-conditional) — Computer Programming, chapter 3
