The Easiest Way to Save and Share Code Snippets on the web

RationalNumber.cpp

cpp

posted: Feb, 8th 2012 | jump to bottom

  1. #ifndef RationalNumberClass
  2. #define RationalNumberClass
  3.  
  4. #include <stdexcept>
  5.  
  6. class RationalNumber
  7. {
  8. int num[2];
  9.  
  10. RationalNumber()
  11. {
  12. num[0] = num[1] = 0;
  13. }
  14.  
  15. public:
  16.  
  17. void SetNumber(int iP, int iQ)
  18. {
  19. if(iQ == 0) throw std::invalid_argument("Denominator can't be zero.");
  20. if(iQ < 0) throw std::invalid_argument("Denominator can't be negative.");
  21. num[0] = iP;
  22. num[1] = iQ;
  23. }
  24.  
  25. double GetDoubleValue()
  26. {
  27. return ((double) num[0]) / ((double) num[1]);
  28. }
  29. }
  30.  
  31. #endif
  32.  
967 views