Construct a class named Coord containing two double-precision data members named
ID: 3860783 • Letter: C
Question
Construct a class named Coord containing two double-precision data members named xval and yval, used to store the x and y values of a point in rectangular coordinates. The class functions should include constructor and display functions and a friend function named convPol(). The convPol() function should accept two double-precision numbers representing a point in polar coordinates and convert them into rectangular coordinates. For conversion from polar to rectangular coordinates, use these formulas:
x=r Cos 0
y= r Sin 0
Include the class into a working program and exercise all functions.
Plz, use a C++ code
Explanation / Answer
PROGRAM CODE:
#include <iostream>
#include <cmath>
using namespace std;
class Coord
{
private:
double xVal, yVal;
public:
Coord(double x, double y)
{
xVal = x;
yVal = y;
}
void setX(double x)
{
xVal = x;
}
void setY(double y)
{
yVal = y;
}
double getX()
{
return xVal;
}
double getY()
{
return yVal;
}
//declaring the friend function
friend void convPol(double &r, double &theta);
};
//defining the friend function
void convPol(double &r, double &theta)
{
double x = r*cos(theta);
double y = r*sin(theta);
r = x;
theta = y;
}
int main() {
double r = 5, theta = 30;
double x = r, y = theta;
convPol(x, y);
Coord coord(x, y);
cout<<"X= "<<coord.getX()<<" Y= "<<coord.getY()<<endl;
return 0;
}
OUTPUT: