Lab8
CS2390 Spring 1998 Lab8
Writing the Count classes in C++
I. Abstract
In this lab you will create the Count classes in C++.
You will be given a specification of what classes to
write and what attributes and services those classes
should contain. You will also be given code for you
main() procedure to run and the output you should
receive. Put all your code (classes, main, etc.)
in one file and turn that file in using the web
turnin system.
II. Class Description
Count
A Count will be an abstract class. Remember how to declare abstract
methods?
A Count know's it's value and resetValue. Should these variables be public,
private or protected? A Count has the following services:
reset() // Set value to reset value
increment() //Increment value and return new value
decrement() //Decrement and return value
getValue() //Return my value
In addition to the above, Count will also need to understand two
operators. "++" and "--" These operators should work exactly like
increment() and decrement(). Needless to say, these operators only need
to be defined in the Count class (ie: do not make them virtual).
++myCount <- myCount.increment()
--myCount <-
myCount.decrement()
To make things even trickier, Count will need to be able to handle
different datatypes
to make it more flexible. You will have to make sure that the type of
value and resetValue
can be changed in subclasses of count. You will need to make Count a
template to do this.
The format for template is:
template < [typelist] [, [ arglist ]]
> declaration
When you create a sublcass of Count, you will need to specify the
datatype to use.
IntegerCount
An IntegerCount is a Count that works on int's.
In addition to the default constructor, you will need to define a
constructor for IntegerCount
that takes in an integer and sets the initial value of the Count to that
int. ie:
IntegerCount myIntCounnt(30); // set's 30 as initial value of
myIntCount
CharCount
A CharCount is a Count that works on char's.
CharCount will also need another constructor that takes a char for it's
initial value.
CharCount will also be specialized in that it will have a maxValue also.
For a CharCount only: If the value exceeds maxValue, reset the value.
If the value
is below the resetValue, reset the value.
The max value will be 'z' and the default initial value is 'A'.
Note that getValue() does the same thing for any Count class. What does
that tell you to do?
III. main() function
Use the following as your main() function. If your program works
correctly, you should not
see any ouput.
/**
*Main
* Test code for Count classes
**/
int main()
{
IntegerCount myIntCount;
IntegerCount myIntCount2(25);
CharCount myCharCount;
CharCount myCharCount2('z');
myIntCount.increment();
if(!(myIntCount.getValue() == 1))
{
DOH("Error1 in IntegerCount1");
}
myIntCount2.increment();
myIntCount2.decrement();
myIntCount2.increment();
if(!(myIntCount2.increment() == 27))
{
DOH("Error2 in IntegerCount!");
}
if(!(++myIntCount2 == 28))
DOH("Error3 in IntegerCount!");
myCharCount.increment();
if(myCharCount.increment() != 'C')
DOH("Error1 in CharCount!");
if((--myCharCount) != 'B')
DOH("Error 2 in CharCount!");
/*if(myCharCount2.increment() != 'A')
DOH("Error2 in CharCount!");*/
myCharCount.decrement();
myCharCount.decrement();
myCharCount.decrement();
if(myCharCount.getValue() != 'A')
DOH("Error3 in CharCount!");
return EXIT_SUCCESS;
}
You will also need this macro:
#define DOH(s) { fprintf(stderr, "%s\n",s); \
return EXIT_FAILURE;}
And depending on the system you are using, you may
have to define EXIT_FAILURE and EXIT_SUCCESS for
the above code to work correctly.
Don't forget about the lecture notes and the
C++ Tutorial if you need help. Everything you will need for this lab
can be found on those pages.
Happy Coding...
Oh yah, I will post a solution to this lab after it's due
News Page | CS2390 Sp'98 Home Page | CS2390 CoWeb | STABLE | BOOST
Questions/comments/concerns to guzdial@cc.gatech.edu
Page last updated 5/27/98; 10:41:59 AM