Suppose there is a class named Office with member variable num_of_desks: class Office \{ private: int num_of_desks; This class is supposed to have an overloaded ++ (increment) operator to increment num_of_desks for any Office object. Which of the following options below is the correct prefix + operator implemented as a friendfunction?
Real Tutor Solution
Answer
Solution
Sign in to Unlock Answers for Free!
A Learning Platform Trusted by Millions of Real Students and Teachers.
Reviewed and approved by the UpStudy tutoring team
The Deep Dive
Here's how you might implement the prefix ++ operator as a friend function for the Office class: ```cpp class Office { private: int num_of_desks; public: Office(int desks) : num_of_desks(desks) {} // Friend function to overload the prefix ++ operator friend Office& operator++(Office& office) { ++office.num_of_desks; // Increment the member variable return office; // Return the updated object } }; ``` In this implementation, the friend function directly accesses the private member variable `num_of_desks`. When you use the prefix increment operator (++office), it increases the number of desks by one and returns a reference to the same Office object allowing chained operations if needed.