Docsity

CSCI261 Lecture Questions #1: Function and Operator Overloading - Prof. Keith E. Hellman, Exams of Computer Science

Toggle sidebar
bg1
CSCI261 Lecture Questions #1 Function and Operator Overloading
This worksheet is for your use during and after lecture. It will not be collected or graded, but I think you will find it a
useful tool as you learn C++ and study for the exams. Explain all false answers for the “True or False” questions; in
general, show enough work and provide enough explanation so that this sheet is a useful pre-exam review. I will be
happy to review your answers with you during office-hours, via Email, or instant messaging.
1. Name six binary operators in C++.
2. When binary operators are overloaded by global functions:
(a) True or False: Both operands must be of the same class.
(b) True or False: The calling object is on the left of the operation symbol.
(c) True or False: The operator should return one of the arguments provided.
(d) True or False: The operator must be written in some class’ scope.
3. (a) Write the protoype of a
friend
global operator for taking the modulus of a
myClass
object (LHS) and an
integer (RHS). It should return an integer value.
(b) Where would you find the answer to part awithin
myClass
’ source files?
(c) Write the protoype of a global operator for multiplying an integer (LHS) with a
myClass
object (RHS). It
should return a new
myClass
object.
(d) Where would you find the answer to part cwithin
myClass
’ source files?
(e) Assume
myClass
has a default constructor. Write a snippet of C++ that would invoke the two global operators
prototyped in parts aand c.
4. The
friend
keyword may occur in C++ source only between the curly braces of what?
pf2

Partial preview of the text

Download CSCI261 Lecture Questions #1: Function and Operator Overloading - Prof. Keith E. Hellman and more Exams Computer Science in PDF only on Docsity!

CSCI261 Lecture Questions #1 Function and Operator Overloading

This worksheet is for your use during and after lecture. It will not be collected or graded, but I think you will find it a useful tool as you learn C++ and study for the exams. Explain all false answers for the “True or False” questions; in general, show enough work and provide enough explanation so that this sheet is a useful pre-exam review. I will be happy to review your answers with you during office-hours, via Email, or instant messaging.

  1. Name six binary operators in C++.
  2. When binary operators are overloaded by global functions: (a) True or False: Both operands must be of the same class.

(b) True or False: The calling object is on the left of the operation symbol.

(c) True or False: The operator should return one of the arguments provided.

(d) True or False: The operator must be written in some class’ scope.

  1. (a) Write the protoype of a friend global operator for taking the modulus of a myClass object (LHS) and an integer (RHS). It should return an integer value.

(b) Where would you find the answer to part a within myClass’ source files?

(c) Write the protoype of a global operator for multiplying an integer (LHS) with a myClass object (RHS). It should return a new myClass object.

(d) Where would you find the answer to part c within myClass’ source files?

(e) Assume myClass has a default constructor. Write a snippet of C++ that would invoke the two global operators prototyped in parts a and c.

  1. The friend keyword may occur in C++ source only between the curly braces of what?

CSCI261 Lecture Questions #1 Function and Operator Overloading

  1. The following is the function header of a global operator: int operator-( const myClass& lhs, int rhs ) Is the operator a friend of myClass or not? A. Certainly not. B. Maybe, I could tell for sure if I saw the function implementation. C. Maybe, I could tell for sure if I saw the myClass declaration.
  2. Write the prototype of a global operator<< for myClass.
  3. Write the prototype of a global operator>> that is a friend of myClass. Where is this protoype found?
  4. Consult the appendix of your Mines Calculus book and review the topic of complex numbers. Write a C++ class representing complex numbers with floating point real and imaginary parts. Implement addition ( z 1 + z 2 ), scalar multiplication (α z ), and an output operator that prints the complex number in the same format as your mathematics reference does.

Page 2