Comments

Wednesday, November 21, 2012

OOP Add two time day month year initialize internally show in 3rd object

Posted by at 9:31 PM Read our previous post
Object 
Create a class called time that has separate int member data for hours, minutes, and seconds. One constructor should initialize this data to 0, and another should initialize it to fixed values. Another member function should display it, in 11:59:59 format. The final member function should add two objects of type time passed as arguments. A main() program should create two initialized time objects (should they be const?) and one that isn’t initialized. Then it should add the two initialized values together, leaving the result in the third time variable. Finally it should display the value of this third variable. Make appropriate member functions const.
Source Code
#include <iostream>
using namespace std;
class time
{ private:
                int hours,minutes,seconds;
public:
                time(){
                                hours=0;
                                minutes=0;
                                seconds=0;
                }
                time(int a,int b,int c)
                {              while(c>=60)
                                {              c-=60;
                                                minutes+=1;
                                }
                                minutes=minutes+1;
                                seconds=c;
                                while(b>=60)
                                {              b-=60;
                                                hours+=1;
                                }
                                hours=hours+1;
                                minutes=b;
                                hours=a;
                }
                void add(time t1,time t2)
                {           seconds=t1.seconds+t2.seconds;
                                while(seconds>=60)
                                {            seconds-=60;
                                                minutes+=1;     }
                                minutes=t1.minutes+t2.minutes;
                                minutes=minutes+1;
                                while(minutes>=60)
                                {            minutes-=60;
                                                hours+=1;        }
                                hours=t1.hours+t2.hours;
                                hours=hours+1;
                }
                void showtime()
                {    cout<<"time 1 is 12:28:32 and time 2 is 7:44:55 ";
                     cout<<"\nthe sum of both times is"<<hours<<":"<<minutes<<":"<<seconds<<".\n";
                }};
void main()
{
                time t1(12,28,32);
                time t2(7,44,55);
                time t3;
                t3.add(t1,t2);
                t3.showtime();
}
 Output
 

No comments:

Post a Comment

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

Popular Posts

Labels

© Codepirate is powered by Blogger - Template designed by Stramaxon - Best SEO Template