Final answer:
This program creates a class called Polar in C++ that represents points in polar coordinates. It includes constructors, assignment operators, and an overloaded subtraction operator. It also demonstrates the usage of the class by creating Polar objects, calculating distances, and working with an array of Polar numbers.
Step-by-step explanation:
Here is an example implementation of the Polar class in C++
#include <iostream>
#include <cmath>
using namespace std;
class Polar {
private:
float Radius;
float Theta;
static int Cnt;
public:
Polar() {
Radius = 0;
Theta = 0;
Cnt++;
cout << "Default constructor called." << endl;
}
Polar(float r, float t) {
Radius = r;
Theta = t;
Cnt++;
cout << "Parametrized constructor called. Radius: " << Radius << ", Theta: " << Theta << endl;
}
Polar(const Polar &p) {
Radius = p.Radius;
Theta = p.Theta;
Cnt++;
cout << "Copy constructor called. Radius: " << Radius << ", Theta: " << Theta << endl;
}
Polar &operator=(const Polar &p) {
Radius = p.Radius;
Theta = p.Theta;
cout << "Assignment operator called. Radius: " << Radius << ", Theta: " << Theta << endl;
return *this;
}
float operator-(const Polar &rhs) {
float dx = rhs.Radius * cos(rhs.Theta) - Radius * cos(Theta);
float dy = rhs.Radius * sin(rhs.Theta) - Radius * sin(Theta);
return sqrt(dx * dx + dy * dy);
}
friend ostream &operator<<(ostream &os, const Polar &p) {
os << "Radius: " << p.Radius << ", Theta: " << p.Theta << endl;
return os;
}
};
int Polar::Cnt = 0;
int main() {
Polar X;
Polar Y(3, 3.14 / 4);
Polar Z(5, 3.14 / 3);
float dist = Z - Y;
cout << "Distance between Z and Y: " << dist << endl;
Polar* arr = new Polar[3];
arr[0] = Polar(3, 3.14 / 3);
arr[1] = Polar(4, 3.14 / 2);
arr[2] = Polar(5, 3.14 / 4);
for (int i = 0; i < 3; i++) {
cout << "Polar "+to_string(i+1)+": " << arr[i];
}
cout << "Number of Polar numbers in play: " << Polar::Cnt << endl;
delete[] arr;
return 0;
}