Write a program to add two time and store result into 3rd object using pass by value

/*
  WAP to add two time and store result
  into 3rd object
*/
#include<iostream>
using namespace std;

class Time
{
int hour,min,sec;

public:
void accept()
{
cout<<"\n\n Enter hour, minute and second: ";
cin>>hour>>min>>sec;
}

void display()
{
cout<<"\n\n"<<hour<<" hour "<<min<<" min "<<sec<<" sec.";
}

void add(Time t1,Time t2)
{
hour=t1.hour+t2.hour;
min=t1.min+t2.min;
sec=t1.sec+t2.sec;

if(sec>=60)
{
min=min+sec/60;
sec=sec%60;
}

if(min>=60)
{
hour=hour+(min/60);
min=min%60;
}

display();
}
};

int main()
{
Time t1,t2,t3;

t1.accept();
t2.accept();
   
t1.display();
t2.display();

t3.add(t1,t2);
   
    return 0;
}

Previous
Next Post »