fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. class Seconds;
  6.  
  7. class Hours {
  8. unsigned long long _hours;
  9. public:
  10. Hours(unsigned long long hours) : _hours(hours) { }
  11.  
  12. unsigned long long getHours() const {
  13. return this->_hours;
  14. }
  15. };
  16.  
  17. class Seconds {
  18. unsigned long long _seconds;
  19. public:
  20. Seconds(unsigned long long seconds) : _seconds(seconds) { }
  21.  
  22. unsigned long long getSeconds() const {
  23. return this->_seconds;
  24. }
  25.  
  26. Seconds& operator= (const unsigned long long& ull);
  27. };
  28.  
  29. unsigned long long operator+(const Hours& hours, const Seconds& seconds)
  30. {
  31. return hours.getHours() + seconds.getSeconds();
  32. }
  33.  
  34. Hours operator "" _h(unsigned long long hours) {
  35. return Hours(hours);
  36. }
  37.  
  38. Seconds operator "" _s(unsigned long long seconds) {
  39. return Seconds(seconds);
  40. }
  41.  
  42. Seconds& Seconds::operator= (const unsigned long long& ull)
  43. {
  44. // do the copy
  45. *this = Seconds(ull);
  46.  
  47. // return the existing object
  48. return *this;
  49. }
  50.  
  51. int main() {
  52. Seconds s(1_h + 10_s);
  53. cout <<s.getSeconds()<<endl;
  54. s = 2_h + 60_s;
  55. cout <<s.getSeconds()<<endl;
  56. return 0;
  57. }
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
11
62