1 |
h04 |
CS32 S18 |
Name: | ||||
---|---|---|---|---|
(as it would appear on official course roster) | ||||
Umail address: | @umail.ucsb.edu | |||
Optional: name you wish to be called if different from name above. | ||||
Optional: name of "homework buddy" (leaving this blank signifies "I worked alone" |
h04: Class Design
ready? | assigned | due | points |
---|---|---|---|
true | Tue 04/10 08:00AM | Tue 04/17 12:30PM |
You may collaborate on this homework with AT MOST one person, an optional "homework buddy".
MAY ONLY BE TURNED IN IN THE LECTURE 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 four lowest scores (if you have zeros, those are the four lowest scores).
Reading: Class Design, DS 2 In CMPSC 24, you likely already read parts of Chapter 2. I want to invite you to read it again, for two reasons: (1) to review parts you may have skipped or skimmed over the first time (2) to revisit things that you may have read many weeks or months ago, and that may not have fully made sense to you at the time.
I’ve included some questions here on a few of the important points from Chapter 2, but these are NOT the only things you need from Chapter 2. I don’t have enough room on the page to give you homework over everything from Chapter 2 you need to learn. Read few this material a few times between now and the first exam.
- (10 pts) Fill in the information in the header. The following are required to get the 10 "participation" points.
- Filling in your name and umail address.
Also: For paper submission PLEASE submit on ONE SHEET OF PAPER, double-sided if at all possible. If you must submit on two printed sheets write name on BOTH sheets and no staples, paperclips, or folded corners.
- Filling in your name and umail address.
- According to Chapter 2 of DS:
- (4 pts) In C++ if you write a class, but do not specify any constructors, what happens?
- (4 pts) When you implement a so-called "default constructor" in C++, what specific capability does this give the user of your class? (By specific, I mean what capability does the default constructor give that is distinguished from other kinds of constructors?)
- (4 pts) Suppose you are writing a class called Student that has two private members, 'std::string name' and 'int perm'. Write the function prototype (function declaration) ONLY for a default constructor for Student. For full credit, include the semicolon that terminates the prototype, but DO NOT write the body of the constructor
- Before enrolling in CMPSC 32, you should have learned about stacks in CMPSC 24 (or an equivalent course). I'm going to tell you a few things about an example stack implementation as a C++ class, then ask a few questions—these are designed to check your understanding of some important ideas about ADTs and C++ classes from Chapter 2 of DS.
Suppose we implement a class for a stack of integers, called
If I declare a method with the function prototypeIntStack
. Our implementation uses a variableint stack[MAX_SIZE];
and a variableint size;
. The constantMAX_SIZE
may be declared asconst int MAX_SIZE=100;
, for example. I will have methodspush
,pop
,isEmpty
and since my implementation has a max size,isFull
.bool isEmpty() const;
, I have the option of putting the function body either in a separateIntStack.cpp
file, or directly inside theIntStack.h
file like this:bool isEmpty() const { return size==0; }
- (2 pts) According to C++ recommended practice, in which section of the class should we find the declarations of
int stack[MAX_SIZE];
andint size;
?Circle one: public private
- (2 pts) When we put the method body (e.g.
{return size==0;}
forisEmpty
inside the class declaration (i.e. in theIntStack.h
file) that makesisEmpty
a special type of member function (three word phrase with initials IMF). What does IMF stand for? - (3 pts) What is one thing that is different about IMF from the perspective of how they are treated by the compiler?
- (4 pts) What is the significance of the keyword
const
that appears afterisEmpty
? - (4 pts) Would
const
be appropriate for thepop
operation? Circle one: yes no Briefly explain your answer. - (4 pts) Would
const
be appropriate for theisFull
operation? Circle one: yes no Briefly explain your answer.
- (2 pts) According to C++ recommended practice, in which section of the class should we find the declarations of
- DS Chapter 2 refers to something called a macro guard. This is a set of two pre-processor directives that appear at the start of a file, and one that appears at the end of a file. DS's macro guard for a file
throttle.h
uses a symbolMAIN_SAVITCH_THROTTLE
, but it would be more traditional to use a symbol such as_THROTTLE_H_
(the name of the file, with underscores before, after, and replacing the dot.)- (4 pts) Write a macro guard for a file called
IntStack.h
, using the symbol_INTSTACK_H_
. Put an ellipsis(…)
in the place between the first two lines and the last line. - (5 pts) What is the purpose of a macro guard?
- (4 pts) Write a macro guard for a file called