Section 7 : Operator Overloading Flashcards
(35 cards)
What does a = b + c; really mean in C++ (under operator overloading)?
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
What does b++ mean?
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
What does ++b mean?
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
What does the expression a = b > c ? b : c; mean?
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
Which operators cannot be overloaded in C++?
❌ 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.
What is a member function overload in C++?
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.
What is a global function overload?
It is an operator function defined outside of the class (often marked friend).
It takes two parameters — the left and right operands.
When must you use a global function to overload an operator?
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 does the compiler rewrite operator calls?
Member form: a + b → a.operator+(b)
Global form: a + b → operator+(a, b)
What’s the function signature for each overload type?
Member:
class MyClass {
MyClass operator+(const MyClass& rhs);
};
Global:
class MyClass {
friend MyClass operator+(const MyClass& a, const MyClass& b);
};
Can a global function access private members of a class?
Only if it is declared a friend inside the class.
Otherwise, it cannot access private or protected members.
When should you prefer member function over global?
When the left-hand operand is always your class type and you want tighter encapsulation.
Why should we avoid passing objects like Vector by value in an overloaded operator?
Because it creates a copy of the object, which can be expensive for large objects and slow down the program.
What’s the best way to pass an object to an overloaded operator?
Pass it as a const reference (e.g., const Vector& other) to avoid copying and prevent unintended modification.
Why add const when passing by reference in operator+?
To protect the passed object from being accidentally modified inside the operator function.
Why do we need a default constructor when overloading operator+?
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.
What happens inside the overloaded operator+ for vectors?
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.
What does this overloaded operator* do in the Vector class?
int operator*(const Vector& b) const {
return x * b.x + y * b.y;
}
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
How can you overload the ~ operator to swap a vector’s x and y?
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
Does overloading change the precedence or associativity of operators?
No. Precedence and associativity are fixed by the C++ language and cannot be changed by overloading.
Why do some operator overloads return by reference?
To allow chaining (e.g., a = b = c;) and to avoid copying. This is common in operator=, operator++, etc
How did the professor overload ~ to swap x and y?
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); }
Why should we pass objects by const reference in overloaded operators?
To avoid unnecessary copying and to protect the object from being accidentally modified. The professor showed this with operator+(const Vector& B) const.
How do you overload»_space; and «_space;to allow cin»_space; v; and cout «_space;v;?
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»_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»_space; v.x»_space; v.y;
return is;
}
Step 3: Overload «_space;for Output
Same rule — cout is not your object, so define it globally:
std::ostream& operator«(std::ostream& os, const Vector& v) {
os «_space;”(“ «_space;v.x «_space;”, “ «_space;v.y «_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; }