Answer to Question #290077 in C++ for S bharath

Question #290077

The location of a point in two dimensions can be determined either by Cartesian coordinates (x, y) or by polar coordinates (r, θ). Using object oriented programming (i.e., by using classes and objects), write a program which yields polar coordinates of a point when the Cartesian coordinates are given as input by the user. The angle should be given in degrees in the output. Your code should be able to tackle the following cases for θ as well:

1
Expert's answer
2022-01-23T13:38:25-0500
#include<iostream>
#include<cmath>


using namespace std;


class Cartesian
{
	double x;
	double y;
public:
	Cartesian(){}
	friend class Polar;
	void SetCoordinate()
	{
		cout << "Please, enter an x coordinate: ";
		cin >> x;
		cout << "Please, enter an y coordinate: ";
		cin >> y;
	}
	void Display()
	{
		cout << "\nCartesian coordinates: x = " << x << " y = "<<y;
	}
};


class Polar
{
	double r;
	double fi;
public:
	Polar(const Cartesian& c)
	{
		r = sqrt(c.x*c.x + c.y*c.y);
		fi = atan(c.y / c.x)*57.296;
	}
	void Display()
	{
		cout << "\nPolar coordinates: r = " << r << " fi = " << fi<<" degrees";
	}
};


int main()
{
	Cartesian cr;
	cr.SetCoordinate();
	cr.Display();
	Polar pr(cr);
	pr.Display();
}	

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment