Download Operator Overloading in C++: Understanding Member, Non-member, and Friend Functions and more Quizzes Computer Science in PDF only on Docsity! Name: Joshua Byers Email: jbyers4@fau.edu Znumber: 15166001 Lab Title: 12 Due Date: 4/8/11 Answers: Member operator overloading Question 1: Referring to the program in Step 1, if the arity of the “+” operator is two, why is there only one formal parameter in operator+’s function header? There is only one because the current object is passed implicitly through the this pointer Question 2: What is the return type of the operator+ function? Bank_Acct & Question 3: Referring to the operator+ function, what is the name of the Bank_Transaction object it returned? my_acct Question 4: Why is there a “Bank_Transaction::” prefixed to the header of the operator+ function? It is prefixed to the header to specify that the operator overload function is a member of the bank_Acc class. Question 5: Can you explain how chaining is performed for the operator+ function in the program in Step 1. The this pointer points to the current object which is retuned by the overload function Question 6: Please explain why the operator “<<” cannot be overloaded as a member function? (hint: modify the code that was used in the laboratory “Classes: Part 2” to overload the “<<” operator as a friend member function.) It cant be overloaded because there needs to be a stream P a g e | 2 Non-member operator overloading Question 1: Please explain how chaining works? (hint: look at the cout statement in main) It works starting off from the furthest right operator and then works two operands at a time until it reaches the end of the operands on the left Question 2: What is the arity of the “<<” operator in the program? two Question 3: What is the return type of the operator<< function in the program? Ostream & Question 4: What is the purpose of the Print function in the program? Displays the name of the object and final balance after chaining. Question 5: Why isn’t there a “Bank_Transaction::” prefixed to the header of the operator<< function? Its not a member function Question 6: What are the similarities and differences you observed in overloading the operator<< as a friend function and as a non-member function of a class? A friend function is a nonmember function but a nonmember function is not necessarily a friend function. Also friend functions can communicate to the class but nonmember functions cant.