Add Two Distances

#include <iostream>
using namespace std;

class Distance {
private:
   int meters;
public:
   Distance(int m) : meters(m) {}
   friend Distance operator+(Distance& d1, Distance& d2)
   {
      return d1.meters + d2.meters;
   }
   // your code here: define operator+ to add two Distance objects
   int getMeters() const {
       return meters;
   }
};

int main() {
   int a, b;
   cin >> a >> b;
   Distance d1(a);
   Distance d2(b);
   Distance total = d1 + d2; // must use overloaded operator
   cout << "Total=" << total.getMeters();
   return 0;
}
Upvote
Downvote
Loading...

Input

5 7

Expected Output

Total=12