WAP to add two time[hr:min:sec] using class and obj...concept(passing object as an argument).
#include <iostream>
using namespace std;
class Time
{
int hr;
int min;
int sec;
public:
Time()
{
this->hr = 0;
this->min = 0;
this->sec = 0;
}
Time(int hr, int min, int sec)
{
this->hr = hr;
this->min = min;
this->sec = sec;
}
Time add(Time otherTime)
{
Time result;
result.sec = this->sec + otherTime.sec;
result.min = this->min + otherTime.min;
result.hr = this->hr + otherTime.hr;
if(result.sec >= 60){
result.min ++;
result.sec = result.sec % 60;
}
if(result.min >= 60){
result.hr ++;
result.min = result.min % 60;
}
return result;
}
void display(){
cout<< this->hr << ":" << this->min << ":" << this->sec;
}
};
int main()
{
int hr,min,sec;
cout << "Enter the first time: " << endl;
cin>> hr >> min >> sec;
Time t1(hr, min, sec);// t2,
cout << "Enter the second time: " << endl;
cin>> hr >> min >> sec;
Time t2(hr, min, sec);
Time result=t1.add(t2);
cout << "Sum: "<< endl;
result.display();
cout << endl<< endl;
system("pause");
return 0;
}
Comments
Leave a comment