Function and Operator Overloading in C++ - Prof. Keith E. Hellman, Exams of Computer Science

Lecture questions related to function and operator overloading in c++, including true or false questions, function prototypes, and a c++ class representing complex numbers with operator overloading. It also includes a global output operator and a friend input operator for the complex class.

Typology: Exams

Pre 2010

Uploaded on 08/19/2009

koofers-user-0tf
koofers-user-0tf 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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++.
Solution: Addition, subtraction, multiplication, division, modulus, input (
<<
), or output (
>>
).
2. When binary operators are overloaded by global functions:
(a) True or False: Both operands must be of the same class.
Solution: False.
(b) True or False: The calling object is on the left of the operation symbol.
Solution: False. There is no calling object for global operators.
(c) True or False: The operator should return one of the arguments provided.
Solution: False. Only the case for
<<
or
>>
.
(d) True or False: The operator must be written in some class’ scope.
Solution: False. The function is global, it cannot be in any class’ scope.
pf3
pf4
pf5

Partial preview of the text

Download Function and Operator Overloading in C++ - Prof. Keith E. Hellman and more Exams Computer Science in PDF only on Docsity!

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++.

Solution: Addition, subtraction, multiplication, division, modulus, input (<<), or output (>>).

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

Solution: False.

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

Solution: False. There is no calling object for global operators.

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

Solution: False. Only the case for << or >>.

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

Solution: False. The function is global, it cannot be in any 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.

Solution: friend int operator%( const myClass&, int rhs );

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

Solution: In myClass.h, within the class declaration (required since it is a friend function).

(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.

Solution: myClass operator*( int lhs, const myClass& rhs );

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

Solution: In myClass.h but outside of the class declaration (required since it is not a friend function).

(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.

Solution: myClass a ; int b ; int i = a % b ; myClass c = i * a ;

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

Solution: A class declaration.

  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.

Solution: C — the only way to be absolutely sure is to see the myClass declaration.

Solution:

include < iostream >

using namespace std ; / ∗ ∗∗ ∗ A c o m p l e x number c l a s s w i t h o p e r a t o r o v e r l o a d i n g. We make l i f e s i m p l e , andp u t e v e r y t h i n g i n t o p u b l i c so we don ’ t hav e t o w r i t e 4 s e p a r a t e a c c e s s o rf u n c t i o n s. ∗ ∗ PLEASE t h i n k a b o u t t h i s :1. T h e r e a r e no v a l u e s f o r r e a l and i m ag t h a t c r e a t e an I N V A LI D c o m p l e xnumber.2. The c o m p l e x c l a s s does n ’ t hav e any d a t a members w i t h DEPENDENCE on anyo t h e r d a t a members. T h e r e i s noCONSISTENCYwe m us t m a i n t a i n! So wedon ’ t need t o make. r e a l and. i m ag p r i v a t e! ∗ ∗ I hav e a r b i t r a r i l y c hoos en t o make o p e r a t o r + and o p e r a t o rf r i e n d f u n c t i o n s. ∗ ∗ / class complex { public : double real , imag ; complex (); complex ( double r , double i ); friend complex operator +( const complex & lhs , const complex & rhs ); friend complex operator *( const complex & lhs , const double & rhs ); };

/ / G l o b a l o u t p u t f u n c t i o n −−− my math r e f e r e n c e us es ( , ) n o t a t i o n. / / S i n c e e v e r y t h i n g i s p u b l i c , we don ’ t need t o f r i e n d t h i s. ostream & operator < <( ostream & os , const complex & rhs ) { return os << " ( " << rhs. real << " ," << rhs. imag << " ) " ; } / / F o r c o m p l e t e n e s s , s u p p o r t a l p h az complex operator *( const double lhs , const complex & rhs ) { / / r e t u r n an anonymous o b j e c t return complex ( lhs * rhs. real , lhs * rhs. imag ); }

/ / c t o r s complex :: complex ( double r , double i ) : real ( r ) , imag (i ) { /em pt y/ } complex :: complex ( ) : real (0) , imag (0) { /em pt y/ }

/ / ops complex operator +( const complex & lhs , const complex & rhs ) { / / r e t u r n an anonymous o b j e c t return complex ( lhs. real + rhs. real , lhs. imag + rhs. imag ); }

complex operator *( const complex & lhs , const double & rhs ) { / / r e t u r n an anonymous o b j e c t return complex ( lhs. real * rhs , lhs. imag * rhs ); }