Advertisement
steverobinson

Steve Robinson

Aug 18th, 2010
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. //++ and -- Operator overloading
  2. #include <iostream.h>
  3. #include <process.h>
  4.  
  5. class clock
  6. {
  7.     int hours, min;
  8.     public:
  9.         clock(int th, int tm)
  10.         {
  11.             hours=th;
  12.             min=tm;
  13.         }
  14.         void display()
  15.         {
  16.             cout<<"\nThe time is "<<hours<<":"<<min;
  17.         }
  18.         void operator ++() //prefix ovrldng
  19.         {
  20.             hours++;
  21.         }
  22.         void operator ++(int temp) //postfix
  23.         {
  24.             min++;
  25.         }
  26.         void operator --() //prefix
  27.         {
  28.             hours--;
  29.         }
  30.         void operator --(int temp)//postfix
  31.         {
  32.             min--;
  33.         }
  34. };
  35.  
  36. int main()
  37. {
  38.     clock c(12,0);
  39.     int ch;
  40.     menu:
  41.     c.display();
  42.     cout<<"\nMenu:\n1. Advance time by 1 hour\n2. Advance time by 1 minute\n3. Reduce time by 1 hour\n4. Reduce time by 1 minute\n5. Exit\nEnter choice...";
  43.     cin>>ch;
  44.     switch(ch)
  45.     {
  46.         case 1:
  47.             ++c;
  48.             break;
  49.         case 2:
  50.             c++;
  51.             break;
  52.         case 3:
  53.             --c;
  54.             break;
  55.         case 4:
  56.             c++;
  57.             break;
  58.         case 5:
  59.             exit(0);
  60.         default:
  61.             cout<<"\nInvalid Choice!";
  62.             goto menu;
  63.     }
  64.     goto menu;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement