Users Online
· Guests Online: 43
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
Operator Overloading Program in C++
Operator Overloading Program in C++
This C++ program overloads the == operator in the Circle Class. The Circle is a user-defined class with radius as its only data member and with provision of member function which computes the area of the circle. The overloaded == operator compares two circles on the basis of their radii, i.e. the circle would be equal to another circle if the radii of two circles are equal.
Here is the source code of the C++ program which overloads the == operator in the Circle Class. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
-
/*
-
* C++ Program to overload == operator in circle class
-
*/
-
-
#include <iostream>
-
using namespace std;
-
-
class Circle {
-
private:
-
float radius;
-
public:
-
Circle(float r = 0) : radius(r) { }
-
void changeRadius(int radius) { this->radius = radius; }
-
float getRadius() { return radius; }
-
float getArea() const { return 3.14 * radius * radius; }
-
bool operator==(Circle a);
-
};
-
-
inline bool Circle::operator==(Circle a)
-
{
-
return this->radius == a.radius;
-
}
-
-
int main()
-
{
-
Circle A(10), B(20), C(10);
-
-
cout << "Circle A : Radius = " << A.getRadius() << " units, Area = "
-
<< A.getArea() << " sq. units\n";
-
cout << "Circle B : Radius = " << B.getRadius() << " units, Area = "
-
<< B.getArea() << " sq. units\n";
-
cout << "Circle C : Radius = " << C.getRadius() << " units, Area = "
-
<< C.getArea() << " sq. units\n";
-
cout << "A == B : ";
-
if (A == B)
-
cout << "Circles are equal.\n";
-
else
-
cout << "Circles are not equal.\n";
-
cout << "A == C : ";
-
if (A == C)
-
cout << "Circles are equal.\n";
-
else
-
cout << "Circles are not equal.\n";
-
}
$ a.out Circle A : Radius = 10 units, Area = 314 sq. units Circle B : Radius = 20 units, Area = 1256 sq. units Circle C : Radius = 10 units, Area = 314 sq. units A == B : Circles are not equal. A == C : Circles are equal.
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.