C++ OPERATOR OVERLOADING AND BUILD A CLOCK
/*
C++ OPERATOR OVERLOADING AND BUILD A CLOCK
WRITE A PROGRAM CLASS NAME CLOCK WITH DATA MEMBERS, HOURS, MINUTES, AND SECONDS. WRITE A PROGRAM TO ADD TWO INPUT TIME USING OPERATOR OVERLOADING.
*/
#include <iostream>
class clockk
{
int m,s,h;
public:
clockk()
{ }
void getdata()
{
std::cout<<"Enter values: "<<std::endl<<"Hour: ";
std::cin>>h;
std::cout<<"minutes: ";
std::cin>>m;
std::cout<<"seconds: ";
std::cin>>s;
}
clockk operator+ (clockk b)
{
clockk temp;
temp.h = h+b.h;
temp.m=m+b.m;
temp.s=s+b.s;
return (temp);
}
void display(void)
{
if(s>=60)
{
m++;
s-=60;
}
if(m>=60)
{
h++;
m-=60;
}
std::cout<<h<<"h "<<m<<"m "<<s<<"s "<<std::endl;
}
};
int main()
{
clockk t1, t2, t3;
std::cout<<"Enter first input: "<<std::endl;
t1.getdata();
std::cout<<"Enter second input: "<<std::endl;
t2.getdata();
t3=t1+t2;
std::cout<<"T1= "; t1.display();
std::cout<<"T2= "; t2.display();
std::cout<<"Addition Result: ";
t3.display();
}
/*
Enter first input:
Enter values:
Hour: 3
minutes: 30
seconds: 00
Enter second input:
Enter values:
Hour: 3
minutes: 30
seconds: 00
INITIALIZE
T1= 3h 30m 0s
T2= 3h 30m 0s
Addition Result: 7h 0m 0s
*/
Comments
Post a Comment