FY-4-b Overload the operator for adding the timings of two clocks in C++ in easy way

Overload the operator for adding the timings of two clocks in C++

Overload the operator + for adding the timings of two clocks, And also pass objects
as an argument.

Click here to get other OOP Practical

FYIT practical 4-b Unary Operator Overloading

#include<iostream>
using namespace std;
class Time
{
int hrs,min,sec;
public:
void get()
{
cout<<"Enter time(h:m:s) : ";
cin>>hrs>>min>>sec;
}
void disp()
{
cout<<endl<<hrs<<":"<<min<<":"<<sec;
}
Time operator +(time t2)
{
Time t3;
t3.sec=sec+t2.sec;
t3.min=min+t2.min+(t3.sec/60);
t3.sec=t3.sec%60;
t3.hrs=hrs+t2.hrs+(t3.min/60);
t3.min=t3.min%60;
return t3;
}
};
int main()
{
Time t1,t2,t3;
t1.get();
t2.get();
t1.disp();
t2.disp();
t3=t1+t2;
t3.disp();
return 0;
}

Output

Overload the operator itvoyagers

Check out more FYIT OOP Practical Program and other logic building programs in C++

We are aiming to explain all concepts of C++ and OOP in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment