第11章 使用类
11.1 运算符重载
C++允许将运算符重载扩展到用户定义的类型中。在C++中,可以定义一个表示数组的类,并重载+运算符,就有如下语句:
evening=sam+janet;
要重载运算符,需要使用被称为运算符函数的特殊函数形式。运算符的格式如下:
operatorop(argument - list)
例,operator+( )重载+运算符,operator*( )重载*运算符。
代码示例:
头文件:
#ifndef MYTIME1_H_
#define MYTIME1_H_
class Time
{
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;
void show()const;
};
#endif
mytime1.cpp
#include<iostream>
#include"mytime1.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;
}
void Time::show()const
{
std::cout<<hours<<"hours, "<<minutes<<"minutes";
}
usetime1.cpp
#include<iostream>
#include"mytime1.h"
int main()
{
using std::cout;
using std::endl;
Time planning;
Time coding(5,55);
Time fixing(5,55);
Time total;
cout<<"coding time=";
coding.show();
cout<<endl;
cout<<"fixing time=";
fixing.show();
cout<<endl;
total=coding+fixing;
cout<<"total time=";
total.show();
cout<<endl;
Time morefixing(3,28);
total=morefixing.operator+(total);
cout<<"new total time=";
total.show();
cout<<endl;
return 0;
}
总之,operator+( )函数的名称使得可以使用函数表示法或运算符表示法来调用它。
11.2.2 重载限制
1)重载后的运算符必须至少有一个操作数是用户定义的类型。
2)使用运算符时不能违反运算符原来的句法规则
3)不能创建新运算符
4)不能重载下面的运算符:
sizeof:sizeof运算符
.:成员运算符
.*:成员指针运算符
:::作用域解析运算符
?::条件运算符
typeid:一个RTTI运算符
…
5)大多数其他的运算符都可以通过成员或非成员函数进行重载,但下面的运算符只能通过成员函数进行重载。
=:赋值运算符;
():函数调用运算符
[ ]:下标运算符
->:通过指针访问类成员的运算符
11.3 友元
C++控制对类对象私有部分的访问。通常,共有类方法提供唯一的访问途径。C++中提供了另外一种 形式的访问权限:友元。友元有三种:
友元函数;
友元类
友元成员函数
11.3.1 创建友元:
创建友元函数的第一步是将其原型放在类声明中,并在原型声明前加上关键字friend:
friend Time operator*(double m,const Time &t); //goes in class declaration
该原型意味着以下两点:
虽然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;
}
总之,类的友元函数是非成员函数,其访问权限与成员函数相同。
11.3.2 常用的友元:重载<<运算符
1)<<的第一种重载版本
要使Time类知道使用cout,必须使用友元函数。
通过使用友元函数,可以像下面这样重载运算符:
void operator<<(ostream & os,const Time &t)
{
os<<t.hours<<"hours. "<<t.minutes<<"minutes";
}
这样就可以使用下面的语句:
cout<<trip;
2) <<的第二种重载版本
可以对友元函数采用相同的方法。只要修改operator<<( )函数,让它返回ostream对象的引用即可:
ostream & operator<<(ostream & os,const Time & t)
{
os<<t.hours<<"hours, "<<t.minutes<<"minutes";
return os;
}
注意:返回类型是ostream&。这意味着该函数返回ostream对象的引用。
此时,下面的语句可以正常使用:
cout<<“Trip time: “<<trip<<”(TUSEDAY)\n”; //can do
提示:一般来说,要重载<<运算符来显示c_name的对象,可使用一个友元函数,其定义如下:
ostream & operator<<(ostream & os,const c_name & obj)
{
os<<...;
return os;
}
范例code:
// mytime3.h -- Time class with friends
#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; } // inline definition
friend std::ostream & operator<<(std::ostream & os, const Time & t);
};
#endif
// mytime3.cpp -- implementing Time methods
#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;
}
//usetime3.cpp -- using the fourth draft of the Time class
// compile usetime3.cpp and mytime3.cpp together
#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; // member operator*()
cout << "Aida * 1.17: " << temp << endl;
cout << "10.0 * Tosca: " << 10.0 * tosca << endl;
// std::cin.get();
return 0;
}