Overloading Operators in C++


Overloading operators in C++ is a very easy thing to do. The reason we overload operators is to implement, for example, +, -, !=, ==, +=, –, or I/O functionality to our objects. For example, if we wanted to compare multiple abstract objects to each other, we can overload the == operator in the class so that we can compare if two objects are the same or not. Or in another example, we can overload the >> and << stream operators (ostream and istream) to both read in and write out object data; so that instead of creating a class method to write some data, we can just write cleaner code like this: cout<<obj1<<endl;

Common operators:

Rules for operator overloading:

  1. Whenever the meaning of an operator is not obviously clear and undisputed, it should not be overloaded. Instead, provide a function with a well-chosen name. Otherwise, its best not to overload any operator.
  2. Always stick to the operator’s well-known semantics. C++ poses no limitations on the semantics of overloaded operators.
  3. Always provide all out of a set of related operations. Operators are related to each other and to other operations. If your type supports a + b, users will expect to be able to call a += b, too.

The following sample code overloads several operators mentioned above (+, -, ==, !=, >>, and <<). Pay close attention to syntax and implementation.

#include <iostream>
using namespace std;

class Distance
{
   private:
      int feet;             // 0 to infinite
      int inches;           // 0 to 12
   public:
      // required constructors
      Distance()
      {
         feet = 0;
         inches = 0;
      }
      Distance(int f, int i)
      {
         feet = f;
         inches = i;
      }

      // overloaded minus (-) operator
      Distance operator- ()
      {
         feet = -feet;
         inches = -inches;
         return Distance(feet, inches);
      }
      // overloaded plus (+) operator
      Distance operator+ (const Distance &d)
      {
         feet = d.feet + feet;
         inches = d.inches+inches;
         return Distance(feet, inches);
      }
      //overload == operator
      bool operator==(const Distance &d)
      {
          return(feet == d.feet && inches == d.inches);
      }
      //overload != operator
      bool operator!=(const Distance &d)
      {
          return(feet != d.feet && inches != d.inches);
      }
      // overloaded minus (-) operator (so we can subtract distances)
      Distance operator- (cosnt Distance &d)
      {
         feet = d.feet - feet;
         inches = d.inches - inches;
         return Distance(feet, inches);
      }
      //overload <<
      friend ostream &operator<<( ostream &output, const Distance &D )
      {
         output << "F : " << D.feet << " I : " << D.inches;
         return output;
      }
      //overload >>
      friend istream &operator>>( istream  &input, Distance &D )
      {
         input >> D.feet >> D.inches;
         return input;
      }
};
int main()
{
   Distance D1(11, 10), D2(5, 11), D3;

   cout << "Enter the value of object : " << endl;
   cin >> D3;
   cout << "First Distance : " << D1 << endl;
   cout << "Second Distance :" << D2 << endl;
   cout << "Third Distance :" << D3 << endl;
   Distance D4;
   D4 = D1 + D2;
   if(D4 != D2)
   {
       cout<<"The two distances are not equal"<<endl;
   }
   return 0;
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s