This is a C++ program. Create a class Polar that represents the points on the pl
ID: 3767722 • Letter: T
Question
This is a C++ program. Create a class Polar that represents the points on the plain as polar coordinates (radius and angles). Create an overloaded + operator for addition of two Polar quantities. "Adding" two points on the plain can be accomplished by adding their X coordinates and then adding their Y coordinates. This gives the X and Y coordinates of the "answer." Thus you'll need to convert two sets of polar coordinates to rectangular coordinates, add them, then convert the resulting rectangular representation back to polar.
Use the following main function to test the class Polar:
int main()
{
Polar p1(10.0, 0.0); //line to the right
Polar p2(10.0, 1.570796325); //line straight up
Polar p3; //uninitialized Polar
p3 = p1 + p2; //add two Polars
cout << " p1="; p1.display(); //display all Polars
cout << " p2="; p2.display();
cout << " p3="; p3.display();
cout << endl;
return 0;
}
Output
p1=(10, 0)
p2=(10, 1.5708)
p3=(14.1421, 0.785398)
Explanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
class Polar
{
private:
double radius;
double angle;
double getx()
{
return radius*cos(angle);
}
double gety()
{
return radius*sin(angle);
}
public:
Polar()
{
radius=0.0;
angle=0.0;
}
Polar(double r, double a)
{
radius=r;
angle=a;
}
void display()
{
cout << "(" << radius<< ", " << angle << ")";
}
Polar operator + (Polar p2)
{
double x = getx() + p2.getx();
double y = gety() + p2.gety();
double r = sqrt(x*x + y*y);
double a = atan(y/x);
return Polar(r, a);
}
};
int main()
{
Polar p1(10.0, 0.0);
Polar p2(10.0, 1.570796325);
Polar p3;
p3 = p1 + p2;
cout << " p1="; p1.display();
cout << " p2="; p2.display();
cout << " p3="; p3.display();
cout << endl;
system (“pause”);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.