-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_overridding.cpp.cpp
More file actions
282 lines (249 loc) 路 7.18 KB
/
Copy pathfunction_overridding.cpp.cpp
File metadata and controls
282 lines (249 loc) 路 7.18 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* Jan25 CSE108
* Practice Session on Constructor, Destructor, and Function Overloading
*
*
* Instructions:
* * Implement the Circle class with the following functionalities:
* * - Default constructor
* * - Constructor with parameters for name, coordinates, and radius
* * - Copy constructor
* * - Method to calculate area and circumference
* * - Method to check if a point is inside the circle
* * - Method to check if another circle is inside, intersects, or outside this circle
* * - Method to translate the circle's position
* * - Method to scale the circle
* * - Method to display circle properties
* * - Setter and getter methods for name and color
* *
* * After implementing the class, test it using the provided main function.
* * Expected output is provided in the comments at the end of the code.
* * Be ready to explain the generated output to the evaluator.
*
*
*
* **---**
*/
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
#define PI 3.14159
// Circle class definition
class Circle
{
double x, y; // Coordinates of the center of the circle
double radius; // Radius of the circle
char *name; // Name of the circle
char *color; // Color of the circle
public:
// Default constructor to initialize a circle with default values
Circle()
{
x = 0, y = 0;
radius = 0;
name = nullptr;
color = nullptr;
}
// Constructor with parameters for a circle's name, coordinates, and radius
Circle(char *name, double x, double y, double r)
{
if (name != nullptr)
{
int len = strlen(name);
this->name = new char[len + 1];
strcpy(this->name, name);
this->x = x;
this->y = y;
radius = r;
color = nullptr;
}
}
// Create a new circle from an existing one
Circle(const Circle &c)
{
if (c.name != nullptr)
{
int len = strlen(c.name);
this->name = new char[len + 1];
strcpy(this->name, c.name);
}
x = c.x;
y = c.y;
radius = c.radius;
if (c.color != nullptr)
{
int length = strlen(c.color);
this->color = new char[length + 1];
strcpy(this->color, c.color);
}
}
// Method to calculate and return the area of the circle
double area()
{
return PI * radius * radius;
}
// Method to calculate and return the circumference of the circle
double circumference()
{
return 2 * PI * radius;
}
double dist(double x, double y, double cx, double cy)
{
double dx = cx - x;
double dy = cy - y;
return sqrt(dx * dx + dy * dy);
}
// Method to check if a point (x, y) is inside the circle
void contains(double x, double y)
{
double distance = dist(x, y, this->x, this->y);
if (distance < radius)
cout << "Point (" << x << ", " << y << ") is inside the circle " << name << "." << endl;
else if (distance > radius)
cout << "Point (" << x << ", " << y << ") is outside the circle " << name << "." << endl;
else
cout << "Point (" << x << ", " << y << ") is on the circle " << name << "." << endl;
}
// Method to check if another circle is inside, intersects, or outside this circle
void contains(Circle c)
{
double d = dist(x, y, c.x, c.y);
if (d + c.radius < radius)
cout << "Circle " << c.name << " is inside the circle " << name << "." << endl;
else if (d > radius + c.radius)
cout << "Circle " << c.name << " is outside the circle " << name << "." << endl;
else
cout << "Circle " << c.name << " intersects with the circle " << name << "." << endl;
}
// Method to translate the circle's position by a distance (dx, dy)
void translate(double dx, double dy)
{
x += dx;
y += dy;
}
// Method to translate the circle in a given direction (up, down, left, right)
void translate(string dir, double dist)
{
if (dir == "up")
y += dist;
else if (dir == "down")
y -= dist;
else if (dir == "left")
x -= dist;
else if (dir == "right")
x += dist;
}
// Method to scale the circle by a given factor
void scale(double factor)
{
radius *= factor;
}
// Method to display circle properties (name, center, radius, area, cirumference, color.)
void display()
{
cout << "Circle Name: " << this->name << "\nCenter: (" << x << ", " << y << ")" << "\nRadius: " << radius << "\nCircumference: " << circumference() << "\nArea: " << area() << "\nColor: " << color << endl;
}
// Setter method for the name of the circle
void setName(char *name)
{
delete[] this->name;
int len = strlen(name);
this->name = new char[len + 1];
strcpy(this->name, name);
}
// Setter method for the color of the circle
void setColor(char *color)
{
delete[] this->color;
int len = strlen(color);
this->color = new char[len + 1];
strcpy(this->color, color);
}
// Getter method for the circle's name
char *getName()
{
return name;
}
// Getter method for the circle's color
char *getColor()
{
return color;
}
// Destructor to clean up dynamically allocated memory
~Circle()
{
delete[] name;
delete[] color;
}
};
// Main function to test the Circle class
int main()
{
Circle c1("C1", 0, 0, 5); // Create a circle with name C1 at (0,0) with radius 5
Circle c2("C2", 6, 4, 1);
Circle c3("C3", 2, 2, 3);
Circle c4("C4", 1, 2, 2);
c1.setColor("Red"); // Set color of circle C1 to Red
c2.setColor("Blue");
c1.display(); // Display properties of circle C1
cout << endl;
c2.display();
cout << endl;
// Test
c1.contains(3, 5); // Point (3, 5) should be outside circle C1
c1.contains(2, 2); // Point (2, 2) should be inside circle C1
c1.contains(c2); // Check if circle C2 is inside, intersects, or outside circle C1
c1.contains(c3);
c1.contains(c4);
// Translate circle C1 by (2, 3)
c1.translate(2, 3);
c1.display();
cout << endl;
// Translate circle C1 upwards by 2 units
c1.translate("up", 2);
c1.display();
cout << endl;
// Scale circle C1 by a factor of 2
c1.scale(2);
c1.display();
return 0;
}
// Expected Output:
/*
Circle Name: C1
Center: (0, 0)
Radius: 5
Area: 78.5397
Circumference: 31.4159
Color: Red
Circle Name: C2
Center: (6, 4)
Radius: 1
Area: 3.14159
Circumference: 6.28318
Color: Blue
Point (3, 5) is outside the circle C1.
Point (2, 2) is inside the circle C1.
Circle C2 is outside the circle C1.
Circle C3 intersects with the circle C1.
Circle C4 is inside the circle C1.
Circle Name: C1
Center: (2, 3)
Radius: 5
Area: 78.5397
Circumference: 31.4159
Color: Red
Circle Name: C1
Center: (2, 5)
Radius: 5
Area: 78.5397
Circumference: 31.4159
Color: Red
Circle Name: C1
Center: (2, 5)
Radius: 10
Area: 314.159
Circumference: 62.8318
Color: Red
*/