Friday, 21 May 2021

CPP-Slip26b

26b)Create a C++ class Time with data members hours, minutes, seconds. Write a C++ program using  operator overloading to perform the following:

 1.!=  To check whether two Times are equal or not.

11.>> To accept the time.

111<< To display the time. 

 #include<conio.h>

#include<stdio.h>

#include<string.h>

#include<iostream.h>

 class time

{

int hours;

int minutes;

int second;

public:

friend istream & operator >>(istream &, time &);

friend ostream & operator <<(ostream &, time &);

int operator !=(time t2);

};

istream & operator >>(istream & stream, time & t)

{

return stream >>t.hours>>t.minutes>>t.second;

}

ostream & operator <<(ostream & stream, time & t)

{

return stream <<t.hours<<":"<<t.minutes<<":"<<t.second;

}

int time::operator !=(time t2)

{

if(hours==t2.hours&&minutes==t2.minutes&&second==t2.second)

return 1;

return 0;

}

 int main()

{

time t1,t2;

clrscr();

cout<<"\n Enter time as hour,minutes,seconds :";

cin>>t1;

cout<<"\n Enter time as hour,minutes,seconds :";

cin>>t2;

cout<<"\n First Time is :";

cout<<t1;

cout<<"\n Second Time is :";

cout<<t2;

if(t1!=t2)

cout<<"\nBoth times are equal";

else

cout<<"\n Time not equal";

getch();

return 0;

}