第十一章 复习题
1.
Stonewt Stonewt::operator*(double n) const
{
Stonewt result;
double total = stn * Lbs_per_stn * n +lbs * n;
result.stn = total / Lbs_per_stn;
result.lbs = total % Lbs_per_stn;
return result;
}
2.
成员函数是类定义的一部分,可以隐式访问成员,无需使用成员运算符。
友元函数不是类的组成部分,必须将成员运算符作为参数传递的对象,才能访问类成员。
3.
访问私有成员时,只能友元;访问公有成员时,可以非友元。
4.
friend Stonewt operator*(double n, const Stonewt & s)
{
return s * n;
}
5.
不能重载的运算符如下:
1. sizeof
2. .
3. .*
4. ::
5. ?:
6. typeid
7. const_cast
8. dynamic_cast
9. reinterpret_cast
10. static_cast
6.
只能通过成员函数进行重载:
- = 赋值运算符
- () 函数调用运算符
- [] 下标运算符
- -> 通过指针访问类成员的运算符
7.
//vector.h
operator double () const;
//vector.cpp
operator double () const
{
mode = form;
if(form == RECT)
{
return double(sqrt(x * x + y * y));
}
if(form == pOL)
{
return double(mag);
}
}