Thursday, October 13, 2016

Overload - and > operator for class Time

Problem

Overload - and > operator for class Time, so that this will work:
if (Time2 > Time1)
Time3 = Time2 - Time1;
else
Time3 = Time1 - Time2;

Code

#include "stdafx.h"
#include<conio.h>
#include<iostream>
#include<string.h>
using namespace std;

class time
      {
      private:
            int hr;
            int min;
            int sec;

      public:
            time():hr(0),min(0),sec(0)
            {     }

            void getdata()
            {
                  cout<<"Enter Hours: ";
                  cin>>hr;
                  cout<<"Enter Minutes: ";
                  cin>>min;
                  cout<<"Enter Seconds: ";
                  cin>>sec;
            }

            bool operator > (time t1)
            {
                  if(hr>t1.hr)
                        return true;
                  else if(hr == t1.hr)
                  {
                        if(min>t1.min)
                              return true;
                        else if(min == t1.min)
                        {
                              if (sec > t1.sec)
                                    return true;
                              else if(sec == t1.sec)
                              {
                                    cout<<"\n\nTimes are EQUAL.";
                                    cout<<"\n\nPress any key to close.";
                                    getch();                           
                                    exit(0);
                              }
                              else
                                    return false;
                        }
                        else
                              return false;
                  }
                  else
                        return false;
            }

            time operator - (time t1)
            {
                  time temp;
                  temp.hr = hr - t1.hr;
                  temp.min = min - t1.min;
                  temp.sec = sec - t1.sec;

                  if(temp.min < 0)
                        temp.min = (temp.min * (-1));
                  if(temp.sec < 0)
                        temp.sec = (temp.sec * (-1));
                 
                  return temp;
            }

            void display()
            {
                  cout<<hr<<":"<<min<<":"<<sec;
            }

      };

int main()
{
      time time1,time2,time3;
     
      cout<<"Enter 1st Time: \n";
      time1.getdata();

      cout<<"\n\nEnter 2nd Time: \n";
      time2.getdata();

      if(time2>time1)
      {
            time3 = time2 - time1;
            cout<<"\n\n2nd Time is greater.";
            cout<<"\nTime after subtracting: ";time3.display();
      }
      else
      {
            time3 = time1 - time2;
            cout<<"\n\n1st Time is greater.";
            cout<<"\nTime after subtracting: ";time3.display();
      }
     
      cout<<"\n\nPress any key to close.";
      getch();
      return 0;
}

Output

Overload - and > operator for class Time

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact