Hi
Why is the output so weird.
at first it gives error(because of the exception), and then it says 1??? shouldnt it be the same??
here is the code
main.cpp
ModCounter.hCode:#include "Counter.h"
#include "ModCounter.h"
#include<iostream>
using namespace std;
void main()
{
ModCounter mc1(2);
ModCounter mc2(4);
try
{
cout << "mc1 == mc2: ";
cout << (mc1 == mc2) << endl;
}
catch(...)
{
cout << "Error" << endl;
}
Counter& c1 = mc1;
Counter& c2 = mc2;
// c1 and c2 are references of type Counter, but refer to ModCounters though.
try
{
cout << "Again mc1 == mc2: ";
cout << (c1 == c2) << endl;
}
catch(...)
{
cout << "Error" << endl;
}
}
ModCounter.cppCode:#include "Counter.h"
#ifndef MODCOUNTER_H
#define MODCOUNTER_H
class ModCounter:public Counter
{
public:
ModCounter();//done
ModCounter(const int);//done
ModCounter(const ModCounter& other);//done
virtual const Counter& operator=(const ModCounter& other);
virtual bool operator==( const ModCounter& other) const;//done
virtual Counter operator++();//done
virtual Counter operator++(int);//done
virtual Counter operator--();//done
virtual Counter operator--(int);//done
virtual ~ModCounter();//done
private:
int limit;
};
#endif MODCOUNTER_H
Counter.hCode:#include "ModCounter.h"
#include "Counter.h"
ModCounter::ModCounter()
:limit(1)
{
}
ModCounter::ModCounter(const int newLimit)
:limit(newLimit)
{
}
ModCounter::~ModCounter()
{
}
ModCounter::ModCounter(const ModCounter& other)
{
int i=0;
for(i=0;i<other.getCounterValue()-1;i++)Counter::operator ++();
limit=other.limit;
}
Counter ModCounter::operator ++()
{
if(getCounterValue()<limit-1)Counter::operator ++();
else reset();
return *this;
}
Counter ModCounter::operator --()
{
int i=0;
if(getCounterValue()==0)
{
for(i=0;i<limit-1;i++)Counter::operator ++();
}
else Counter::operator --();
return *this;
}
Counter ModCounter::operator ++(int)
{
if(getCounterValue() < limit - 1)Counter::operator++();
else reset();
return *this;
}
Counter ModCounter::operator --(int)
{
int i=0;
if(getCounterValue() == 0 )
{
for (i = 0; i < limit - 1; i++)Counter::operator++();
}
else Counter::operator--();
return *this;
}
bool ModCounter::operator ==(const ModCounter& other) const
{
const ModCounter *ptrMod= dynamic_cast<const ModCounter*>(&other);
if(ptrMod!=0)
{
if(limit!=ptrMod->limit)throw Invalid();
}
return (limit==ptrMod->limit && getCounterValue()==ptrMod->getCounterValue());
}
const Counter& ModCounter::operator =(const ModCounter& other)
{
const ModCounter *ptrMod1= dynamic_cast<const ModCounter*>(&other);
if(ptrMod1!=0)
{
Counter::operator=(other);
limit = ptrMod1->limit;
}
return *ptrMod1;
}
Counter.cppCode:#ifndef COUNTER_H
#define COUNTER_H
// DO NOT MODIFY THIS FILE
// DO NOT MODIFY THIS FILE
// DO NOT MODIFY THIS FILE
// This class models a Counter that holds a value and allows you
// to increment, decrement and reset.
// An exception class
class Invalid
{
};
class Counter
{
public:
Counter();
// Purpose: To initialize the Counter object
// Requirements: -
// Promises: Couter's value is 0.
// Exception: -
virtual ~Counter();
// Purpose: To properly cleanup the object
// Requirements: -
// Promises: -
// Exception: -
Counter(const Counter& other);
// Purpose: To initialize the Counter object
// Requirements: -
// Promises: Counter's value == other's value
// Exception: -
virtual const Counter& operator=(const Counter& other);
// Purpose: To copy the value from one counter to another.
// Requirements: -
// Promises: this object's value == other's value
// Exception: May throw an Invalid exception (for future use).
virtual bool operator==(const Counter& other) const;
// Purpose: Checks if two counter objects are equal (by value).
// Requirements: -
// Promises: Returns a true if value of this object == other's value.
// Exception: May throw an Invalid exception (for future use).
int getCounterValue() const;
// Purpose: To query the value of the counter object.
// Requirements: -
// Promises: Returns the current value of the counter.
// Exception: -
virtual Counter operator++();
// Purpose: To increment the value of the counter.
// Requirements: -
// Promises: Note the value may actually be more or less depending on implementation.
// Exception: -
virtual Counter operator++(int);
// Purpose: To increment the value of the counter.
// Requirements: -
// Promises: Note the value may actually be more or less depending on implementation.
// Exception: -
virtual Counter operator--();
// Purpose: To decrement the value of the counter.
// Requirements: -
// Promises: Note the value may actually be more or less depending on implementation.
// Exception: -
virtual Counter operator--(int);
// Purpose: To increment the value of the counter.
// Requirements: -
// Promises: Note the value may actually be more or less depending on implementation.
// Exception: -
virtual void reset();
// Purpose: To set the value of the counter to 0.
// Requirements: -
// Promises: Counter's value is 0.
// Exception: -
private:
int value;
};
#endif COUNTER_H
Code:// DO NOT MODIFY THIS FILE
// DO NOT MODIFY THIS FILE
// DO NOT MODIFY THIS FILE
#include "Counter.h"
Counter::Counter()
{
value = 0;
}
Counter::~Counter()
{
}
Counter::Counter(const Counter& other)
{
value = other.value;
}
const Counter& Counter::operator=(const Counter& other)
{
if (this != &other)
{
value = other.value;
}
return *this;
}
bool Counter::operator==(const Counter& other) const
{
return value == other.value;
}
int Counter::getCounterValue() const
{
return value;
}
Counter Counter::operator++()
{
value++;
return *this;
}
Counter Counter::operator++(int)
{
Counter temp = *this;
value++;
return temp;
}
Counter Counter::operator--()
{
value--;
return *this;
}
Counter Counter::operator--(int)
{
Counter temp = *this;
value--;
return temp;
}
void Counter::reset()
{
value = 0;
}