#include "boundedinteger.h" //#include #include #include BoundedInteger::BoundedInteger() { printf("BoundedInteger()\n"); mValue = 7; mMin = 0; mMax = 10; } BoundedInteger::BoundedInteger(int value, int min, int max) { printf("BoundedInteger(int, int, int)\n"); setMin(min); setMax(max); setValue(value); } BoundedInteger::BoundedInteger(const BoundedInteger& orig) { printf("BoundedInteger(BoundedInteger) (copy constructor)\n"); mValue = orig.mValue; mMin = orig.mMin; mMax = orig.mMax; } int BoundedInteger::getValue(void) const { // Now I can access member variables return mValue; } void BoundedInteger::setValue(int value) { if(value < mMin){ mValue = mMin; } else if(value > mMax){ mValue = mMax; } else { mValue = value; } } 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& rhs) { printf("operator=\n"); mValue = rhs.mValue; mMin = rhs.mMin; mMax = rhs.mMax; // TODO: if we had pointers, we'd make a deep copy here return *this; // "this" is a pointer to the current object } BoundedInteger BoundedInteger::operator+(const BoundedInteger& rhs) const { printf("operator+\n"); //BoundedInteger sum; //sum.mValue = mValue + rhs.mValue; // short for this->mValue, etc. //sum.mMin = mMin + rhs.mMin; //sum.mMax = mMax + rhs.mMax; BoundedInteger sum(mValue + rhs.mValue, mMin + rhs.mMin, mMax + rhs.mMax); // This causes all kinds of trouble... return sum; } int main(int argc, char* argv[]) { BoundedInteger i; // i.setMin(5); // i.setMax(30); // i.setValue(300); BoundedInteger j; // j.setMin(0); // j.setMax(100); // j.setValue(20); BoundedInteger k; // j.operator=(i); //j.operator+(i); //k = j + i; k = i; BoundedInteger l = j; //std::cout << "The value of i is " << i.getValue() << std::endl; printf("The value of i is %d\n", i.getValue()); printf("The value of j is %d\n", j.getValue()); printf("The value of k is %d\n", k.getValue()); printf("The value of l is %d\n", l.getValue()); return 0; }