home *** CD-ROM | disk | FTP | other *** search
/ Using Visual C++ 4 (Special Edition) / Using_Visual_C_4_Special_Edition_QUE_1996.iso / ch14 / addeq2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-18  |  850 b   |  45 lines

  1. #include <iostream.h>
  2.  
  3.  
  4. typedef struct _MyStruct {
  5.  
  6.     unsigned int a;
  7.     unsigned int b;
  8.  
  9.     // Operators
  10.     _MyStruct operator+(_MyStruct &o)
  11.     {
  12.         _MyStruct temp = { a + o.a, b + o.b };
  13.         return temp;
  14.     }
  15.  
  16.     unsigned int operator==(_MyStruct &o)
  17.     {
  18.         return a == o.a && b == o.b;
  19.     }
  20.  
  21. } MyStruct;
  22.  
  23.  
  24. template <class T>
  25. unsigned int AddEquals(T val1, T val2, T compare)
  26. {
  27.     return (val1 + val2) == compare;
  28. }
  29.  
  30. void main()
  31. {
  32.     // Example 1 _ Legal
  33.     cout << "Does 4 + 5 = 9? ";
  34.     cout << (AddEquals(4, 5, 9) ? "Yes" : "No") << "\n";
  35.  
  36.     // Example 2 _ Legal
  37.     cout << "Does 7.0 + 5.5 = 12.0? ";
  38.     cout << (AddEquals(7.0, 5.5, 12.0) ? "Yes" : "No") << "\n";
  39.  
  40.     // Example 3 _ Illegal
  41.     MyStruct  a = { 23, 43 }, b = { 11, 19 }, c = { 34, 62 };
  42.     cout << "Does a + b = c? ";
  43.     cout << (AddEquals(a, b, c) ? "Yes" : "No") << "\n";
  44. }
  45.