Monday, April 30, 2012

Time Class Program in C++ using operator overloading to add two different Time values

The following program is an implementation of operator overloading in C++ to add two time values in the format HH:MM:SS to the resulting time along with rounding off when 24 hours is reached.
A Time class is created and Operator + is overloaded to add the two Time Class Objects.


The complete source code in C++ to Implement Time class is given below
 #include<iostream.h>  


 #include<conio.h>  
 class time  
 {  
 public:  
 time()  
 {  
 hr=0;  
 min=0;  
 sec=0;  
 }  
 int hr,min,sec;  
 void read()  
 {  
 cout<<"Hours=";  
 cin>>hr;  
 cout<<"\nMinutes=";  
 cin>>min;  
 cout<<"\nSeconds=";  
 cin>>sec;  
 }  
 time operator +(time t2)  
 {  
 time t3;  
 t3.sec=sec+t2.sec;  
 if(t3.sec>60)  
 {  
 t2.min+=1;  
 t3.sec-=60;  
 }  
 t3.min=min+t2.min;  
 if(t3.min>60)  
 {  
 t2.hr+=1;  
 t3.min-=60;  
 }  
 t3.hr=hr+t2.hr;  
 return t3;  
 }  
 void display()  
 {  
 if(hr>=24)  
 { hr=hr%24;  
 }  
 if(hr<10)  
 { cout<<"0"<<hr;  
 }  
 else  
      cout<<hr;  
 if(min<10)  
 { cout<<":0"<<min;  
 }  
 else  
      cout<<":"<<min;  
 if(sec<10)  
 { cout<<":0"<<sec;  
 }  
 else  
      cout<<":"<<sec;  
 }  
 };  
 void main()  
 {  
 clrscr();  
 time c1,c2,c3;  
 cout<<"\n\nEnter the First Time\n\n";  
 c1.read();  
 cout<<"\n\nEnter the Second Time\n\n";  
 c2.read();  
 c3=c1+c2;  
 cout<<"\n\nFirst Time  \t\t";  
 c1.display();  
 cout<<"\n\nSecond Time  \t\t";  
 c2.display();  
 cout<<"\n\nAfter addition,the time is ";  
 c3.display();  
 getch();  
 }  


Which is the Best Photo Watermarking Software

Photo Theft is becoming more and more common in the web with the outburst of social websites like Facebook,Google Plus and Image sharing se...