CSE 490c/303 Homework 5 - Nonplussed about C++

Due: Friday, 11/21/03

Some advice before we start: C++ (like C) has a lot of concepts in it that work almost identically to those found in Java. If you view C++ code as somewhat deranged Java, you should be able to read a lot of it before having to consult the reference manual.

Turnin

Put all of your files for this homework in a directory named 'hw5'.  Run the command 'turnin -ccse490c hw5'.  Run turnin on the directory itself - don't run 'tar' on this directory before turnin.

Make sure your code works with the compiler on attu (g++) before your turn it in!

Overview

For this assignment, you will create a word count class, then subclass it to be more detailed. You'll also use rcs to keep track of changes, as you make them.

Step 1

In the first step you'll create a single class that will serve as the base class for the work in Step 3. This first class should be called WordCounter , and has the following public functions:

virtual int getLines();
virtual int getWords();
virtual int getChars();
virtual void processChar(char c);
virtual void printResults();

A WordCounter object should start off with getLines(), getWords() and getChars() returning 0. processChar() will be repeatedly called, and the appropriate line, character and word counts should be updated. printResults should print the line, word and character information to the screen. WordCounter should exhibit behavior identical to that found with wc , except perhaps for minor whitespace issues.

How will you know your implementation works? You'll also create a main() function that tests it, of course.

Step 2

Now that you have WordCounter working, create an RCS repository for it before you continue working on it (which will break it, at least temporarily). man rcsintro will provide more information about most of what is going on here. (It's short...)
  1. Create a subdirectory named RCS.
  2. Put the following line at the top of each of your files:
    // $Header$
  3. Issue the command ci XXX for each .cpp and .h file, and your Makefile (putting the filename in place of XXX).
  4. It would be nice to be able to retrieve the working copy of the program you just checked in later, even after you've checked in later versions. To help do that, "tag" the files in the repository now using
    rcs -nStep1: RCS/*
  5. You'll notice all the files you checked in have disappeared from your working directory. Use co XXX to obtain a (read-only) copy of XXX.
  6. When you want to edit a file, you'll need a writable copy. Use co -l XXX to get one (and also to "lock" the file in the repository so no one else can get a writable copy until you do a ci, including you!).
  7. When you have completed the next step, check in all the files and tag them with name "Step2".

Step 3

Objects of the second class you'll build for this assignment are able to count characters, words, and lines, but they also do something more -- they keep track of how often each character appears.

Create a class named FreqCounter that is a subclass of your implementation of WordCounter from Step 1. FreqCounter has the following (additional) functions:

virtual int getCharCount(char c);
virtual double getCharFreq(char c);

getCharCount() should return the total number of occurrences seen so far for the character c. 
getCharFrequency() should return the fraction of the total number of characters the given character represents. (In both cases, a "character" is anything passed in, not just, say, alphanumerics.)

You should also override printResults() to output the additional information now collected.

Update your test program (main()) to test the new funcationality.

Hints: