



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




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.
Solution: Addition, subtraction, multiplication, division, modulus, input (<<), or output (>>).
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.
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 ;
Solution: A class declaration.
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:
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 , and ∗ p 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 r ∗ f 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 x ∗ number. ∗ 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 any ∗ o t h e r d a t a members. T h e r e i s no ∗ CONSISTENCY ∗ we m us t m a i n t a i n! So we ∗ don ’ 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 r ∗ f 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 a ∗ z 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 ); }