Sum of Two Objects

#include <iostream>
using namespace std;

class Distance;  // forward declaration

class Point {
 private:
 int x;

 public:
  Point(int v) : x(v) {}
  	// your code here: declare friend function sumValues
	friend Point sumValues(Point& p, Distance& d);
	friend std::ostream& operator<<(std::ostream& os, const Point& p);
};

class Distance {
 private:
 	int d;

 public:
 	Distance(int v) : d(v) {}
 	// your code here: declare friend function sumValues
	friend Point sumValues(Point& p, Distance& d);
};

// your code here: define friend function sumValues
Point sumValues(Point& p, Distance& d)
{
	return Point(p.x + d.d);
}

std::ostream& operator<<(std::ostream& os, const Point& p)
{
	os << "Sum=" << p.x;
	return os;
}

int main() {
	int a, b;
	cin >> a >> b;
	
	Point p(a);
	Distance dist(b);

	cout << sumValues(p, dist);
	return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

5 7

Expected Output

Sum=12