#ifndef TEMP_H
#define TEMP_H
#include <iostream>
using namespace std;
class Temp
{
private :
float heigh,low;
friend istream& operator >>(istream& in,Temp& p);
friend ostream& operator <<(ostream& out,Temp& p);
public:
Temp(float hi=0,float lw=0)
{
heigh=hi,
low=lw;
}
void UpDateTemp(float temp);
float GetHeight() const;
float GetLow() const;
};
void Temp::UpDateTemp(float tmp)
{
if(tmp>heigh)heigh=tmp;
if(tmp<low)low=tmp;
}
float Temp::GetHeight()const
{
return heigh;
}
float Temp::GetLow()const
{
return low;
}
istream& operator>>(istream& in,Temp& p)
{
cout<<"Enter height and low\n";
in>>p.heigh>>p.low;
return in;
}
ostream& operator<<(ostream& out,Temp& p)
{
out<<"heigh is "<<p.heigh<<" low is "<<p.low<<endl;
return out;
}
#endif
#endif
//测试:
#include <cstdlib>
#include <iostream>
#include "Temp.h"
using namespace std;
int main(int argc, char *argv[])
{
Temp tmp;
cin>>tmp;
cout<<tmp;
system("PAUSE");
return EXIT_SUCCESS;
}
没有明白为什么《和》运算符,这样写会报错
ostream& Temp::operator<<(ostream& out,Temp& p)
{
out<<"heigh is"<<p.heigh<<" low is"<<p.low<<endl;
return out;
}