1.友元函数可以访问类的私有成员的函数
2.创建友元
class Time
{
private:
int hours;
int minutes;
public:
Time(); //构造函数
Time(int h,int m=0); //构造函数
void AddMin(int m);//分钟增加
void AddHr(int h);//小时增加
void Reset(int h=0,int m=0);
Time operator+(const Time & t) const;
Time operator*(double mult) const;
void show() const;
};
...
Time Time::operator*(double mult) const
{
Time result;
long totalminutes = hours * mult * 60 + minutes * mult;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
//以上类的声明对*号的重载可以实现
Time A;
Time B;
A=B*7.5; //正确
A=7.5*B;//错误
//接下来用友元实现7.5*B的重载
#include<iostream>
using namespace std;
class Time
{
private:
int hours;
int minutes;
public:
Time(); //构造函数
Time(int h,int m=0); //构造函数
void AddMin(int m);//分钟增加
void AddHr(int h);//小时增加
void Reset(int h=0,int m=0);
Time operator+(const Time & t) const;
Time operator*(double mult) const;
friend Time operator*(double m,const Time & t);//友元函数
void show() const;
};
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h,int m)
{
hours = h;
minutes = m;
}
void Time::AddMin(int m)
{
minutes +=m;
hours +=minutes / 60;
minutes %= 60;
}
void Time::AddHr(int h)
{
hours += h;
}
void Time::Reset(int h,int m)
{
hours =h;
minutes = m;
}
Time Time::operator+(const Time & t) const
{
Time sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes /60;
sum.minutes %= 60;
return sum;
}
Time Time::operator*(double mult) const
{
Time result;
long totalminutes = hours * mult * 60 + minutes * mult;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
Time operator*(double m,const Time & t)
{
Time result;
long totalminutes=m * t.hours*60 + m * t.minutes;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
void Time::show() const
{
cout<<hours<<"小时"<<minutes<<"分"<<endl;
}
int main()
{
Time planning;
Time am(3,30);
Time pm(4,35);
Time A;
Time B;
cout<<"planning time is ";
planning.show();
cout<<"am time is ";
am.show();
cout<<"pm time is ";
pm.show();
B = am + pm;
cout<<"一天耗时 am + pm =";
B.show();
A = 7.5*B;
cout<<"7.5天耗时 = ";
A.show();
return 0;
}
运行结果:
3.使用友元重载<<
3-1:
#include<iostream>
using namespace std;
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h,int m);
void show();
friend void operator<<(ostream & os,const Time & t);
};
Time::Time()
{
hours=minutes=0;
}
Time::Time(int h,int m)
{
hours=h;
minutes=m;
}
void Time::show()
{
cout<<hours<<"小时"<<minutes<<"分"<<endl;
}
void operator<<(ostream & os,const Time & t)
{
os<<t.hours<<"hours,"<<t.minutes<<"minutes"<<endl;
}
int main()
{
Time t1;
cout<<t1;
t1.show();
}
运行结果:
3-2:
#include<iostream>
using namespace std;
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h,int m);
void show();
// friend void operator<<(ostream & os,const Time & t);
friend ostream & operator<<(ostream & os,Time & t);
};
Time::Time()
{
hours=minutes=0;
}
Time::Time(int h,int m)
{
hours=h;
minutes=m;
}
void Time::show()
{
cout<<hours<<"小时"<<minutes<<"分"<<endl;
return os;
}
/*
void operator<<(ostream & os,const Time & t)
{
os<<t.hours<<"hours,"<<t.minutes<<"minutes"<<endl;
}*/
ostream & operator<<(ostream & os,Time & t)
{
os<<t.hours<<"时|"<<t.minutes<<"分钟"<<endl;
}
int main()
{
Time t1;
cout<<t1;
t1.show();
}
运行结果:
3-3:重载输出到一个文件夹
#include<iostream>
#include<fstream>
using namespace std;
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h,int m);
void show();
// friend void operator<<(ostream & os,const Time & t);
friend ostream & operator<<(ostream & os,Time & t);
};
Time::Time()
{
hours=minutes=0;
}
Time::Time(int h,int m)
{
hours=h;
minutes=m;
}
void Time::show()
{
cout<<hours<<"小时"<<minutes<<"分"<<endl;
}
/*
void operator<<(ostream & os,const Time & t)
{
os<<t.hours<<"hours,"<<t.minutes<<"minutes"<<endl;
}*/
ostream & operator<<(ostream & os,Time & t)
{
os<<t.hours<<"时|"<<t.minutes<<"分钟"<<endl;
return os;
}
int main()
{
Time t1;
cout<<t1;
t1.show();
ofstream fout;
fout.open("test.txt");
fout<<t1;
}
运行结果: