Write a program adding two times and correction of time using class program in C++

 


In this code, two times are adding and using correction function try to correct time

 //C++ code to add two time   
 #include<iostream>  
 #include<iomanip>  
 using namespace std;  
 class Time   
 {  
   private:  
     int hour;int minut;int sec;  
   public:  
   Time(){};// default constratr  
   Time(int,int,int);  
   void GetTime();  
   void print();  
   void correction_time(Time );// tjis is correction function  
   void add(Time t1,Time t2) //function to add two time   
   {  
     this->hour=t1.hour+t2.hour;  
     this->minut=t1.minut+t2.minut;  
     this->sec=t1.sec+t2.sec;  
   }  
 };  
 Time :: Time(int h,int m,int s)  
 {  hour=h;  
   minut=m;  
   sec=s;  
 }  
 void Time::GetTime()  
 {  
   int h,m,s;  
   cout<<"Enter value of hour,mim,second :";  
   cin>>h>>m>>s;  
   hour=h;  
   minut=m;  
   sec=s;  
 }  
 void Time::print()  
 {  
 cout << setw(2) << setfill('0') << hour << ":"  
     << setw(2) << setfill('0') << minut << ":"  
      << setw(2) << setfill('0') << sec<<endl;}  
 void Time::correction_time(Time t)  
 {  
   if(minut>60 )  
   {  
     hour=hour+1;  
   }  
   minut=minut%60;  
   if(sec>60)  
   {  
     minut++;  
   }  
   sec=sec%60;  
 }  
 int main()  
 {  
   Time t(4,90,90),t1,t2;  
   t.correction_time(t);  
   cout<<"First Time "<<endl;  
   t.print();  
   t1.GetTime();  
   t1.print();  
   t1.correction_time(t1);  
   cout<<"Second Time "<<endl;  
   t1.print();  
   t2.add(t,t1);  
   t2.correction_time(t2);  
   cout<<"After addtion of two time "<<endl;  
   t2.print();  
   return 0;  
 }  
Output
First Time
05:31:30
Enter value of hour,mim,second :5
66
66
05:66:66
Second Time 
06:07:06
After addtion of two time 
11:38:36

0 Comments