Sunday, April 22, 2012

operator over loading

#include<iostream.h>
#include<conio.h>
class time
{
int hr;
   int min;
   int sec;
   public:
   time();
   time(int h, int m, int s);
   time (time &t);
   time operator +(time t);
   time operator -(time t);
   time operator *(time t);
   time operator /(time t);
   time operator ++();
   time operator --(int);
   time friend operator *(int , time t); // operators to
   time friend operator +(int , time t);// multiply int data type
   time friend operator -(int , time t);//   with time class object
   time friend operator /(int , time t);
   ///////////////////////////////////////
   time friend operator *( time t,int); // operators to
   time friend operator +( time t,int);// multiply time class object
   time friend operator -(time t,int); // with int data type.
   time friend operator /(time t, int);
   friend istream & operator >>(istream & in, time &t);
   friend ostream & operator <<(ostream & out,time &t);
   ~time()
   {
    cout<<"Time object destroyed \n";
      getch();
   }
};
time::time()
{
hr=0;min=0;sec=0;
}
time::time(int h, int m, int s)
{
hr=h;
   min=m;
   sec=s;
}
time::time(time &t)
{
hr=t.hr;
   min=t.min;
   sec=t.sec;

}

time time ::operator++()
{
hr++;
   min++;
   sec++;
return *this;
}
time time :: operator --(int )
{
hr--;
   min--;
   sec--;
return *this;
}
time time::operator +(time t)
{
time temp;
   temp.hr=hr+t.hr;
   temp.min=min+t.min;
   temp.sec=sec+t.sec;
   return temp;
}
time time::operator -(time t)
{
time temp;
   temp.hr=hr-t.hr;
   temp.min=min-t.min;
   temp.sec=sec-t.sec;
   return temp;
}
time time::operator *(time t)
{
time temp;
   temp.hr=hr*t.hr;
   temp.min=min*t.min;
   temp.sec=sec*t.sec;
   return temp;
}
time time::operator /(time t)
{
time temp;
   temp.hr=hr/t.hr;
   temp.min=min/t.min;
   temp.sec=sec/t.sec;
   return temp;
}
ostream & operator <<(ostream & out,time &t)
{
cout<<t.hr<<endl<<t.min<<endl<<t.sec<<endl;
   return out;
}
istream & operator >>(istream & in,time &t)
{
cin>>t.hr>>t.min>>t.sec;
   return in;
}
time operator +(int n, time t)
{
time temp;
   temp.hr=n+t.hr;
   temp.min=n+t.min;
   temp.sec=n+t.sec;
   return temp;
}
time operator -(int n, time t)
{
time temp;
   temp.hr=n-t.hr;
   temp.min=n-t.min;
   temp.sec=n-t.sec;
   return temp;
}
time operator *(int n, time t)
{
time temp;
   temp.hr=n*t.hr;
   temp.min=n*t.min;
   temp.sec=n*t.sec;
   return temp;
}
time operator /(int n, time t)
{
time temp;
   temp.hr=n/t.hr;
   temp.min=n/t.min;
   temp.sec=n/t.sec;
   return temp;
}
time operator +( time t,int n)
{
time temp;
   temp.hr=t.hr+n;
   temp.min=t.min+n;
   temp.sec=t.sec+n;
   return temp;
}
time operator -( time t,int n)
{
time temp;
   temp.hr=t.hr-n;
   temp.min=t.min-n;
   temp.sec=t.sec-n;
   return temp;
}
time operator *( time t,int n)
{
time temp;
   temp.hr=t.hr*n;
   temp.min=t.min*n;
   temp.sec=t.sec*n;
   return temp;
}
time operator /( time t,int n)
{
time temp;
   temp.hr=t.hr/n;
   temp.min=t.min/n;
   temp.sec=t.sec/n;
   return temp;
}
void  main()
{
clrscr();
time t1,t2,t3(5,2,3),t4(6,6,6);
   cout<<t3;
   t2=2*t3;
   cout<<t2;
   t1=t2+6;
   cout<<t1;


     getch();
}

No comments:

Post a Comment