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:
#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();
}
Comments
Leave a comment