Section 7 : Operator Overloading Flashcards

(35 cards)

1
Q

What does a = b + c; really mean in C++ (under operator overloading)?

A

It can be rewritten in two ways:

a = b.operator+(c); → if + is a member of b

a = operator+(b, c); → if + is a global function

These are function-call equivalents of the + operator expression.

Member Function Form (if + is overloaded inside a class):
a = b.operator+(c); // b is the object, c is the argument

Global Function Form (if + is overloaded globally):
a = operator+(b, c); // both are arguments, not methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does b++ mean?

A

It’s the postfix increment. It increases the value of b by 1, but returns the original value first.
int b = 5;
int a = b++; // a = 5, b = 6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does ++b mean?

A

It’s the prefix increment. It increases the value of b by 1 first, then returns the updated value.

int b = 5;
int a = ++b; // b = 6, a = 6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the expression a = b > c ? b : c; mean?

A

It’s a ternary conditional operator. It assigns a to the value of b if b > c, otherwise to c.

a = (b > c) ? b : c;
// if (b > c) → a = b;
// else → a = c;

ternary expression cannot be overloaded

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which operators cannot be overloaded in C++?

A

❌ The following operators cannot be overloaded:

?: → Ternary conditional operator

. → Member access (dot operator)

:: → Scope resolution

sizeof → Size-of-type or object at compile time

.* → Pointer-to-member access

alignof, noexcept, const_cast, static_cast, etc.

✅ All other operators (like +, -, [], (), etc.) can be overloaded.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a member function overload in C++?

A

It is an operator function defined inside a class.
It takes one parameter (the right-hand side).
The left-hand side (A in A + B) is the object calling the function — i.e., this.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a global function overload?

A

It is an operator function defined outside of the class (often marked friend).
It takes two parameters — the left and right operands.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When must you use a global function to overload an operator?

A

When the left-hand operand is not your class type — like in 3 + v.(v is a vector here for example)
You can’t define a member function on a built-in type like int.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does the compiler rewrite operator calls?

A

Member form: a + b → a.operator+(b)

Global form: a + b → operator+(a, b)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What’s the function signature for each overload type?

A

Member:
class MyClass {
MyClass operator+(const MyClass& rhs);
};

