0% found this document useful (0 votes)
8 views5 pages

Overloading Relational Operators in C

Uploaded by

Putta Swamy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Overloading Relational Operators in C

Uploaded by

Putta Swamy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Overloading relational operators:

Overloading relational operators in


C++ allows you to define custom behavior for comparison operators like ==,
!=, <, >, <=, and >= for user-defined types (e.g., classes or structs). This
can make your objects comparable in a natural and intuitive way, particularly
useful when working with containers or algorithms that rely on comparisons.
Steps to Overload Relational Operators
1. Use the operator keyword followed by the relational operator (==, !=, <,
etc.).
2. Overload as either a member function or a non-member function.
o Member functions can access the private members of the object
directly.
o Non-member functions can be used for symmetry and often declared
as friend functions.
3. Return a bool to indicate the result of the comparison.
Example: Overloading == and <
#include <iostream>
#include <string>
using namespace std;
class Person
{
private:
string name;
int age;
public:
// Constructor
Person(string n, int a) : name(n), age(a) {}
// Overload '==' operator
bool operator==(const Person& other) const
{
return (name == other.name && age == other.age);
}
// Overload '<' operator
bool operator<(const Person& other) const
{
return age < other.age;
}
// Display function
void display() const
{
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main()
{
Person p1("Alice", 25);
Person p2("Bob", 30);
Person p3("Alice", 25);
// Compare using '=='
if (p1 == p3)
cout << "p1 is equal to p3" << endl;
else
cout << "p1 is not equal to p3" << endl;
// Compare using '<'
if (p1 < p2)
cout << "p1 is younger than p2" << endl;
else
cout << "p1 is not younger than p2" << endl;

return 0;
}
Explanation
1. Overloading ==:
o bool operator==(const Person& other) const
o Compares both name and age to check if the objects are equal.
o The const keyword ensures that the method doesn’t modify the
objects.
2. Overloading <:
o bool operator<(const Person& other) const
o Compares the age of the two Person objects to determine their
relative ordering.
3. Output:
p1 is equal to p3
p1 is younger than p2
Using Friend Functions for Relational Operators
If symmetry is needed, such as comparing a non-class object with a class
object, you can use friend functions. Here’s an example:
class Person {
private:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {}
// Friend function to overload '!='
friend bool operator!=(const Person& p1, const Person& p2);
// Friend function to overload '>='
friend bool operator>=(const Person& p1, const Person& p2);
void display() const {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
// Overload '!=' operator
bool operator!=(const Person& p1, const Person& p2) {
return !(p1.name == p2.name && p1.age == p2.age);
}
// Overload '>=' operator
bool operator>=(const Person& p1, const Person& p2) {
return p1.age >= p2.age;
}
Guidelines for Relational Operator Overloading
1. Consistency:
o When overloading relational operators, ensure consistency
among them. For example, if == is overloaded, != should also be
defined.
o Similarly, if < is overloaded, overload >, <=, and >= for
complete functionality.
2. Symmetry:
o Relational operators should behave symmetrically (e.g., a < b
should imply !(b < a)).
3. Return Type:
o Always return a bool for relational operators.
4. Const Correctness:
o Use const to ensure the objects being compared are not
modified.
Extending: Overloading All Relational Operators
Here’s an example of overloading all six relational operators for a Rectangle
class based on area:
#include <iostream>
using namespace std;
class Rectangle {
private:
int width;
int height;
public:
Rectangle(int w, int h) : width(w), height(h) {}
int area() const { return width * height; }
bool operator==(const Rectangle& other) const { return area() ==
other.area(); }
bool operator!=(const Rectangle& other) const { return area() !=
other.area(); }
bool operator<(const Rectangle& other) const { return area() <
other.area(); }
bool operator>(const Rectangle& other) const { return area() >
other.area(); }
bool operator<=(const Rectangle& other) const { return area() <=
other.area(); }
bool operator>=(const Rectangle& other) const { return area() >=
other.area(); }
};
int main() {
Rectangle r1(4, 5);
Rectangle r2(3, 6);
if (r1 > r2)
cout << "r1 has a larger area than r2" << endl;
else
cout << "r1 does not have a larger area than r2" << endl;
return 0;
}
This ensures complete support for all relational operators.

You might also like