Operator Overloading Questions with Answers

28 practice questions on Operator Overloading (C++) with answers and step-by-step solutions. Try 10 samples below, then practise all 28 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

#include <iostream>
using namespace std;

struct Money {
    int rupees;
    friend ostream& operator<<(ostream& os, const Money& m) {
        return os << "Rs." << m.rupees;
    }
};

int main() {
    Money m{250};
    cout << m << "!";
    return 0;
}
  1. Compilation error
  2. Rs.250!
  3. Rs.250
  4. 250!

Answer: B. Rs.250!

Explanation

operator<< returns the stream by reference, so further insertions can be chained after the custom object.

Step-by-step solution

  1. The friend operator<< writes the text Rs. and then m.rupees, which is 250.
  2. It returns the stream, so the "!" from main is printed right after.
  3. The output is Rs.250!: option B.

Why the other options are wrong

  • A. The code is valid: a friend operator<< defined inside the struct compiles.
  • C. The "!" from main is printed too, so the output does not stop at Rs.250.
  • D. The operator prints the text Rs. before the number, so it is not just 250!.

Tip: Returning the ostream by reference lets you chain more << after a custom type.

Question 2Easy

What is the output?

#include <iostream>
using namespace std;

struct Num {
    int v = 0;
    Num& operator=(const Num& o) { v = o.v + 1; return *this; }
};

int main() {
    Num a, b, c;
    c.v = 5;
    a = b = c;
    cout << a.v << b.v << c.v;
    return 0;
}
  1. 665
  2. 555
  3. 765
  4. 675

Answer: C. 765

Explanation

Assignment is right-associative:
b = c gives b.v = 6, then a = b gives a.v = 7.

Returning *this by reference allows the chain.

Step-by-step solution

  1. Assignment is right-associative, so a = b = c is a = (b = c).
  2. b = c calls operator= with c: b.v becomes c.v + 1 = 6, and returns b.
  3. a = b then sets a.v to b.v + 1 = 7.
  4. The values are a 7, b 6 and c 5, so the output is 765: option C.

Why the other options are wrong

  • A. A is 7, not 6, because the operator adds 1 on each assignment.
  • B. The operator adds 1 each time, so a and b are not 5.
  • D. B is 6 and a is 7, so the output is 765, not 675.

Tip: The assignment operator runs once per =, so chained assignments run it several times, right to left.

Question 3Easy

Which of these operators can NOT be overloaded in C++?

  1. () (function call)
  2. << (left shift)
  3. :: (scope resolution)
  4. [] (subscript)

Answer: C. :: (scope resolution)

Explanation

::, ., .*, ?: and sizeof cannot be overloaded. Subscript, function call and shift operators are commonly overloaded.

Step-by-step solution

  1. Most operators can be overloaded, including (), << and [].
  2. A few cannot be overloaded, such as ::, ., .*, ?: and sizeof.
  3. Scope resolution :: cannot be overloaded.
  4. So the answer is option C.

Why the other options are wrong

  • A. The function call operator () can be overloaded.
  • B. The left shift operator << can be overloaded.
  • D. The subscript operator [] can be overloaded.

Tip: Remember the few fixed operators: :: . .* ?: and sizeof.

Question 4Easy

What is the output?

#include <iostream>
using namespace std;

struct Temp {
    int deg;
    Temp operator-() const { return {-deg}; }
};

int main() {
    Temp t{12};
    Temp u = -t;
    cout << t.deg << " " << u.deg;
    return 0;
}
  1. -12 12
  2. 12 -12
  3. -12 -12
  4. Compilation error

Answer: B. 12 -12

Explanation

The unary minus returns a new object and leaves t unchanged because it is a const member function returning by value.

Step-by-step solution

  1. T has deg 12; operator- is a const member that returns a new Temp with deg -12.
  2. U is set to that new object, so u.deg is -12.
  3. T is not changed, so t.deg is still 12.
  4. The output is 12 -12: option B.

Why the other options are wrong

  • A. t.deg is still 12, not -12, because unary minus returns a new object.
  • C. t.deg stays 12; only u gets -12.
  • D. The code compiles, since the unary minus is a valid const member.

Tip: A unary operator that returns a new object leaves the original unchanged.

Question 5Medium

What is the output?

#include <iostream>
using namespace std;

struct Counter {
    int v = 0;
    Counter& operator++() { ++v; return *this; }
    Counter operator++(int) { Counter old = *this; ++v; return old; }
};

int main() {
    Counter c;
    Counter d = c++;
    Counter e = ++c;
    cout << c.v << d.v << e.v;
    return 0;
}
  1. 202
  2. 212
  3. 222
  4. 201

Answer: A. 202

Explanation

The postfix form (with the dummy int) returns the old value, so d.v is 0; the prefix form returns the updated object, so e.v is 2. c is incremented twice.

Step-by-step solution

  1. The postfix operator runs with a dummy int: old is a copy of c with v 0, c.v becomes 1, and d gets old, so d.v is 0.
  2. ++c calls the prefix operator: c.v becomes 2 and returns c itself, so e copies 2.
  3. Printing c.v, d.v and e.v gives 2, 0 and 2.
  4. The output is 202: option A.

Why the other options are wrong

  • B. d.v is 0, not 1, because the postfix form returns the value from before the increment.
  • C. d.v is 0, not 2; only the prefix result e is 2.
  • D. e.v is 2, not 1, because c was incremented twice in total.

