#include "boundedinteger.h" #include int BoundedInteger::value() { return mValue; } void BoundedInteger::setValue(int val) { if(val < mMin){ mValue = mMin; } else if(val > mMax){ mValue = mMax; } else{ mValue = val; } } void BoundedInteger::setMin(int min) { mMin = min; if(mValue < mMin){ mValue = mMin; } } void BoundedInteger::setMax(int max) { mMax = max; if(mValue > mMax){ mValue = mMax; } } BoundedInteger BoundedInteger::operator+(const BoundedInteger &other) { printf("Adding %d and %d\n", mValue, other.mValue); BoundedInteger result; if(mMin > other.mMin){ result.setMin(mMin); } else { result.setMin(other.mMin); } if(mMax > other.mMax){ result.setMax(other.mMax); } else { result.setMax(mMax); } result.setValue(mValue + other.mValue); return result; } BoundedInteger BoundedInteger::operator=(const BoundedInteger &other) { printf("Copying a BoundedInteger with value %d\n", other.mValue); mValue = other.mValue; mMin = other.mMin; mMax = other.mMax; // Other stuff for a proper deep copy return *this; } int main(int argc, char* argv[]) { BoundedInteger a; a.setMin(1); a.setMax(10); a.setValue(2); BoundedInteger b; b = a; b.setValue(5); BoundedInteger c; c = a + a + b; printf("The value of a is %d\n", a.value()); printf("The value of b is %d\n", b.value()); printf("The value of c is %d\n", c.value()); }