Global:
class MyClass {
friend MyClass operator+(const MyClass& a, const MyClass& b);
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can a global function access private members of a class?

A

Only if it is declared a friend inside the class.
Otherwise, it cannot access private or protected members.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When should you prefer member function over global?

A

When the left-hand operand is always your class type and you want tighter encapsulation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why should we avoid passing objects like Vector by value in an overloaded operator?

A

Because it creates a copy of the object, which can be expensive for large objects and slow down the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What’s the best way to pass an object to an overloaded operator?

A

Pass it as a const reference (e.g., const Vector& other) to avoid copying and prevent unintended modification.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why add const when passing by reference in operator+?

A

To protect the passed object from being accidentally modified inside the operator function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why do we need a default constructor when overloading operator+?

A

Because we create a new Vector object (like Vector C;) to hold the result, and this requires a way to initialize it — which a default constructor provides.

17
Q

What happens inside the overloaded operator+ for vectors?

A

It computes:

C.x = A.x + B.x;
C.y = A.y + B.y;
return C;

Where C is a new object, A is the calling object (this), and B is the parameter.

18
Q

What does this overloaded operator* do in the Vector class?
int operator*(const Vector& b) const {
return x * b.x + y * b.y;
}

A

It calculates the dot product (scalar product) of two 2D vectors:

This function:

Takes another Vector by const&

Does not modify either vector (marked const)

Returns an int result

Vector A(2, 3), B(4, 5);
int dot = A * B; // returns 24 + 35 = 8 + 15 = 23

19
Q

How can you overload the ~ operator to swap a vector’s x and y?

A

Two ways:

Non-destructive (returns new vector):

Vector operator~() const {
return Vector(y, x);
}
Destructive (modifies object in-place):

void operator~() {
std::swap(x, y);
}
Use the first when you want to assign the result.
Use the second when you want to mutate the vector directly

20
Q

Does overloading change the precedence or associativity of operators?

A

No. Precedence and associativity are fixed by the C++ language and cannot be changed by overloading.

21
Q

Why do some operator overloads return by reference?

A

To allow chaining (e.g., a = b = c;) and to avoid copying. This is common in operator=, operator++, etc

22
Q

How did the professor overload ~ to swap x and y?

A

Return a new object with swapped values:
Vector operator~() const { return Vector(y, x); }

Modify the object in-place:
void operator~() { std::swap(x, y); }

23
Q

Why should we pass objects by const reference in overloaded operators?

A

To avoid unnecessary copying and to protect the object from being accidentally modified. The professor showed this with operator+(const Vector& B) const.

24
Q

How do you overload&raquo_space; and &laquo_space;to allow cin&raquo_space; v; and cout &laquo_space;v;?

A

Use global functions:

std::istream& operator»(std::istream& is, Vector& v);
std::ostream& operator«(std::ostream& os, const Vector& v);

These let your class behave like a built-in type for input and output.

In Steps :

🧩 Step 1: Define the Vector class
class Vector {
public:
int x, y;

Vector(int x = 0, int y = 0) : x(x), y(y) {} };

Step 2: Overload&raquo_space; for Input
This should be a global function, not a member, because the left-hand operand is cin, not your class.

std::istream& operator»(std::istream& is, Vector& v) {
is&raquo_space; v.x&raquo_space; v.y;
return is;
}
Step 3: Overload &laquo_space;for Output
Same rule — cout is not your object, so define it globally:

std::ostream& operator«(std::ostream& os, const Vector& v) {
os &laquo_space;”(“ &laquo_space;v.x &laquo_space;”, “ &laquo_space;v.y &laquo_space;”)”;
return os;
}
Step 4: Use it in main()
int main() {
Vector v;

std::cout << "Enter vector components (x y): ";
std::cin >> v;

std::cout << "You entered: " << v << std::endl;

return 0; }
25
What does it mean to overload an operator?
Operator overloading is a form of compile-time polymorphism. It allows giving special meaning to an existing operator for a user-defined type without changing its original purpose for built-in types.
25
What makes an operator function different from a regular function?
It has the name format operator (e.g., operator+) and is called automatically when the corresponding operator is used in code.
25
How do you define a function to overload the + operator for a class Complex?
Complex operator+(const Complex& obj);
25
What must be true about operands for operator overloading to work?
At least one operand must be a user-defined class object. You cannot overload operators for only built-in types like int + float.
25
Can an operator be overloaded multiple times?
✅ Yes, as long as the parameter list is different (i.e., function signature is unique).
25
How is + overloaded for a Complex class?
Complex operator+(const Complex& obj) { return Complex(real + obj.real, imag + obj.imag); } Called as: c1 + c2 → c1.operator+(c2)
26
When should you define a global operator overload?
When the left-hand operand is not your class type, e.g., 2 * Vector.
27
How is scalar-vector multiplication defined using a global function?
Vector operator*(int num, const Vector& A) { return Vector(num * A.x, num * A.y); }
28
Why can't we use a member function to define 2 * Vector?
Because 2 is a built-in type, and you can't call methods on integers. So the operator must be defined globally.
29
Why do we need two definitions of the * operator to support both A * 2 and 2 * A?
Because: A * 2 → works with a member function, since A is a Vector 2 * A → needs a global function, since 2 is an int (and ints can’t call methods) ✅ So we define: // Member function inside class (Vector * int) Vector operator*(int num) const; // Global function outside class (int * Vector) Vector operator*(int num, const Vector& v) { return v * num; // reuse the member function } This lets you write both A * 2 and 2 * A, and only write the logic once.
30
Why should the operator<< (output stream) function return std::ostream& instead of void?
Returning void allows only one-time output. But in C++, we often write chained output like: cout << a << b << c; If operator<< returned void, this would break, because you couldn't apply another << to the result (you can’t do void << b). ✅ To enable chaining, we must return a reference to the output stream: std::ostream& operator<<(std::ostream& os, const MyClass& obj); This way: The first call cout << a returns a reference to cout Then cout << b works And so on... 🗣️ As the professor said: "It will not work with void if you make a chain of outputs. Try it — it will fail." void version ✅ cout << a; → prints 5 ❌ cout << a << b; → compile error because no chaining is possible