-
Notifications
You must be signed in to change notification settings - Fork 0
/
l4.4.cpp
100 lines (69 loc) · 1.64 KB
/
l4.4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Task #4:
* WAP in CPP to convert the co-ordinates from Cartesian system to polar system using the concept of
* user defined to user defined conversion by writing conversion routine in destination class.
* i.e. using parameterized constructor
*/
#include <iostream>
#include <cmath>
class Cartesian {
public:
Cartesian();
Cartesian(float, float);
~Cartesian();
void printData();
float return_x();
float return_y();
private:
float x;
float y;
};
class Polar {
public:
Polar();
Polar(Cartesian);
~Polar();
void printData();
private:
float r;
float theta;
};
Cartesian::Cartesian() : x{0.0}, y{0.0}{
}
Cartesian::Cartesian(float t_x, float t_y){
x = t_x;
y = t_y;
}
Cartesian::~Cartesian(){
}
float Cartesian::return_x(){
return x;
}
float Cartesian::return_y(){
return y;
}
void Cartesian::printData(){
std::cout << "\nCoordinates in Cartesian form (x, y): (" << x << ", " << y << ")" << std::endl;
}
Polar::Polar() : r{0.0}, theta{0.0}{
}
Polar::Polar(Cartesian c){
r = sqrt(pow(c.return_x(), 2) + pow(c.return_y(), 2));
theta = atan(c.return_x() / c.return_y());
}
Polar::~Polar(){
}
void Polar::printData(){
std::cout << "Coordinates in Polar form (r, theta): (" << r << ", " << theta << ")" << std::endl;
}
int main(){
Polar *polar = new Polar;
Cartesian *cartesian = new Cartesian;
*cartesian = Cartesian(3, 4); //(x, y)
*polar = *cartesian;
cartesian->printData();
polar->printData();
delete polar;
delete cartesian;
return 0;
}