C++/Overload/Compare

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Define

  
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
class Employee {
  string name;
  unsigned number;
public:
  Employee() { name = ""; number = 0; }
  Employee(string n, unsigned num) {
    name = n;
    number = num;
  }
  string get_name() { return name; }
  unsigned get_number() { return number; }
};
void show(vector<Employee> vect) {
  vector<Employee>::iterator itr;
  for(itr=vect.begin(); itr != vect.end(); ++itr)
    cout << itr->get_number() << " " << itr->get_name() << endl;;
}
bool operator<(Employee a, Employee b){
  return a.get_number() < b.get_number();
}
int main()
{
  vector<Employee> employeeList;
  employeeList.push_back(Employee("A", 9));
  employeeList.push_back(Employee("B", 8));
  employeeList.push_back(Employee("C", 6));
  employeeList.push_back(Employee("D", 1));
  show(employeeList);
  sort(employeeList.begin(), employeeList.end());
  show(employeeList);
  return 0;
}


overloaded "==" operator compares strings

  
#include <iostream>
using namespace std;
#include <string.h>
class String{
   private:
      enum { SZ = 80 };
      char str[SZ];
   public:
      String(){ strcpy(str, ""); }
      String( char s[] ){ strcpy(str, s); }
      void display() const{ cout << str; }
      void getstr(){ cin.get(str, SZ); }
      bool operator == (String ss) const{
         return ( strcmp(str, ss.str)==0 ) ? true : false;
      }
   };
int main(){
   String s1 = "yes";
   String s2 = "no";
   String s3;
   cout << "\nEnter "yes" or "no": ";
   s3.getstr();
   if(s3==s1)
      cout << "You typed yes\n";
   else if(s3==s2)
      cout << "You typed no\n";
   else
      cout << "You didn"t follow instructions\n";
   return 0;
}


overloaded "

  
#include <iostream>
using namespace std;
class Distance
   {
   private:
      int feet;
      float inches;
   public:
      Distance() : feet(0), inches(0.0){ }
      Distance(int ft, float in) : feet(ft), inches(in) {  }
      void getdist(){
         cout << "\nEnter feet: ";  cin >> feet;
         cout << "Enter inches: ";  cin >> inches;
      }
      void showdist() const{ 
         cout << feet << "\"-" << inches << "\""; 
      }
      bool operator < (Distance) const;  
};
bool Distance::operator < (Distance d2) const{
   float bf1 = feet + inches/12;
   float bf2 = d2.feet + d2.inches/12;
   return (bf1 < bf2) ? true : false;
}
int main(){
   Distance dist1;                 
   dist1.getdist();                
   Distance dist2(6, 2.5);         
   cout << "\ndist1 = ";  dist1.showdist();
   cout << "\ndist2 = ";  dist2.showdist();
   if( dist1 < dist2 )             
      cout << "\ndist1 is less than dist2";
   else
      cout << "\ndist1 is greater than (or equal to) dist2";
   cout << endl;
   return 0;
}


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;
 }


Overload the < and >

" src="http://www.java2s.com/Code/CppImages/Overloadtheand.PNG">


 
#include <iostream>
using namespace std;
class MyClass {
  int x, y; 
public:
  MyClass() { 
     x=0; 
     y=0; 
  }
  MyClass(int i, int j) { 
     x=i; 
     y=j; 
  }
  void getXY(int &i, int &j) { 
     i=x; 
     j=y; 
  }
  int operator<(MyClass object2);
  int operator>(MyClass object2);
};
int MyClass::operator<(MyClass object2)
{
  return x<object2.x && y<object2.y; 
}
int MyClass::operator>(MyClass object2)
{
  return x>object2.x && y>object2.y;
}
 
int main()
{
  MyClass object1(10, 10), object2(5, 3);
  if(object1>object2) 
    cout << "object1 > object2\n";
  else 
    cout << "object1 <= object2 \n";
  if(object1<object2) 
    cout << "object1 < object2\n";
  else 
    cout << "object1 >= object2\n";
  return 0;
}