1 |
h03 |
CS24 W18 |
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" |
h03: Chapter 3, section 3.1, Chapter 4, 4.1 - 4.5
ready? | assigned | due | points |
---|---|---|---|
true | Tue 04/17 11:00AM | Tue 04/24 11:00AM |
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 3, section 3.1, Chapter 4, 4.1 - 4.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. (10 pts) What is the output of the following code:
int *p, *q;
int x;
p = &x;
q = new int;
*p = 50;
*q = 60;
cout << *p << ", " << *q << endl;
p = q;
cout << *p << ", " << *q << endl;
*p = 30;
*q = 40;
cout << *p << ", " << *q << endl;
delete p;
delete q;
after the last line of the code in the previous question, how would the behavior of the program change?
3. (10 pts) On page 98 (Chapter 3), the author talks about value semantics, which specifies different ways of copying the value of objects of a class to other objects of the same class. Using examples list two ways in which this can be done for the point class covered in lecture. You can find the code for that class here: https://github.com/ucsb-cs24-w18/cs24-w18-lecture-04
4. Read the code provided in pointCloud.h in point.h in this repo that describes a point and a pointCloud: https://github.com/ucsb-cs24-w18/cs24-h03. Answer the following questions about the provided code
i. (5 pts) Suppose the parametrized constructor was implemented as below, identify the syntax/logic error and provide a correct implementation.
pointCloud::pointCloud(int cap = 10){
cloud = new point[capacity];
}
void whatsThePoint(){
pointCloud *p = new pointCloud;
for (int i = 0;i < 20; i++){
p->insert(point(i, 10*i));
}
pointCloud q(*p);
pointCloud w;
w = q;
delete p;
}