---
title: "Syntax of operator overloading"
subject: "Object Oriented Programming"
chapter: 5
key: "semester_3/object_oriented_programming:operator-overloading-syntax"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/operator-overloading-syntax
site: BCT Engineering
---

# Syntax of operator overloading

Object Oriented Programming · Chapter 5: Operator Overloading

- Asked in: 2073 Shrawan, 3 marks; 2072 Kartik, 3 marks; 2070 Asar, 4 marks; 2068 Baisakh, 2 marks (asked 4 times)
- Video: [Operator Overloading in C++ Programming | C++ Programming for Beginners](https://www.youtube.com/watch?v=BO2KagRMS3M) — Simple Snippets
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/operator-overloading-syntax
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-5

## Question

Write down the syntax of operator overloading for various cases.

## Answer

Operator overloading is done by writing an **operator function** with the keyword `operator` followed by the operator symbol. General form:

```cpp
return_type operator op (argument_list) { body }
```

#### 1. Unary operator as member function (no argument)

```cpp
return_type ClassName::operator-() { ... }     // used as  -a;  calls a.operator-()
```

#### 2. Binary operator as member function (one argument)

```cpp
return_type ClassName::operator+(ClassName b) { ... }   // c = a + b;  calls a.operator+(b)
```
The left operand is the invoking object; the right operand is the argument.

#### 3. Unary operator as friend function (one argument)

```cpp
friend return_type operator-(ClassName a);    // -a;  calls operator-(a)
```

#### 4. Binary operator as friend function (two arguments)

```cpp
friend return_type operator+(ClassName a, ClassName b);   // a + b
```
Needed when the left operand is a built-in type, e.g. `5 + obj`.

#### 5. Prefix and postfix increment/decrement

```cpp
ClassName operator++();        // prefix  ++a
ClassName operator++(int);     // postfix a++  (dummy int distinguishes it)
```

#### 6. Stream insertion and extraction (always non-member, usually friend)

```cpp
friend ostream& operator<<(ostream &out, ClassName &obj);
friend istream& operator>>(istream &in, ClassName &obj);
```

#### 7. Subscript operator

```cpp
int& operator[](int index);
```

#### 8. Assignment operator (must be a member)

```cpp
ClassName& operator=(const ClassName &rhs);
```

Rules: `=`, `[]`, `()` and `->` can be overloaded only as member functions. At least one operand must be a user-defined type.

## Check yourself

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

1. What is the general form of an operator function?
   A. return_type operator op (argument_list)
   B. operator return_type op
   C. op operator()
   D. return_type op operator
   Answer: A. The keyword operator followed by the symbol is used. (Key point: General form)

2. How is postfix ++ distinguished from prefix?
   A. A dummy int parameter
   B. Different name
   C. const keyword
   D. Return type void
   Answer: A. operator++(int) is postfix; the int is a dummy. (Key point: Postfix distinction)

3. How must << be overloaded for an ostream?
   A. As a member of the class
   B. As a non-member (usually friend)
   C. As static
   D. It cannot be
   Answer: B. The left operand is cout, so it can't be a member of the class. (Key point: Stream operators)

4. Which operator must be overloaded as a member function?
   A. +
   B. <<
   C. =
   D. ==
   Answer: C. =, [], () and -> can only be overloaded as members. (Key point: Member only)

## Related in Object Oriented Programming

- [Binary and unary operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abinary-unary-overloading) — Object Oriented Programming, chapter 5
- [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
- [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
- [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
- [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
- [Bus service use cases with elaboration](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Agre-bus-service-use-cases) — Object Oriented Analysis and Design, chapter 2
