topic -- reference variable
In c++
This is a program that tells the user what the time is using opps and classes(time class). It runs as I expect it , when I do not pass the time(t) as a reference it throws a garbage value(or whatever the problem is if you want to be more specific) when I pass it as reference variable it works perfectly. I am struggling to understand that why we need to pass reference of time class. Any explanation would be appreciated.
source code---
#include <iostream>
using namespace std;
class time{
int hh=0,mm=0,ss=0;
public:
friend void operator>>(istream &in,time &t);
friend void operator<<(ostream &out,time &t);
friend void operator==(time,time);
};
void operator>>(istream &in,time &t){
cout<<"enter hours:";in>>t.hh;cout<<endl;
cout<<"enter minutes:";in>>t.mm;cout<<endl;
cout<<"enter seconds:";in>>t.ss;cout<<endl;
}
void operator<<(ostream &out,time &t){
out<<"hours :"<<t.hh;out<<endl;
out<<"minutes :"<<t.mm;out<<endl;
out<<"seconds :"<<t.ss;out<<endl;
}
void operator==(time t1,time t2){
if (t1.hh == t2.hh && t1.mm == t2.mm && t1.ss == t2.ss)
{
cout<<"same time";
}
else
cout<<"different time";
}
int main(){
time t1,t2;
cin>>t1;
cout<<t1;
cin>>t2;
cout<<t2;
t1 == t2;
return 0;
}
I am struggling to understand that why we need to pass reference of time class. Any explanation would be appreciated.