使用类
(2) 友元
C++控制对类对象私有部分的访问。 通常,公有类方法提供唯一的访问途径,但是有时候这种限制太严格,以至于不适合特定的编程问题。 在这种情况下,C++提供了另外一种形式的访问权限:友元。
通过让函数成为类的友元,可以赋予该函数与类的成员函数相同的访问权限。
1. 创建友元
创建友元函数的第一步时将其原型放在类声明中,并在原型声明前加上关键字friend:
friendTimeoperator*(doublem, constTime & t);
该原型意味着下面两点:
① 虽然operator*()函数是在类声明中声明的,但它不是成员函数,因此不能使用成员运算符来调用;
② 虽然operator*()函数不是成员函数,但它与成员函数的访问权限相同。
第二步是编写函数定义。 因为它不是成员函数,所以不要使用Time::限定符。 另外,不要在定义中使用关键字friend,定义应该如下:
Time operator*(double m, const Time & t)
{
Time result;
long totalminutes = t.hours*mult * 60 + t.minutes*mult;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
有了上述声明和定义后,下面的语句:
A=2.75*B;
将转换为如下语句,从而调用刚才定义的非成员友元函数:
A=operator*(2,75,B);
总之,类的友元函数是非成员函数,其访问权限与成员函数相同。
1-1. mytime3.h
类定义中包括operator*()和operator<<()这两个友元函数。它将第一个友元函数作为内联函数,因为其代码很短。
//mytime3.h
#ifndef MYTIME3_H_
#define MYTIME3_H_
#include<iostream>
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-(const Time & t) const;
Time operator*(double n) const;
friend Time operator*(double m, const Time & t)
{
return t * m;
}//内联定义
friend std::ostream & operator<<(std::ostream & os, const Time & t);
};
#endif // !MYTIME3_H_
1-2. mytime3.cpp
//mytime3.cpp -- 实现Time方法
#include<iostream>
#include"mytime3.h"
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-(const Time & t) const
{
Time diff;
int tot1, tot2;
tot1 = t.minutes + 60 * t.hours;
tot2 = minutes + 60 * hours;
diff.minutes = (tot2 - tot1) % 60;
diff.hours = (tot2 - tot1) / 60;
return diff;
}
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;
}
std::ostream&operator<<(std::ostream & os, const Time & t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}
1-3. usetime.cpp
① <<的第一种重载版本
通过使用友元函数,可以像下面这样重载运算符:
void operator<<(ostream &os, const Time &t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
}
这样可以使用下面的语句:
cout << trip;
② <<的第二种重载版本
cout<< "Trip time: " << trip << "(Tuesday)\n"; //无法实现
只要修改operator<<()函数,让它返回ostream对象的引用即可:
ostream & operator<<(ostream &os, const Time &t)
{
os << t.hours << " hours, "<< t.minutes << " minutes";
return os;
}
//usetime3.cpp
#include<iostream>
#include"mytime3.h"
int main()
{
using std::cout;
using std::endl;
Time aida(3, 35);
Time tosca(2, 48);
Time temp;
cout << "Aida and Tosca:\n";
cout << aida << "; " << tosca << endl;
temp = aida + tosca; //operator+()
cout << "Aida + Tosca: " << temp << endl;
temp = aida * 1.17; //成员operator*()
cout << "Aida * 1.17: " << temp << endl;
cout << "10.0 * Tosca: " << 10.0*tosca << endl;
std::cin.get();
return 0;
}