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();  
 }  


6 comments:

  1. This information is impressive; I am inspired by your post writing style & how continuously you describe this topic.... Java Training in Chennai | Python Training in Chennai

    ReplyDelete
  2. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.
    rpa training in chennai
    rpa training in bangalore
    rpa course in bangalore
    best rpa training in bangalore
    rpa online training

    ReplyDelete
  3. Thank you for an additional great post. Exactly where else could anybody get that kind of facts in this kind of a ideal way of writing? I have a presentation next week, and I’m around the appear for this kind of data.
    python training Course in chennai
    python training in Bangalore
    Python training institute in bangalore

    ReplyDelete
  4. Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up. 

    Best Devops training in sholinganallur
    Devops training in velachery
    Devops training in annanagar
    Devops training in tambaram

    ReplyDelete

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...