×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

第一题是3, 但你解释得不够全面. 这主要是计算次序不同造成的. 相同次序相加, 加多少次都是同一个值.

for example:
Let a = { -b, b,b}, where b<Max and 2b>Max.
then the first sum will be b, but the caculation of the second sum will end up with an overflow.

As for the second problem, I think the error lies in the illegal access to pNumber in Bolt::init(). That is because pNumber is a private member, so it is only accessable to the methods in the same class with it. In fact, even if there is no violation to access right here, say pNumber is a public number, this line also should be ruled out, since father's no-argument constructor will be called automatically in the beginning of the execution of son's constructor, if no explicit call to father's constructor is put on the first line in son's constructor.

Buddy, could you kindly share your job hunting experience with me? I am falling flatly on that, help!!!
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 两道C/C++面试题,麻烦大家帮我看看。谢谢。第一题我想答案是3,因为我觉得有可能因为浮点数计算的误差,会导致有时候计算结果不同。
    本文发表在 rolia.net 枫下论坛Here is a program in ANSI Standard C:
    double sum, a[20];
    int i;
    /*
    * Some code to initialize array a with legitimate
    * floating point values whose sum is less than "maximum double".
    */
    sum = 0.0;
    for (i = 0; i < 20; i++)
    sum += a[i];
    printf("%e\n", sum);
    sum = 0.0;
    i = 20;
    while (i--)
    sum += a[i];
    printf("%e\n", sum);

    Question:
    Will the 2 "printed" values:
    1) always be the same? if so, explain why.
    2) always be different? if so, explain why.
    3) sometimes be the same and sometimes different? if so, give an
    example.
    Please be as complete as possible.


    -------------------------------


    Here is some code written in C++. Can you find the error, and how would you
    fix it without changing the interface?

    class Widget
    {
    public:
    Widget() { Init(); }
    virtual ~Widget() { Destroy(); }
    virtual void Init();
    virtual void Destroy();
    long GetNum() { return *pNumber; }
    void SetNum(long num) { *pNumber = num; }
    private:
    long *pNumber;
    };

    class Bolt : public Widget;
    {
    public:
    Bolt() { Init(); }
    virtual ~Bolt() { Destroy(); }
    virtual void Init();
    virtual void Destroy();
    long GetNum2() { return *pNumber2; }
    void SetNum2(long num) { *pNumber2 = num; }
    private:
    long *pNumber2;
    };

    void Widget::Init()
    {
    pNumber = new long;
    }

    void Widget::Destroy()
    {
    delete pNumber;
    }

    Bolt::Init()
    {
    pNumber = new long;
    pNumber2 = new long;
    }

    Bolt::Destroy()
    {
    delete pNumber2;
    }更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • up!
    • 各位大侠给点建议好吗?谢谢。
    • 第一题是3, 但你解释得不够全面. 这主要是计算次序不同造成的. 相同次序相加, 加多少次都是同一个值.
      for example:
      Let a = { -b, b,b}, where b<Max and 2b>Max.
      then the first sum will be b, but the caculation of the second sum will end up with an overflow.

      As for the second problem, I think the error lies in the illegal access to pNumber in Bolt::init(). That is because pNumber is a private member, so it is only accessable to the methods in the same class with it. In fact, even if there is no violation to access right here, say pNumber is a public number, this line also should be ruled out, since father's no-argument constructor will be called automatically in the beginning of the execution of son's constructor, if no explicit call to father's constructor is put on the first line in son's constructor.

      Buddy, could you kindly share your job hunting experience with me? I am falling flatly on that, help!!!
      • 哪有经验可言啊,我还在家里坐着呢。:(
        • at least you get an interview, right? I never got a phone call !
          • 我也没有面试啊,他们说人太多了,没法安排面试,所以先发email,给两道题做做。
            • pat pat