Add Two Distances

#include <iostream>
#include <cstdint>

using namespace std;

class Distance {
private:
    int32_t meters;

public:
    Distance(int32_t m) : meters(m) {}

    // Define operator+ to add two Distance objects
    Distance operator+(Distance& d){
        int32_t res = d.getMeters() + meters;
        Distance ans(res);
        return ans;
    }

    int32_t getMeters() const {
        return meters;
    }
};

int main() {
    int32_t a, b;
    cin >> a >> b;

    Distance d1(a);
    Distance d2(b);

    Distance total = d1 + d2;
    cout << "Total=" << total.getMeters();

    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Input

5 7

Expected Output

Total=12