| < Day Day Up > |
|
The relational operators are used to compare one object to another. The trick to implementing a relational operator is to pick one or more attributes from the set of class data members upon which the comparison will be made. Essentially, you as the programmer have to ask: 'What does it mean for one object to be less than or greater than another?'
The relational operator function can be implemented as a class member function or as a non-member function. The member function version takes one argument and the non-member function version takes two arguments. The non-member version must either be declared to be a friend of the particular class for which it is being implemented or the class must supply adequate accessor and mutator functions. Example 14.7 gives the class declaration of the Person class yet again extended to include declarations for each of the relational operators.
Listing 14.7: person.h
1 #ifndef __Person_H 2 #define __Person_H 3 #include <iostream> 4 #include <fstream> 5 using namespace std; 6 7 class Person{ 8 public: 9 Person(char* _f_name = "John", char* _m_name = "M", 10 char* _l_name = "Doe", char _sex = 'M', int _age = 18); 11 ~Person(); 12 Person(Person& person); 13 Person&operator=(Person& rhs); 14 void setFirstName(char* f_name); 15 void setMiddleName(char* m_name); 16 void setLastName(char* l_name); 17 void setAge(int age); 18 void setSex(char sex); 19 char* getFullName(); 20 char* getFirstName(); 21 char* getMiddleName(); 22 char* getLastName(); 23 char getSex(); 24 int getAge(); 25 friend ofstream& operator<<(ofstream& out, Person& p); 26 friend ifstream& operator>>(ifstream& in, Person& p); 27 friend ostream& operator<<(ostream& out, Person& p); 28 bool operator<(Person& rhs); 29 bool operator>(Person& rhs); 30 bool operator<=(Person& rhs); 31 bool operator>=(Person& rhs); 32 private: 33 char* f_name; 34 char* l_name; 35 char* m_name; 36 char sex; 37 int age; 38 char* full_name; 39 bool name_changed; 40 }; 41 #endif
The attribute selected for comparison was age. Example 14.8 gives the code showing the implementation of all four relational operators. These operators are implemented as Person class member functions and appear in the person.cpp file.
Listing 14.8: overloaded relational operator implementation
1 /******************************************************* 2 Overloaded Relational Operators 3 ******************************************************/ 4 bool Person::operator<(Person& rhs){return (age < rhs.age);} 5 6 bool Person::operator>(Person& rhs){return (age > rhs.age);} 7 8 bool Person::operator<=(Person& rhs){return (age <= rhs.age);} 9 10 bool Person::operator>=(Person& rhs){return (age >= rhs.age);}
Example 14.9 shows a main() function putting one of the overloaded relational operators to the test.
Listing 14.9: main.cpp
1 #include <iostream> 2 using namespace std; 3 #include "person.h" 4 5 int main(){ 6 7 Person Rick("Rick", "Warren", "Miller", 'M', 41); 8 Person Bob("Bob", "J", "Jones", 'M', 25); 9 10 cout<<Rick<<endl; 11 cout<<Bob<<endl; 12 13 if(Bob < Rick) 14 cout<<Bob<<" is younger than "<<Rick<<endl; 15 else 16 cout<<Rick<<" is younger than "<<Bob<<endl; 17 18 return 0; 19 }
And Figure 14-5 shows the results of running example 14.9.
Figure 14-5: Results of Running Example 14.9
Notice on line 13 of Example 14.9 how the two Person objects Rick and Bob are used in the if statement. Overloading relational operators, in situations where it makes sense to do so, results in cleaner, more natural code. Before moving on to the next section I should speak a little more to example 14.8. Notice in each of the relational operator functions how they all boil down to the use of a relational operator on a fundamental data type. The result of the comparison is simply returned.
| < Day Day Up > |
|