212k views
1 vote
Write program that creates a class Polar class that contains the following private member data: - float Radius, float Theta - static int Cnt Default constructors - print message Parametrized constructors - print message Copy constructors - print message Overloaded assignment operators operator =0 - print message float Overloaded operator-(Polar \& rhs) - distance between two polar numbers friend ostream \& operator < (ostream os, Polar \&rhs) - Demonstrate the followings Polar X, Y(3,pi/4), Z(5,pi/3); pi =3.14159 Output X,Y,Z float dist =Z−Y (print result) Create array of 3 polar numbers on the heap Set the values of polar numbers on the heap to (3,pi/3),(4,pi/2),(5, pi/4) Output all Polar numbers on heap Output number of Polar numbers in play

User Urmaul
by
7.6k points

1 Answer

4 votes

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;
}

User NoComprende
by
7.6k points