C++ primer plus 第六版 第十一章 复习题

第十一章 复习题

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.

只能通过成员函数进行重载:

  1. = 赋值运算符
  2. () 函数调用运算符
  3. [] 下标运算符
  4. -> 通过指针访问类成员的运算符

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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值