fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class dist
  4. {
  5. public:
  6. int km, m;
  7. void print_();
  8. dist operator-(dist obj);
  9. //constructor
  10. dist()
  11. {
  12. km = 0;
  13. m = 0;
  14. }
  15. dist(int k, int mt)
  16. {
  17. km = k;
  18. m = mt;
  19. }
  20. };
  21.  
  22. //the function for subtracting
  23. dist dist::operator-(dist obj)
  24. {
  25. {
  26. dist temp;
  27. temp.km = km - obj.km;
  28. temp.m = m - obj.m;
  29. if (temp.m >= 1000)
  30. {
  31. temp.km++;
  32. temp.m = temp.m - 1000;
  33. }
  34. return temp;
  35. }
  36. }
  37.  
  38.  
  39. //main function starts here
  40. void dist::print_()
  41. {
  42. cout << km << " " << m;
  43. }
  44. int main ()
  45. {
  46. dist d1(50, 60), d2(30, 40), d3;
  47. d3 = d1 - d2;
  48. d3.print_();
  49. return 0;
  50. }
Success #stdin #stdout 0s 5296KB
stdin
Standard input is empty
stdout
20 20