1 |
h03 |
CS24 S19 |
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 | Wed 04/17 02:00PM | Tue 04/23 09: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 lowest scores (if you have zeros, those are the 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 write two ways in which this can be done for the Quadratic class covered in lecture. You can find the code for that class here: https://github.com/ucsb-cs24-s19-mirza/cs24-s19-lectures/blob/master/lec-03/quadratic.cpp
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, what is the syntax/logic error. 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;
}