Tip: Postfix ++ returns the old value; prefix ++ returns the new value.

Question 6Medium

What is the output?

#include <iostream>
using namespace std;

struct Conn {
    bool up;
    explicit operator bool() const { return up; }
};

int main() {
    Conn a{true}, b{false};
    if (a) cout << "A";
    if (!b) cout << "B";
    cout << (a && b);
    return 0;
}
  1. A0
  2. AB1
  3. Compilation error
  4. AB0

Answer: D. AB0

Explanation

An explicit operator bool is still used in boolean contexts such as if, !, && and ||; it only blocks implicit conversions like int x = a;.

Step-by-step solution

  1. if (a) uses a in a boolean context, which is allowed for an explicit operator bool, so A is printed.
  2. !b also uses b in a boolean context; b is false, so !b is true and B is printed.
  3. A && b converts both to bool: true and false give false, which prints 0.
  4. The output is AB0: option D.

Why the other options are wrong

  • A. B is printed too, because !b is true.
  • B. The last value is 0, because a && b is false.
  • C. The code compiles, because explicit operator bool works in if, ! and && contexts.

Tip: An explicit operator bool is used in if, !, && and || but not in plain assignments.

Question 7Medium

What is the output of this program?

    #include <iostream>
    using namespace std;
    class myclass
    {
        public:
        int i;
        myclass *operator->()
        {return this;}
    };
    int main()
    {
        myclass ob;
        ob->i = 10;
        cout << ob.i << " " << ob->i;
        return 0;
    }
  1. 10 10
  2. 11 11
  3. error
  4. runtime error

Answer: A. 10 10

Explanation

The -> operator is overloaded to return a pointer to the same object. So ob->i sets ob.i to 10, and both values print 10.

10 10

Step-by-step solution

  1. Ob-> calls operator-> on ob, which returns this, a pointer to ob.
  2. Applying -> to that pointer then reaches member i of ob.
  3. So ob->i = 10 sets ob.i to 10.
  4. ob.i and ob->i both give 10, so the output is 10 10.

Why the other options are wrong

  • B. 11 would need an extra increment, which the code does not do.
  • C. The code compiles, since operator-> is defined and returns a pointer.
  • D. There is no runtime error; the pointer is valid.

Tip: An overloaded operator-> that returns a pointer lets -> continue on that pointer.

Question 8Medium

Which of these operators can be overloaded ONLY as non-static member functions?

  1. +, -, * and /
  2. ==, != and <
  3. =, [], () and ->
  4. << and >>

Answer: C. =, [], () and ->

Explanation

The assignment, subscript, function-call and member-access arrow operators must be members. Arithmetic, comparison and stream operators may be members or free functions.

Step-by-step solution

  1. Assignment, subscript, function call and member access arrow must be non-static member functions.
  2. Arithmetic, comparison and stream operators may be members or free functions.
  3. So the operators that must be members are = [] () and ->.
  4. The answer is option C.

Why the other options are wrong

  • A. Arithmetic operators can be free functions, so they do not have to be members.
  • B. Comparison operators can be free functions, so they do not have to be members.
  • D. Stream operators << and >> are usually free functions, so they do not have to be members.

Tip: Only =, [], () and -> must be member functions.

Question 9Hard

What happens when this program is compiled with -std=c++17?

#include <iostream>
using namespace std;

struct Id {
    int v;
    bool operator==(const Id& o) const { return v == o.v; }
};

int main() {
    Id a{1}, b{2};
    cout << (a != b);
    return 0;
}
  1. It prints 1
  2. Compilation error: no operator!= for Id
  3. It prints true
  4. It prints 0

Answer: B. Compilation error: no operator!= for Id

Explanation

In C++17 the compiler does not derive != from ==; each operator must be overloaded separately. (C++20 added rewriting of a != b as !(a == b).)

Step-by-step solution

  1. The class defines only operator== for Id.
  2. In C++17, a != b is not generated from ==, so a separate operator!= is needed.
  3. No operator!= exists, so the compiler rejects a != b.
  4. The answer is option B: a compilation error.

Why the other options are wrong

  • A. The program does not compile, so it does not print 1.
  • C. The program does not compile, so true is never printed.
  • D. The program does not compile, so 0 is never printed.

Tip: Before C++20, define != together with == for class types.

Question 10Hard

What happens when this program is compiled?

#include <iostream>
using namespace std;

struct P {
    int v;
    P operator+(int x) const { return {v + x}; }
};

int main() {
    P p{1};
    P q = p + 2;
    P r = 2 + p;
    cout << q.v << r.v;
    return 0;
}
  1. It prints 30
  2. Compilation error at 2 + p
  3. It prints 33
  4. It prints 3 followed by a garbage value

Answer: B. Compilation error at 2 + p

Explanation

A member operator+ needs the object on the left, so 2 + p finds no match. A non-member operator+(int, const P&) is needed for the reversed order.

Step-by-step solution

  1. Operator+ is a member function, so the left operand must be a P object.
  2. p + 2 matches it, so q is built fine.
  3. 2 + p has an int on the left, and there is no non-member operator+(int, P), so no match exists.
  4. The compiler reports an error at 2 + p: option B.

Why the other options are wrong

  • A. The program does not compile, so nothing is printed.
  • C. The reversed form 2 + p does not compile, so 33 is not printed.
  • D. The program does not compile, so no garbage value is printed.

Tip: A member operator needs the object on the left; write a non-member overload for the reversed order.

Practise all 28 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C++ topics · All topics