Operator Overloading || C++


1.    Overloading Unary Operator
/*Overloading Unary Operator */
#include<iostream>
#include<conio.h>
using namespace std;
class space{
              int x,y,z;
              public:
                             void getdata(int a, int b, int c);
                             void  display();
                             void operator-();
};
void space::getdata(int a, int b, int c)
{
              x=a;y=b;z=c;
}
void space::display()
{
              cout<<"x= "<<x<<endl;
              cout<<"y= "<<y<<endl;
              cout<<"z= "<<z<<endl;
}
void space::operator-()
{
              x=-x;y=-y;z=-z;
}
int main()
{
              space s;
              s.getdata(3,5,2);
              s.display();
              -s;
              cout<<"-s"<<endl;
              s.display();
              return 0;
}

2.    Overloading ++ operator on prefix.
//Overloading ++ operator
#include<iostream>
#include<conio.h>
using namespace std;
class space{
      int x,y,z;
      public:
                  space(int a , int b, int c)
                  {
                              x=a;y=b;z=c;
                  }
                  void show()
                  {
                              cout<<"x= "<<x<<" y= "<<y<<" z= "<<z<<endl;
                  }
                  void operator++()
                  {
                              x=++x;
                              y=++y;
                              z=++z;
                  }
};
int main()
{
      space s(3,1,4);
      s.show();
      cout<<"++s"<<endl;
      ++s;
      s.show();
      getch();
}

3.    Overloading ++ operator on postfix
//Overloading ++ operator in post fix
#include<iostream>
#include<conio.h>
using namespace std;
class space{
       int x,y,z;
       public:
                      space(int a , int b, int c)
                      {
                                    x=a;y=b;z=c;
                      }
                      void show()
                      {
                                    cout<<"x= "<<x<<" y= "<<y<<" z= "<<z<<endl;
                      }
                      void operator++(int)
                      {
                                    x=x++;
                                    y=y++;
                                    z=z++;
                      }
};
int main()
{
       space s(3,1,4);
       s.show();
       cout<<"s++"<<endl;
       s++;
       s.show();
       getch();
}

4.    Binary Operator Overloading
/*Binary operator overlaoding */
#include<iostream>
#include<conio.h>
using namespace std;
class dist{
       int meter, centimeter;
       public:
                      dist(int m=0, int cm=0)
                      {
                                    meter=m;
                                    centimeter=cm;
                      }
                      void showdist()
                      {
                                    cout<<meter<<" Meter "<<centimeter<<" Centimeter ";
                      }
                      dist operator+(dist dd1)
                      {
                                    dist temp;
                                    temp.meter=meter+dd1.meter;
                                    temp.centimeter=centimeter+dd1.centimeter;
                                    if(temp.centimeter>=100)
                                    {
                                                  temp.meter++;
                                                  temp.centimeter=temp.centimeter-100;
                                    }
                                    return temp;
                                   
                      }
};
int main()
{
       dist d1(2,40),d2(1,80),d3;
       d3=d1+d2;                      //Overloading Binary Operator
       d1.showdist();
       cout<<" + ";
       d2.showdist();
       cout<<"=";
       d3.showdist();
      
}


5.    WAP to add two complex numbers using binary operator overloading
/* binary operator overloading
WAP to add two complex numbers
*/
#include<iostream>
#include<conio.h>
using namespace std;
class complex{
       float real,imag;
       public:
       complex()
       {
                      real=0.0;
                      imag=0.0;
       }
       complex(float r,float i)
       {
                      real=r;
                      imag=i;
       }
       void show()
       {
                      cout<<"("<<real<<"+ i"<<imag<<")";
       }
       complex operator+(complex c)
       {
                      complex temp;
                      temp.real=real+c.real;
                      temp.imag=imag+c.imag;
                      return temp;
       }
      
};

int main()
{
       complex c1(2.3,4.3),c2(1.2,7.8),c3;
       c3=c1+c2;
       c1.show();
       cout<<" + ";
       c2.show();
       cout<<" = ";
       c3.show();
       getch();
}

6.     Overloading Relational Operator < to compare the distances described in object
#include<iostream>
#include<conio.h>
using namespace std;
class Distance{
              int feet,inch;
              public:
                             Distance(int f=0, int i=0)
                             {
                                           feet=f;
                                           inch=i;
                             }
                             void show()
                             {
                                           cout<<feet<<" ft"<<inch<<" inch";
                             }
                             bool operator< (Distance d)
                             {
                                           float f1,f2;
                                           f1=feet+inch/12;
                                           f2=d.feet+d.inch/12;
                                           return(f1<f2);
                             }
};
int main()
{
              Distance d1(2,4),d2(4,2);
              if(d1<d2)
              {
              d1.show();cout<<" is less than ";d2.show();
              }
              else
              {
              d2.show();cout<<" is less than ";d1.show();
}
              getch();
}

Comments