Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Operator Overloading in C++: Understanding Member, Non-member, and Friend Functions, Quizzes of Computer Science

An explanation of operator overloading in c++ with a focus on member, non-member, and friend functions. It covers the concept of chaining, arity, return types, and the differences between friend and non-member functions. The document also includes examples and exercises.

Typology: Quizzes

2010/2011

Uploaded on 05/02/2011

mrpaintball1331
mrpaintball1331 🇺🇸

1 document

1 / 3

Related documents


Partial preview of the text

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



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved