- #1
shivajikobardan
- 674
- 54
- Homework Statement
- Why do ostream &os? What does it do? Why not just do "ostream os"? What happens in memory when we do this call by reference thing?
- Relevant Equations
- none
Full code goes here: The goal of the program is to overload the extraction operator for cout.
Please guide how to understand this. Tell me any prerequisite that I'm missing, that'd be immensely helpful than giving me hand holded solution.
Code:
#include<iostream>
using namespace std;class Time
{
private:
int hour;
int min;
public:
Time()
{
hour=min=0;
}
Time(int h,int m)
{
hour=h;
min=m;
}
Time operator+(Time t)
{
Time t1;
int m=min+t.min;
t1.hour=hour+t.hour+m/60;
t1.min=m%60;
return t1;
}
Time operator+(int x)
{
Time t;
t.hour=hour+x;
t.min=min;
return t;
}
friend void operator<<(ostream &os,Time t );
};
void operator<<(ostream &os,Time t)
{
cout<<"Hour : "<<t.hour<<"min:"<<t.min<<endl;
}int main()
{
Time t1;
Time t2(8,40);
Time t3(5,30);
Time t4(2,30);
t1=t2+t3+t4;
cout<<t1;
return 0;
}