Created
July 27, 2016 15:52
-
-
Save ravikiran0606/6e0fe49b32ec9f0eed5b9dd8de550c4f to your computer and use it in GitHub Desktop.
C++ program to create a time class and perform the required operations :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
// Creating a class time... | |
class time{ | |
int hour,minute; | |
public: | |
// Member functions of class time... | |
void initialize(int hr,int mi); | |
time sum(time t1,time t2); | |
void update(int val); | |
void display(); | |
}; | |
// Function definitions of the defined functions... | |
void time::initialize(int hr,int mi){ | |
hour=hr; | |
minute=mi; | |
} | |
void time::update(int val){ | |
minute+=val; | |
hour+=(minute/60); | |
minute%=60; | |
hour%=24; | |
} | |
time time::sum(time t1,time t2){ | |
time t3; | |
t3.minute=t1.minute+t2.minute; | |
t3.hour=t1.hour+t2.hour; | |
t3.hour+=(t3.minute/60); | |
t3.minute%=60; | |
t3.hour%=24; | |
return t3; | |
} | |
void time::display(){ | |
cout<<hour<<" hours and "<<minute<<" minutes"; | |
} | |
int main() | |
{ | |
int choice; | |
int hr,mi; | |
time t1,t2,t3; | |
int val; | |
cout<<"Choice : 1) Update the existing time by a certain value..\n 2) Find the sum of given two times..\n 3) Exit :P"; | |
while(1){ | |
cout<<"\nEnter your choice.."; | |
cin>>choice; | |
if(choice==1){ | |
cout<<"\nEnter the time to be updated in hours and minutes...( hour followed by minute ) "; | |
cin>>hr>>mi; | |
t1.initialize(hr,mi); | |
cout<<"\nEnter the increment value.."; | |
cin>>val; | |
t1.update(val); | |
cout<<"\nThe new time is..."; | |
t1.display(); | |
cout<<endl; | |
} | |
else if(choice==2){ | |
cout<<"\nEnter the first and second time one by one.. ( hour followed by minute )"; | |
cin>>hr>>mi; | |
t1.initialize(hr,mi); | |
cin>>hr>>mi; | |
t2.initialize(hr,mi); | |
t3=t3.sum(t1,t2); | |
cout<<"\nThe sum of the times is.. "; | |
t3.display(); | |
cout<<endl; | |
} | |
else{ | |
break; | |
} | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment