1 |
h02 |
CS32 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: Scoping
ready? | assigned | due | points |
---|---|---|---|
true | Thu 04/20 09:30AM | Mon 04/24 09:30AM |
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.)
Reading: Scoping, PS 10.2
- According to Savitch in PS9, the scope resolution operator
::
and the dot operator.
have a similar purpose, but there is a major difference between them.- (5 pts) What do they have in common?
- (5 pts) What is the difference between them?
- In a class for an object representing a UCSB student:
- (5 pts) Would
setPerm
be an accessor or a mutator function? Circle one: accessor mutator - (5 pts) Would
getName
be an accessor or a mutator function? Circle one: accessor mutator
- (5 pts) Would
- (5 pts) What does the term encapsulation refer to?
- (5 pts) According to Savitch in PS9, it is normal practice to make member functions private in what circumstance?
On p. 579 and then again on p. 586 Savitch drives home a point about the syntax of invoking a constructor—what you SHOULD do when invoking a no-arg constructor, and what you should NOT do. Though he doesn't mention it, this is a "trap" that many C++ learners fall into if they learned Java first, because this is a place where C++ and Java syntax differ significantly.
Suppose you have a no-arg constructor for a classStudent
.- (5 pts) What is the correct syntax to declare a local variable inside a function or method called
s
that is of typeStudent
, and is created with the no-arg constructor? - (5 pts) What is the "wrong" syntax for doing that same thing that Savitch specifically warns against doing?
- (5 pts) In one of the passages where this is explained, Savitch indicates WHY this other syntax is wrong if your intention is to make
s
an instance of classStudent
. This syntax actually has a completely different meaning in C++. What does this alternate syntax mean—that is, what woulds
be declared to be under this alternate syntax?}
- (5 pts) What is the correct syntax to declare a local variable inside a function or method called
-
private: int perm; std::string name;
(5 pts) Suppose you have a class
Student
with private data members as shown in the box at right. You could write a constructor like this:Student::Student(int thePerm, std::string theName) { perm = thePerm; name = theName; }
However, there is an alternate way to initialize data members in the so called "constructor initialization section" described in Section 10.2 of PS9. Rewrite this constructor so that the body is an empty set of braces, and the code is moved to the so-called "constructor initialization section". (Note that I am not referring to the C++11 member initializers described on pp 587-588; this is a feature that has been in C++ for many years and is described somewhere before p. 587.)