Operator Overloading & Type Conversions Flashcards
7 cards from real CPP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Operator Overloading & Type Conversions flashcards as text
Which keyword is used to define an overloaded operator in C++?
Answer: operator
The `operator` keyword followed by the operator symbol (e.g., `operator+`) defines an overloaded operator in C++.
Which of the following operators CANNOT be overloaded in C++?
Answer: operator::
The scope resolution operator (::), dot operator (.), sizeof, typeid, and ternary (?:) cannot be overloaded.
What is the conventional return type for the copy assignment operator (operator=)?
Answer: T&
The assignment operator should return `T&` (reference to *this) to support chaining such as `a = b = c`.
How is the postfix increment operator (++) differentiated from the prefix version when overloading?
Answer: Postfix takes a dummy int parameter
The postfix version is declared as `operator++(int)` with a dummy int parameter to distinguish it from the prefix `operator++()`.
Which of the following operators MUST be overloaded as a member function?
Answer: operator=
The assignment (=), subscript ([]), call (()), and arrow (->) operators must be defined as member functions per the C++ standard.
What does the `explicit` keyword do when applied to a single-argument constructor?
Answer: Prevents implicit conversions using that constructor
`explicit` prevents the compiler from using that constructor for implicit type conversions, requiring an explicit cast instead.
When overloading the output stream operator (operator<<), what should be the first parameter type?
Answer: std::ostream&
The first parameter must be `std::ostream&` (a non-const reference) so the stream state can be updated and chaining with << is supported.