home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / integer.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  3.2 KB  |  119 lines

  1. // integer.cpp
  2. //
  3. // Example class implementation.  This file contains the implementations
  4. // of the non inline functions contained in the example Integer class.
  5. // Notice, in the first two functions, that we always check the type
  6. // before casting.  Since the object based containers can hold mixed
  7. // types, this is a very good practice to get into.  Also notice the
  8. // I/O function implementations.  These functions make this class usable
  9. // with the C++ stream operators as well as the reserve class.
  10. //
  11. #include "integer.h"
  12.  
  13. // "isEqual" returns TRUE if the input object is of type "Integer"
  14. // and if it's integer value is the same as this integer value.
  15. //
  16. Bool Integer::isEqual(CmObject* pObj) const
  17. {
  18.   if (!pObj->isA("Integer")) return FALSE;       // A MUST!!!
  19.   return (((Integer*) pObj)->_value == _value);
  20. }
  21.  
  22. // "compare" returns FALSE if the input object is not of type
  23. // "Integer".  If it is, "compare" returns 0 if the integer values
  24. // are equal, -1 if this integer value is less than the input, and
  25. // 1 if this integer value is greater than the input.
  26. //
  27. int Integer::compare(CmObject* pObj) const
  28. {
  29.   if (!pObj->isA("Integer")) return FALSE;       // A MUST!!!
  30.   int val = ((Integer*) pObj)->_value;
  31.   return ((_value == val) ? 0 : ((_value > val) ? 1 : -1));
  32. }
  33.  
  34. // "hash" is the hash function for the integer value.  A mod is
  35. // performed with the value and the input table size.
  36. //
  37. unsigned Integer::hash(unsigned size) const
  38. {
  39.   return (unsigned)(_value % size);
  40. }
  41.  
  42. // "printOn" prints the integer value on the specified output
  43. // stream.
  44. //
  45. void Integer::printOn(ostream& os)  const
  46. {
  47.   os << _value;
  48. }
  49.  
  50. // "readFrom" reads the integer value from the specified input
  51. // stream.
  52. //
  53. void Integer::readFrom(istream& is)
  54. {
  55.   is >> _value;
  56. }
  57.  
  58. // "write" writes the integer value to the specified file.
  59. //
  60. Bool Integer::write(CmReserveFile& file) const
  61. {
  62.   return file.write(_value);
  63. }
  64.  
  65. // "read" reads the integer value from the specified file.
  66. //
  67. Bool Integer::read(CmReserveFile& file)
  68. {
  69.   return file.read(_value);
  70. }
  71.  
  72. // "<" operator returns TRUE if this integer is less than the
  73. // specified integer.
  74. //
  75. Bool Integer::operator<(const Integer& rInt) const
  76. {
  77.   return (_value < rInt._value);
  78. }
  79.  
  80. // "<=" operator returns TRUE if this integer is less than or
  81. // equal to specified integer.
  82. //
  83. Bool Integer::operator<=(const Integer& rInt) const
  84. {
  85.   return (_value <= rInt._value);
  86. }
  87.  
  88. // ">" operator returns TRUE if this integer is greater than the
  89. // specified integer.
  90. //
  91. Bool Integer::operator>(const Integer& rInt) const
  92. {
  93.   return (_value > rInt._value);
  94. }
  95.  
  96. // ">=" operator returns TRUE if this integer is greater than or
  97. // equal to specified integer.
  98. //
  99. Bool Integer::operator>=(const Integer& rInt) const
  100. {
  101.   return (_value >= rInt._value);
  102. }
  103.  
  104. // "==" operator returns TRUE if this integer is equal to the
  105. // specified integer.
  106. //
  107. Bool Integer::operator==(const Integer& rInt) const
  108. {
  109.   return (_value == rInt._value);
  110. }
  111.  
  112. // "!=" operator returns TRUE if this integer is not equal to the
  113. // specified integer.
  114. //
  115. Bool Integer::operator!=(const Integer& rInt) const
  116. {
  117.   return (_value != rInt._value);
  118. }
  119.