//------------------------------------------------------------------------------------------- // Arithmetical operations // (c) 2010 year Andrey Y. Rzhavsko(aka rjaan) //------------------------------------------------------------------------------------------- #ifndef __CPP_POLYMORPHOS_H #define __CPP_POLYMORPHOS_H 1 #include #include #include #include // base class BaseValue using namespace std; class IntegerValue; class LongValue; class DoubleValue; template class BaseValue { public: BaseValue ( ) { }; // constructor by default BaseValue ( T in_val ); // constructor for initialize the object value virtual ~BaseValue( ) { }; // virtual destructor virtual T value_abs ( ) = 0; // virtual function for compute abs for object value void print(); // print a object value in console // operators BaseValue& operator= ( T val ) { this->value = val; return *this; } protected: void setValue( T val ) { this->value = val; } // set new object value T getValue ( void ) { return this->value; } // get a object value private: T value; // object value }; // the IntegerValue type is a derived class of class BaseValue class IntegerValue : protected BaseValue { public: // methods IntegerValue() { }; // default constructor IntegerValue( int val ); // constructor with pre-installed value ~IntegerValue( ) { }; int value_abs ( ); // compute abs for integer value // wrapper functions are getting/setting the value int getIntegerValue () { return getValue(); }; void setIntegerValue ( int val ) { setValue( val ); }; // overlaped operators // addition IntegerValue operator+ ( int val ); IntegerValue operator+ ( double val ); IntegerValue operator+ ( long val ); // diminution IntegerValue operator- ( int val ); IntegerValue operator- ( double val ); IntegerValue operator- ( long val ); // division IntegerValue operator/ ( int val ); IntegerValue operator/ ( double val ); IntegerValue operator/ ( long val ); // multiplication IntegerValue operator* ( int val ); IntegerValue operator* ( double val ); IntegerValue operator* ( long val ); }; // the LongValue type is a derived class of class BaseValue and // allows to do arithmetical operations with long type class LongValue : protected BaseValue { public: // methods LongValue () { }; // default constructor LongValue ( long val ); // constructor with pre-installed value ~LongValue( ) { } long value_abs ( ); // compute abs for long value // wrapper functions are getting/setting the value long getLongValue () { return this->getValue(); }; void setLongValue ( double val ) { setValue( val ); }; // overlaped operators // addition LongValue operator+ ( int val ); LongValue operator+ ( double val ); LongValue operator+ ( long val ); // diminution LongValue operator- ( int val ); LongValue operator- ( double val ); LongValue operator- ( long val ); // division LongValue operator/ ( int val ); LongValue operator/ ( double val ); LongValue operator/ ( long val ); // multiplication LongValue operator* ( int val ); LongValue operator* ( double val ); LongValue operator* ( long val ); }; // the DoubleValue type is a derived class of class BaseValue and // allows to do arithmetical operations with double type class DoubleValue : protected BaseValue { public: // methods DoubleValue(){ }; // default constructor DoubleValue( double val ); // constructor with pre-installed value ~DoubleValue( ) { }; DoubleValue ( LongValue val ); DoubleValue ( IntegerValue val ); double value_abs ( ); // compute abs for double value // wrapper functions are getting/setting the value double getDoubleValue () { return getValue(); }; void setDoubleValue ( double val ) { setValue( val ); }; double do_div_with_absvalue ( LongValue & divs ); double do_div_with_absvalue ( IntegerValue & divs ); double do_div_with_absvalue ( DoubleValue & divs ); // overlaped addition operator // addition DoubleValue operator+ ( int val ); DoubleValue operator+ ( double val ); DoubleValue operator+ ( long val ); // diminution DoubleValue operator- ( int val ); DoubleValue operator- ( double val ); DoubleValue operator- ( long val ); // division DoubleValue operator/ ( int val ); DoubleValue operator/ ( double val ); DoubleValue operator/ ( long val ); // multiplication DoubleValue operator* ( int val ); DoubleValue operator* ( double val ); DoubleValue operator* ( long val ); friend DoubleValue operator/ ( DoubleValue &a, LongValue &b ); friend DoubleValue operator/ ( DoubleValue &a, IntegerValue &b ); friend DoubleValue operator/ ( LongValue &a, IntegerValue &b ); friend DoubleValue operator- (DoubleValue &a, IntegerValue &b ); friend DoubleValue operator+ ( double a, IntegerValue &b ); operator double() { return this->getValue(); } }; #endif /*__CPP_POLYMORPHOS_H*/