| 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
Wodgetdescribed below. “Minimal” means that all the member functions are there, and the ones that return values have a dummyreturnstatement, 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
Wodgetshould be implemented in fileswodget.handwodget.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
stringand anint. - Member function
cattaking anintparameter and returning nothing. - Const member function
dogtaking twointparameters and returning anint. - Const member function
llamataking one parameter, avectorofWodgetobjects, and returning nothing. - Accessor and mutator functions for a presumed member variable
cowof typestring. These should be namedgetCowandsetCowand should be written as usual.
Demonstrate your class by compiling it with program
wodget_main.cppand executing the result. Do not modify filewodget_main.cpp.
- Write a class
Bracketswith the following threepublicmember functions.- Const member function
hello. This should print “[Hello!]” tocout, followed by a newline. - Const member function
dollars. This takes anintparameter, and prints that many dollar signs between brackets tocout, followed by a newline. For example, calling functiondollarwith parameter4would print “[$$$$]”. - Const member function
message. This should take astringparameter. It should print the string between brackets tocout, followed by a newline. For example, calling functiondollarwith 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
privatemember functionoutputwhich all three of them call. In particular, member functionoutputshould 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.cppby writing functionprintAll. This function takes avectorof(PrintSomething *). It returns nothing. Each pointer in thevectorcan be assumed to point to aPrintSomethingobject. The function is to go through thevector, in order. For each pointer, it should call member functiondoPrinton the object the pointer points to.Do not modify any other part of program
print_from_ptrs.cpp.
- Write a class
Greeterwith the following member functions.- Default constructor.
This should generate a random number from
1to1000(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
Greeterby writing a program that does the following.- Ask the user how many objects to create.
- Create a
vectorcontaining that manyGreeterobjects. - End.
The program should do nothing else. (This ought to result in a number of calls to the
Greeterdefault constructor and destructor, with the result that some friendly messages are printed.)
- Default constructor.
This should generate a random number from