Overloading Equality and Inequality Operators
#include <iostream>
using namespace std;
class CDate
{
private:
int m_nDay;
int m_nMonth;
int m_nYear;
void AddDays (int nDaysToAdd);
void AddMonths (int nMonthsToAdd);
void AddYears (int m_nYearsToAdd);
public:
CDate (int nDay, int nMonth, int nYear)
: m_nDay (nDay), m_nMonth (nMonth), m_nYear (nYear) {};
void DisplayDate ()
{
cout << m_nDay << " / " << m_nMonth << " / " << m_nYear << endl;
}
// integer conversion operator
operator int();
// equality operator that helps with: if (mDate1 == mDate2)...
bool operator == (const CDate& mDateObj);
// overloaded equality operator that helps with: if (mDate == nInteger)
bool operator == (int nDateNumber);
// inequality operator
bool operator != (const CDate& mDateObj);
// overloaded inequality operator for integer types
bool operator != (int nDateNumber);
};
CDate::operator int()
{
return ((m_nYear * 10000) + (m_nMonth * 100) + m_nDay);
}
// equality operator that helps with if (mDate1 == mDate2)...
bool CDate::operator == (const CDate& mDateObj)
{
return ( (mDateObj.m_nYear == m_nYear)
&& (mDateObj.m_nMonth == m_nMonth)
&& (mDateObj.m_nDay == m_nDay) );
}
bool CDate::operator == (int nDateNumber)
{
return nDateNumber == (int)*this;
}
// inequality operator
bool CDate::operator != (const CDate& mDateObj)
{
return !(this->operator== (mDateObj));
}
bool CDate::operator != (int nDateNumber)
{
return !(this->operator == (nDateNumber));
}
void CDate::AddDays (int nDaysToAdd)
{
m_nDay += nDaysToAdd;
if (m_nDay > 30)
{
AddMonths (m_nDay / 30);
m_nDay %= 30; // rollover 30th -> 1st
}
}
void CDate::AddMonths (int nMonthsToAdd)
{
m_nMonth += nMonthsToAdd;
if (m_nMonth > 12)
{
AddYears (m_nMonth / 12);
m_nMonth %= 12; // rollover dec -> jan
}
}
void CDate::AddYears (int m_nYearsToAdd)
{
m_nYear += m_nYearsToAdd;
}
int main ()
{
CDate mDate1 (25, 6, 2008);
mDate1.DisplayDate ();
CDate mDate2 (23, 5, 2009);
mDate2.DisplayDate ();
if (mDate2 != mDate1)
cout << "The two dates are not equal... As expected!" << endl;
CDate mDate3 (23, 5, 2009);
mDate3.DisplayDate ();
if (mDate3 == mDate2)
cout << "mDate3 and mDate2 are evaluated as equals" << endl;
// Get the integer equivalent of mDate3 using operator int()
int nIntegerDate3 = mDate3;
cout << nIntegerDate3<< endl;
// Use overloaded operator== (for int comparison)
if (mDate3 == nIntegerDate3)
cout << "The integer and mDate3 are equivalent" << endl;
// Use overloaded operator != that accepts integers
if (mDate1 != nIntegerDate3)
cout << "The mDate1 is inequal to mDate3";
return 0;
}
Related examples in the same category