Mastering C++: A Comprehensive Quiz Based on 'Thinking in C++'

Disable ads (and more) with a membership for a one time $2.99 payment

Test your C++ skills with our quiz based on Bruce Eckel's 'Thinking in C++'. Dive into object-oriented programming, advanced topics, and fundamentals. Perfect for learners and experts alike. Assess your knowledge and become a C++ master!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


What is the correct way to overload the '+' operator for a class as a member function?

  1. Integer operator+(const Integer&);

  2. const Integer operator+(const Integer&) const;

  3. static Integer operator+(const Integer&);

  4. Integer& operator+(const Integer&) const;

The correct answer is: const Integer operator+(const Integer&) const;

The operator+ should be overloaded as a const member function as it prevents modification of the current object's state. Option A is incorrect as the first parameter of an overloaded operator+ should be a reference to an object of the same class. Option C is incorrect as static member functions cannot be called on objects. Option D is incorrect as it returns a reference to an integer, whereas the operator+ should return the modified object. Therefore, option B is the correct way to overload the '+' operator for a class as a const member function.