CS 202 Fall 2013 > Assignment 2 |
CS 202 Fall 2013
Assignment 2
Assignment 2 is due at 2 p.m. Wednesday, September 25. It is worth 30 points.
Procedures
Complete each of the exercises below. Then turn in your work as follows.
- Run Check. Demonstrate the programs you have written for the exercises below to either the instructor (Glenn G. Chappell) or the T.A. (Zak Williams). Get official approval.
- Submission. Submit your code using Blackboard, under Assignment 2 for this class. You should only submit code that has already passed the Run Check. Be sure you attach your source/header files; do not paste them into the text box. Also, send only source & headers; no project files or executables.
We may not look at your homework submission immediately. If you have questions, e-mail me.
Exercises (30 pts total)
Do each of the following exercises.
- Write a minimal version of a class
Wodget
described below. “Minimal” means that all the member functions are there, and the ones that return values have a dummyreturn
statement, but there is no other code written. So most of the member functions should have empty function bodies: “{}”. And there should be no member variables at all.Class
Wodget
should be implemented in fileswodget.h
andwodget.cpp
. It should follow standard conventions regarding the construction of header and source files. It should have the following member functions.- A default constructor.
- A constructor taking two parameters:
a
string
and anint
. - Member function
cat
taking anint
parameter and returning nothing. - Const member function
dog
taking twoint
parameters and returning anint
. - Const member function
llama
taking one parameter, avector
ofWodget
objects, and returning nothing. - Accessor and mutator functions for a presumed member variable
cow
of typestring
. These should be namedgetCow
andsetCow
and should be written as usual.
Demonstrate your class by compiling it with program
wodget_main.cpp
and executing the result. Do not modify filewodget_main.cpp
.
- Write a class
Brackets
with the following threepublic
member functions.- Const member function
hello
. This should print “[Hello!]
” tocout
, followed by a newline. - Const member function
dollars
. This takes anint
parameter, and prints that many dollar signs between brackets tocout
, followed by a newline. For example, calling functiondollar
with parameter4
would print “[$$$$]
”. - Const member function
message
. This should take astring
parameter. It should print the string between brackets tocout
, followed by a newline. For example, calling functiondollar
with parameter"boat"
would print “[boat]
”.
(Just in case it is not clear, these are brackets: “
[]
”. These are not: “(){}<>
”.)The common functionality in these three member functions should be implemented in a
private
member functionoutput
which all three of them call. In particular, member functionoutput
should be the only member function that does any printing.Demonstrate your class by writing a program that uses it and calls all three public member functions.
- Const member function
- Finish program
print_from_ptrs.cpp
by writing functionprintAll
. This function takes avector
of(PrintSomething *)
. It returns nothing. Each pointer in thevector
can be assumed to point to aPrintSomething
object. The function is to go through thevector
, in order. For each pointer, it should call member functiondoPrint
on the object the pointer points to.Do not modify any other part of program
print_from_ptrs.cpp
.
- Write a class
Greeter
with the following member functions.- Default constructor.
This should generate a random number from
1
to1000
(userand
?) and save this number in a member variable. The constructor should print “Hello #
” followed by the number and a newline tocout
. - Destructor.
This should
print “
Goodbye #
” followed by the saved number and a newline tocout
.
So, for example, here is a program that uses class
Greeter
.[C++]
#include "greeter.h" int main() { Greeter x; return 0; }
This program would produce output something like the this.
Hello #328 Goodbye #328
Demonstrate class
Greeter
by writing a program that does the following.- Ask the user how many objects to create.
- Create a
vector
containing that manyGreeter
objects. - End.
The program should do nothing else. (This ought to result in a number of calls to the
Greeter
default constructor and destructor, with the result that some friendly messages are printed.)
- Default constructor.
This should generate a random number from