-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticep52_opoverloading_1.cpp
More file actions
76 lines (64 loc) · 1.55 KB
/
practicep52_opoverloading_1.cpp
File metadata and controls
76 lines (64 loc) · 1.55 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
// #include <iostream>
// using namespace std;
// class abc{
// private:
// int x;
// int y;
// int z;
// public:
// abc(int x, int y, int z){
// this->x=x;
// this->y=y;
// this->z=z;
// };
// void display(){
// cout<<x<<" ";
// cout<<y<<" ";
// cout<<z<<" "<<endl;
// };
// void operator-(){
// x=-x;
// y=-y;
// z=-z;
// };
// };
// int main(){
// abc obj(10,20,30);
// cout<<"Before operator: ";
// obj.display();
// cout<<endl;
// -obj;
// cout<<"After operator: ";
// obj.display();
// cout<<endl;
// return 0;
// }
#include <iostream>
#include <cmath> // for M_PI
using namespace std;
class Circle {
private:
double radius;
public:
// Constructor to initialize the radius
Circle(double r) {
radius = r;
}
// Function to calculate the area of the circle
double area() const {
return M_PI * radius * radius;
}
// Overload + operator to add the areas of two circles
double operator+(const Circle& c) {
return this->area() + c.area();
}
};
int main() {
// Create two circle objects with different radii
Circle circle1(5.0); // Circle with radius 5
Circle circle2(3.0); // Circle with radius 3
// Calculate and display the sum of their areas
double totalArea = circle1 + circle2;
cout << "Sum of areas of the two circles: " << totalArea << endl;
return 0;
}