1
h02
CS24 S17
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h02: Chapter 2.3-2.5

ready? assigned due points
true Wed 04/12 05:00PM Wed 04/19 05:00PM 26

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the three lowest scores (if you have zeros, those are the three lowest scores.)


Please:

  • No Staples.
  • No Paperclips.
  • No folded down corners.

Complete your reading of Chapter 2, section 2.3 - 2.5 (If you don’t have a copy of the textbook yet, there is one on reserve at the library under “COMP000-STAFF - Permanent Reserve”).

    1. (2 pts) Why should you use namespaces in your C++ programs?
    2. (6 pts) Consider the point class defined and implemented on pages 64 and 65. Then, answer the following questions:
    a. Describe the value semantics of the point class.
    b. What is the output of the following code? Assume the code is embedded in a correct C++ program.
    point p1, p2(20.0), p3(50,60); 
    cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
    cout<<p2.get_x()<<" "<<p2.get_y()<<endl;
    cout<<p3.get_x()<<" "<<p3.get_y()<<endl;
    p1 = p3;
    p1.rotate90();
    cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
    
    c. Is the default copy constructor invoked in the above code?
    3. (2 pts) On page 72, why are the parameters of the distance() function of type const reference?
    2
    h02
    CS24 S17
    4. (8 pts) For the point class on page 64 and 65, implement the overloaded parameter '*', so that the operator can be used to scale the x and y values of a point by a fixed number. Usage: point p2 = 2.0*p1; In this example p2's x and y coorinates should be double that of p1. Specify whether you would implement the operator as a member function, a non-member function or a friend function, and why?
    5. (8 pts) For the point class on page 64 and 65, implement the overloaded parameter '+=', so that for two point objects p1 and p2, p1+=p2, produces the effect of adding p2's x and y member variables to the corresponding member variables of p1, and store the resulting values back into p1. Usage: p1+=p2; Specify whether you would implement the operator as a member function, a non-member function or a friend function, and why?