CS 202 Fall 2013 > Assignment 5 |
CS 202 Fall 2013
Assignment 5
Assignment 5 is due at 2 p.m. Monday, October 21. It is worth 30 points.
Procedures
Complete each of the exercises below. Then turn in your work as follows.
- Run Check. Demonstrate the code 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 5 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. Coding standards are as on Assignment 3.
Exercise A—Collection of Similar Classes, But Now with—Polymorphism!
Redo Assignment 4 Exercise B using inheritance and virtual functions. (And throw out your driver program.)
- Create a class
FancyPrint
, defined in filesfancyprint.h
andfancyprint.cpp
. This class should have a virtual member functionprint
, which takes an optionalostream
(defaults tocout
). - Classes
StarPrint
,LinePrint
, andBackPrint
should be derived classes ofFancyPrint
. They should otherwise act just as in the previous assignment. - Common functionality should be moved to
class
FancyPrint
. In particular, classFancyPrint
should maintain thestring
andint
members. - Things should be done right.
In particular,
- Classes should be set up in accordance with standard conventions for class hierarchies: have virtual destructors, call base-class constructors properly.
- Use
const
correctly. - Remember DRY!
- You may add any member functions you wish to
the various classes.
This should be neatly written and commented,
but they will not be tested.
In particular,
FancyPrint
will need an appropriate constructor. You might also consider giving it get- and set-functions for the member variables.
Test Program
A test program
is available:
fancyprint_test.cpp
.
If you compile and run this program (unmodified!) with your code,
then it will test
whether your code works properly.
Notes & Misc Requirements
- You are not being asked to write a program.
Do not write “
main
”! - Your code should not produce any output other than that required above.
- As we have done in class, I strongly suggest that you first get your classes to compile with the test program. Only then should you make it work properly.
Exercise B—File Filter
Do problems 9–10 in the Chapter 15 Programming Challenges (p 942), with the following changes.
- Ignore the words “abstract” and “pure” in “Write an abstract file filter class that defines a pure virtual function ....” Just make it a class that defines a virtual function.
- Write a driver program that uses all three derived classes (encryption, upper-case, double spacing).
- Whatever encryption you do, be sure the function that encrypts is also capable of decrypting.
Notes
- Your encryption does not need to be at all sophisticated.
For example, you can do the following.
[C++]
// Suppose we have the following variables int key; // The encryption key (must be positive) char c; // The character to encrypt char encrypted_c; // We will set this to our encrypted character // Then you *could* encrypt like this: encrypted_c = c; if (c >= 32 && c <= 127) encrypted_c = c^(key&31); // So, what does this do?
Or do a better job encrypting, if you want. (But not at the expense of finishing this exercise!)
- In case it is not clear, member function
doFilter
is supposed to be used something like this.[C++]
ifstream infile("exb_inputfile.txt"); if (!infile) return; // Error checking ifstream outfile("exb_outputfile.txt"); if (!outfile) return; // Error checking some_kinda_object_here.doFilter(infile, outfile);
- Once again, do things right!