Click HERE to download a formatted copy in Word97
// A simple counting class that demonstrates Virtual Functions in C++
class Counter {
public:
Counter(); // Constructs a counter set to 0.
int value() const; // Current counter value.
void nvPrint() const; // Prints the counter value.
// virtual member functions
virtual void print() const; // Prints the count.
virtual void increment(); // Increments the counter one unit.
virtual void decrement(); // Decrements the counter one unit.
protected:
int count_;
};
//////////////////////////////////////////////////////////////////////////////////////
// A counter which does its counting modulo a positive integer base.
class ModCounter : public Counter {
public:
ModCounter(int b = 10); // Constructs a counter set to 0 to count modulo b
// ASSUME: b > 1
void nvPrint() const; // Prints the values of the ModCounter.
void print() const; // Prints the values of the ModCounter.
int base() const; // Base value.
void increment(); // Increments the count one unit modulo the base.
void decrement(); // Decrements the count one unit modulo the base.
protected:
int base_;
};
//////////////////////////////////////////////////////////////////////////////////////
// A counter that keeps its count within a fixed range of values.
class RangeCounter : public Counter {
public:
RangeCounter(int b = 0, int t = 1000); // Creates a counter set to 0 which
// counts from a minimum of b to maximum of t.
// ASSUME: 0 <= b <= t
void nvPrint() const; // Prints the values for the RangeCounter.
void print() const; // Prints the values for the RangeCounter.
int top() const; // Maximum possible counter value.
int bottom() const; // Minimum possible counter value.
void increment(); // Increments the count by 1 up to the maximum.
void decrement(); // Decrements the count by 1 down to the minimum.
protected:
int bottomOfRange_;
int topOfRange_;
};
//////////////////////////////////////////////////////////////////////////////////////
// A counter with a fixed range that can detect when an overflow occurs.
class CounterWithOverflow : public RangeCounter {
public:
CounterWithOverflow(int b = 0, int t = 1000);
// Creates a counter set to 0 which
// counts from a minimum of b to maximum of t.
// ASSUME: 0 <= b <= t
void increment(); // Increments the count by 1 up to the maximum.
void print() const; // Prints the currrent values.
bool overflow() const; // Has overflow occurred?
protected:
bool overflow_;
};
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
Counter :: Counter() : count_(0)
{ }
int Counter :: value() const
{
return count_;
}
void Counter :: nvPrint() const
{
cout << "The count is " << count_ << endl;
}
void Counter :: print() const
{
nvPrint();
}
void Counter :: increment()
{
count_++;
}
void Counter :: decrement()
{
count_--;
}
//////////////////////////////////////////////////////////////////////////////////////
ModCounter :: ModCounter(int b) : base_(b)
{ }
void ModCounter :: nvPrint() const
{
cout << "The count is " << count_ << " modulo " << base_ << endl;
}
void ModCounter :: print() const
{
nvPrint(); // invokes ModCounter::nvPrint()
}
int ModCounter :: base() const
{
return base_;
}
void ModCounter :: increment()
{
count_ = (count_ + 1) % base_;
}
void ModCounter :: decrement()
{
count_--;
if (count_ < 0)
count_ = base_ - 1;
}
//////////////////////////////////////////////////////////////////////////////////////
RangeCounter :: RangeCounter(int b, int t) : bottomOfRange_(b), topOfRange_(t)
{ }
int RangeCounter :: top() const
{ return topOfRange_;
}
int RangeCounter :: bottom() const
{
return bottomOfRange_;
}
void RangeCounter :: increment()
{
if (count_ < topOfRange_)
count_++;
}
void RangeCounter :: decrement()
{
if (count_ > bottomOfRange_)
count_--;
}
void RangeCounter :: nvPrint() const
{
cout << "The count is " << count_ << ", top of range: " << topOfRange_
<< ", bottom of range " << bottomOfRange_ << endl;
}
void RangeCounter :: print() const
{
nvPrint();
}
//////////////////////////////////////////////////////////////////////////////////////
CounterWithOverflow :: CounterWithOverflow (int b, int t) : RangeCounter(b,t)
{ }
void CounterWithOverflow :: increment()
{
if (count_ == topOfRange_)
overflow_ = true;
else
count_++;
}
void CounterWithOverflow :: print() const
{
cout <<"OVFL: ";
RangeCounter::print();
}
bool CounterWithOverflow :: overflow() const
{
return overflow_;
}
virtfunc.doc -- Created by DrB on 09/30/02