Click Here!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


4.5 Overload relational and equality operators?

Problem

For many classes that describe new data types, I want to specify an order for the elements of a class. For example, I am always able to say whether a date is greater than or less than another date. I want to use this order in my applications and I want to overload relational and equality operators (<, <=, >, >=, ==, =!) to specify that order.

Technique

Operator overloading is a powerful feature of C++. If it is consistently applied to a class, it can make the code much simpler and therefore more reliable. To overload the operators, the appropriate functions have to be added to the class. Every function will overload one operator, and together they will create the complete set of relational and equality operators.

Steps

1.  Creating a class
For this example, the DDate structure that was created in the beginning of this chapter will be transferred into the DDate class. To keep the example simple, the code creates only one member function that sets up the date. As usual, create the two files: the class declaration file (ddate.h) and the class definition file (ddate.cpp).
// ddate.h

class DDate{

private:
  int dday;
  int mmonth;
  int yyear;.
public:
  // constructor DDate(void);
  // destructor ~DDate(void);
  // creates a new DDate object
  // for the specified day, month, and year
  void SetDate(int, int, int);};

// ddate.cpp

#include “ddate.h”

// constructor
DDate::DDate()
{
}

// destructor
DDate::~DDate()
{
}

// creates a new DDate object
// for the specified day, month, and year
void DDate::SetDate(int InDay, int InMonth, int InYear)
{
  dday = InDay;
  mmonth = InMonth;
  yyear = InYear;
}


The class declaration has a private section with the dday, the mmonth, and the yyear integer variables. This section is similar to the structure of How-To 4.1. The public section contains the constructor, the destructor, and the SetDate member function. The SetDate function sets up the date based on the day, month, and year arguments.
2.  Overloading the relational and equality operators
The syntax of the declaration of overloaded operators declaration is similar to the syntax of declaration of the overloaded operators in How-To 4.4. The syntax of the declaration is similar to the syntax of the declaration of a function. The difference is that instead of a function name, the keyword operator and the actual operator are used. For example, to declare the overloaded > operator for the DDate class use
bool operator>(DDate);

You must specify the return value of the functions, which is bool, because the overloaded operators must have the same behavior as the regular relational and equality operators. The code will need to use the operators in logical expressions and in statements such as
if (Date1 > Date2) Destroy_All();

Therefore, the class declaration will be the following:
// ddate.h

class DDate{

private:
  int dday;
  int mmonth;
  int yyear;

public:
  // constructor
  DDate(void);
  // destructor
  ~DDate(void);
  // creates a new DDate object
  // for the specified day, month, and year
  void SetDate(int, int, int);
  // overloaded operator >
  bool operator>(DDate);
  // overloaded operator >=
  bool operator>=(DDate);
  // overloaded operator
  < bool operator<(DDate);
  // overloaded operator <=
  bool operator<=(DDate);
  // overloaded operator ==
  bool operator==(DDate);
  // overloaded operator !=
  bool operator!=(DDate);

};

The class definition will need quite trivial functions. The following code covers three of the six operators (<, >, and ==). You can add the rest to prove your knowledge of operator overloading.
// constructor and destructor
DDate::DDate()
{}
DDate::~DDate()
{}

// overloaded operator >
bool DDate::operator>(DDate InDDate)
{
  if (yyear > InDDate.yyear) return true;
  if (yyear < InDDate.yyear) return false;

  // years are equal
  if (mmonth > InDDate.mmonth) return true;
  if (mmonth < InDDate.mmonth) return false;

  // months are equal
  if (dday > InDDate.dday) return true;
  return false;

}

// overloaded operator <
bool DDate::operator<(DDate InDDate)
{
    if (yyear < InDDate.yyear) return true;
    if (yyear > InDDate.yyear) return false;

    // years are equal
    if (mmonth < InDDate.mmonth) return true;
    if (mmonth > InDDate.mmonth) return false;

    // months are equal
    if (dday < InDDate.dday) return true;
    return false;
}

// overloaded operator ==
bool DDate::operator==(DDate InDDate)
{
   if (yyear != InDDate.yyear) return false;

   // years are equal
   if (mmonth != InDDate.mmonth) return false;

   // months are equal
   if (dday == InDDate.dday) return true;
   return false;
}
// Readers can add the code to the functions below
bool DDate::operator<=(DDate InDDate)
{}
bool DDate::operator>=(DDate InDDate)
{}
bool DDate::operator!=(DDate InDDate)
{}

All functions defined in the preceding are similar to the other binary functions. The major question to be answered is “What is the order of the parameters in these functions?” For example, when the > (greater than) operator is defined, is InDDate the first or the second parameter? If you write
DDate Date1, Date2;
if (Date1 > Date2) do_something;

will the InDDate parameter be replaced with the Date1 argument or the Date2 argument?
The rule is simple: The parameter in any function that represents an overloaded binary operator is replaced with the second argument. The first argument is considered to be an object of the defined class itself. Therefore, in this example the InDDate parameter in the functions
bool DDate::operator>(DDate InDDate)
bool DDate::operator<(DDate InDDate)
bool DDate::operator==(DDate InDDate)

will be replaced with the Date2 argument for the statements
if (Date1 > Date2) do_something;
if (Date1 < Date2) do_something;
if (Date1 == Date2) do_something;

To prove it we have to create a simple test example.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.