67 279
Assignments Done
99,1%
Successfully Done
In November 2018

Answer to Question #48266 in C++ for Payal Nayak

Question #48266
write a program to over load plus operator and minus operator for time class.
Expert's answer
#include <stdlib.h>
#include <iostream>  
#include <stdio.h>
#include <cstdlib> 
using namespace std;
class Time
{
private:

int hr;
int min;
int sec;
public:
Time(void) {};
   void EnterData();
   void Display(void);
   void operator +(Time t);
   void operator -(Time t);
};
int main()
{
Time t1,t2, t3;

t1.EnterData(); t3 = t1;
t2.EnterData();  system("cls");
cout<<"First class Time : "<<endl;
t1.Display();
cout<<"Second class Time : "<<endl;
t2.Display();
t1 + t2;
cout<<"Sum of this clases : "<<endl;
t1.Display();
cout<<"Minus of rhis clases( second - first): "<<endl;
t2 - t3;
t2.Display();
system("pause");
return 0;
}
void Time::EnterData()
{
do
{
cout<<"Enter hours"<<endl;
cin>>hr;
} while(hr < 0);
do
{
cout<<"Enter min"<<endl;
cin>>min;
} while(min<0 || min > 59); 
do
{
cout<<"Enter sec"<<endl;
cin>>sec;
} while(sec<0 || sec > 59);
}
void Time::Display()
{
cout<<" hr  = "<<hr<<endl;
cout<<" min = "<<min<<endl;
cout<<" sec = "<<sec<<endl;
}
 void Time::operator +(Time t)
 {
hr  += t.hr;
min += t.min;
sec += t.sec;
while (sec > 59)
{
sec -= 60;
    ++min;
}
 while (min > 59)
{
min -= 60;
    ++hr;
}
 }
  void Time::operator -(Time t)
 {
hr  -= t.hr;
min -= t.min;
sec -= t.sec;
while (sec > 59)
{
sec -= 60;
    ++min;
}
 while (min > 59)
{
min -= 60;
    ++hr;
}
 if (min < 0 && hr > 0) {--hr; min += 60;}
 if (sec < 0 && min > 0) { --min; sec += 60;}
 
 }


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions