Free Hosting

Overloading the comparison operators

Overloading the comparison operators is simple once you’ve learned how to overload the arithmetic operators.
Because the comparison operators are all binary operators that do not modify their operands, we will make our overloaded comparison operators friend functions.
Here’s an example Point class from the previous lesson with an overloaded operator== and operator!=.
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
class Point
{
private:
    double m_dX, m_dY, m_dZ;
public:
    Point(double dX=0.0, double dY=0.0, double dZ=0.0)
    {
    m_dX = dX;
    m_dY = dY;
    m_dZ = dZ;
    }
    friend bool operator== (Point &cP1, Point &cP2);
    friend bool operator!= (Point &cP1, Point &cP2);
    double GetX() { return m_dX; }
    double GetY() { return m_dY; }
    double GetZ() { return m_dZ; }
};
bool operator== (Point &cP1, Point &cP2)
{
    return (cP1.m_dX == cP2.m_dX &&
            cP1.m_dY == cP2.m_dY &&
            cP1.m_dZ == cP2.m_dZ);
}
bool operator!= (Point &cP1, Point &cP2)
{
    return !(cP1 == cP2);
}
The code here should be straightforward. Because the result of operator!= is the opposite of operator==, we define operator!= in terms of operator==, which helps keep things simpler, more error free, and reduces the amount of code we have to write.
It doesn’t really make sense to overload operator> or operator< for the Point class. What does it mean for a 3d point to be greater or less than another point? Greater than or less than isn't a concept we normally apply to 3d points, so it's better not to include those operators in the Point class, because the results of the operators (whatever you define them to be) would not be intuitive.
Here's a different example with an overloaded operator>, operator<, operator>=, and operator<=:
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
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
#include<iostream.h>
#include<conio.h>
class Distance
{
private:
 int m,cm;
 int convert_Distance(int met,int cemt)
 {
  return((met*100)+cemt);
 }
public:
 Distance()
 {
  set_Distance(0,0);
 }
 Distance(int mm,int cmt)
 {
  set_Distance(mm,cmt);
 }
 Distance(int cmt)
 {
  set_Distance(cmt);
 }
 void set_Distance(int mt,int cmt)
 {
  set_Distance(convert_Distance(mt,cmt));
 }
 void set_Distance(int cmt)
 {
  m=cmt/100;
  cm=cmt%100;
 }
 bool operator<(Distance De)
 {
  return(convert_Distance(m,cm)<convert_Distance(De.m ,De.cm ));
 }
 int operator>(Distance De)
 {
  return(convert_Distance(m,cm)>convert_Distance(De.m ,De.cm ));
 }
 Distance operator+(Distance De)
 {
  Distance temp;
  temp.m=m+De.m;
  temp.cm=De.cm;
  return(temp);
 }
 friend ostream & operator<<(ostream &o,Distance &t)
 {
           o<<t.m<<":::"<<t.cm;
           return o;
           }
     friend istream & operator>>(istream &i,Distance &t)
 {
           i>>t.m>>t.cm;
           return i;
           }
};

int main()
{
 Distance d1(4500);
 Distance d2(45,300);
 cout<<"Distance 1=   "<<d1<<endl;
 cout<<"Distance 2=   "<<d2<<endl;
 if(d1<d2)
   cout<<"Distance 1 is smaller then Distance 2"<<endl;
 return 0; 
}

This is also pretty straightforward.
Note that there is some redundancy here as well. operator> and operator<= are logical opposites, so one could be defined in terms of the other. operator< and operator>= are also logical opposites, and one could be defined in terms of the other. In this case, I chose not to do so because the function definitions are so simple, and the comparison operator in the function name line up nicely with the comparison operator in the return statement.
Distance d1(4500); Distance d2(45,300); cout<<"Distance 1= "< and operator<= are logical opposites, so one could be defined in terms of the other. operator< and operator>= are also logical opposites, and one could be defined in terms of the other. In this case, I chose not to do so because the function definitions are so simple, and the comparison operator in the function name line up nicely with the comparison operator in the return statement.

0 comments:

Blogger Template by Clairvo