#include "cpp_polymorphos.h" //------------------------------------------------------------------------ // determine methods of base class for derived classes are IntegerValue, // LongValue, DoubleValue. //------------------------------------------------------------------------ template BaseValue::BaseValue( T in_val ) : value(in_val) { } template void BaseValue::print() { cout << "Input value: " << value << "\n"; } //------------------------------------------------------------------------ // determine methods of IntegerValue class //------------------------------------------------------------------------ IntegerValue :: IntegerValue ( int val ) { this->setValue( val ); } int IntegerValue :: value_abs ( ) { int value = getValue(); return abs ( value ); } IntegerValue IntegerValue :: operator+ ( int val ) { IntegerValue ival; ival.setValue(this->getValue()+val); return ival; } IntegerValue IntegerValue :: operator+ ( double val ) { IntegerValue ival; ival.setValue(this->getValue()+val); return ival; } IntegerValue IntegerValue :: operator+ ( long val ) { IntegerValue ival; ival.setValue(this->getValue()+val); return ival; } IntegerValue IntegerValue :: operator- ( int val ) { IntegerValue ival; ival.setValue(this->getValue()-val); return ival; } IntegerValue IntegerValue :: operator- ( double val ) { IntegerValue ival; ival.setValue(this->getValue()-val); return ival; } IntegerValue IntegerValue :: operator- ( long val ) { IntegerValue ival; ival.setValue(this->getValue()-val); return ival; } // division IntegerValue IntegerValue :: operator/ ( int val ) { IntegerValue ival; ival.setValue(this->getValue()/val); return ival; } IntegerValue IntegerValue :: operator/ ( double val ) { IntegerValue ival; ival.setValue(this->getValue()/val); return ival; } IntegerValue IntegerValue :: operator/ ( long val ) { IntegerValue ival; ival.setValue(this->getValue()/val); return ival; } IntegerValue IntegerValue :: operator* ( int val ) { IntegerValue ival; ival.setValue(this->getValue()*val); return ival; } IntegerValue IntegerValue :: operator* ( double val ) { IntegerValue ival; ival.setValue(this->getValue()*val); return ival; } IntegerValue IntegerValue :: operator* ( long val ) { IntegerValue ival; ival.setValue(this->getValue()*val); return ival; } //----------------------------------------------------------------------- // determine methods of LongValue class //----------------------------------------------------------------------- LongValue :: LongValue ( long val ) { this->setValue( val ); } long LongValue :: value_abs ( ) { long value = getValue(); return labs ( value ); } LongValue LongValue :: operator+ ( int val ) { LongValue lval; lval.setValue(this->getValue()+val); return lval; } LongValue LongValue :: operator+ ( double val ) { LongValue lval; lval.setValue(this->getValue()+val); return lval; } LongValue LongValue :: operator+ ( long val ) { LongValue lval; lval.setValue(this->getValue()+val); return lval; } LongValue LongValue :: operator- ( int val ) { LongValue lval; lval.setValue(this->getValue()-val); return lval; } LongValue LongValue :: operator- ( double val ) { LongValue lval; lval.setValue(this->getValue()-val); return lval; } LongValue LongValue :: operator- ( long val ) { LongValue lval; lval.setValue(this->getValue()-val); return lval; } LongValue LongValue :: operator/ ( int val ) { LongValue lval; lval.setValue(this->getValue()/val); return lval; } LongValue LongValue :: operator/ ( double val ) { LongValue lval; lval.setValue(this->getValue()/val); return lval; } LongValue LongValue :: operator/ ( long val ) { LongValue lval; lval.setValue(this->getValue()/val); return lval; } LongValue LongValue :: operator* ( int val ) { LongValue lval; lval.setValue(this->getValue()*val); return lval; } LongValue LongValue :: operator* ( double val ) { LongValue lval; lval.setValue(this->getValue()*val); return lval; } LongValue LongValue :: operator* ( long val ) { LongValue lval; lval.setValue(this->getValue()*val); return lval; } //----------------------------------------------------------------------- // determine methods of DoubleValue class //----------------------------------------------------------------------- DoubleValue :: DoubleValue ( double val ) { this->setValue( val ); } DoubleValue :: DoubleValue ( LongValue val ) { this->setValue ( val.getLongValue() ); } DoubleValue :: DoubleValue ( IntegerValue val ) { this->setValue ( val.getIntegerValue() ); } double DoubleValue :: value_abs ( ) { double value = getValue(); return fabs ( value ); } double DoubleValue :: do_div_with_absvalue( LongValue & divs ) { return ( *this / divs ).value_abs(); } double DoubleValue :: do_div_with_absvalue( IntegerValue & divs ) { return ( *this / divs ).value_abs(); } DoubleValue DoubleValue :: operator+ ( int val ) { DoubleValue dval; dval.setValue(this->getValue() + val); return dval; } DoubleValue DoubleValue :: operator+ ( double val ) { DoubleValue dval; dval.setValue(this->getValue() + val); return dval; } DoubleValue DoubleValue :: operator+ ( long val ) { DoubleValue dval; dval.setValue(this->getValue() + val); return dval; } DoubleValue DoubleValue :: operator- ( int val ) { DoubleValue dval; dval.setValue(this->getValue() - val); return dval; } DoubleValue DoubleValue :: operator- ( double val ) { DoubleValue dval; dval.setValue(this->getValue() - val); return dval; } DoubleValue DoubleValue :: operator- ( long val ) { DoubleValue dval; dval.setValue(this->getValue() - val); return dval; } DoubleValue DoubleValue :: operator/ ( int val ) { DoubleValue dval; dval.setValue(this->getValue() / val); return dval; } DoubleValue DoubleValue :: operator/ ( double val ) { DoubleValue dval; dval.setValue(this->getValue() / val); return dval; } DoubleValue DoubleValue :: operator/ ( long val ) { DoubleValue dval; dval.setValue(this->getValue() / val); return dval; } DoubleValue DoubleValue :: operator* ( int val ) { DoubleValue dval; dval.setValue(this->getValue() * val); return dval; } DoubleValue DoubleValue :: operator* ( double val ) { DoubleValue dval; dval.setValue(this->getValue() * val); return dval; } DoubleValue DoubleValue :: operator* ( long val ) { DoubleValue dval; dval.setValue(this->getValue() * val); return dval; } //-------------------------------------------------------------------------------- // methods are not members of any classes //-------------------------------------------------------------------------------- DoubleValue operator/ ( DoubleValue &a, LongValue &b ) { DoubleValue outval ( a.getDoubleValue() / b.getLongValue() ); return outval; } DoubleValue operator/ ( DoubleValue &a, IntegerValue &b ) { DoubleValue outval ( a.getDoubleValue() / b.getIntegerValue() ); return outval; } DoubleValue operator/ (LongValue &a, IntegerValue &b ) { IntegerValue outval ( a.getLongValue() / b.getIntegerValue() ); return outval; } DoubleValue operator- (DoubleValue &a, IntegerValue &b ) { IntegerValue outval ( a.getDoubleValue() - b.getIntegerValue() ); return outval; } DoubleValue operator+ ( double a, IntegerValue &b ) { DoubleValue outval ( b.getIntegerValue() ); return outval + a; } int main ( int argc, char** argv ) { int d; LongValue lval = 12345678; IntegerValue ival = 122343; DoubleValue dval = -123.1234567890 ; DoubleValue rval1 = -64.1234 + ival ; DoubleValue rval2 = dval / lval; DoubleValue rval3 = rval2 + ival; cout << "absolute result value1: " << rval1.value_abs() << "\n"; cout << "absolute result value2: " << rval2.value_abs() << "\n"; cout << "absolute result value3: " << dval.do_div_with_absvalue( ival ) << "\n"; cout << "absolute result value4: " << rval2 << "\n"; return 0; } /*eof*/