โ† All CPP Flashcard Decks

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
  1. 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++.

  2. 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.

  3. 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`.

  4. 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++()`.

  5. 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.

  6. 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.

  7. 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.