×

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

再来一道题目,C++的,类继承的问题

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;
}
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 问题讨论,考考你的C语言知识(1)很有趣哟
    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.
    • 再来一道题目,C++的,类继承的问题
      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;
      }
      • virtual 去掉;bolt:init()中的第一行去掉。
      • There are some small issues in this program. But I guess your point is we cannot call virtual function in constructor.
    • (3) 积累误差所致。
      • 方向是对的,但是答案不对 这是一道面试题目
        • 题目里暗示如果超过FLOAT MAX发生答案就不同。纯属钻牛角尖。
          • 问题就在这里,结果不溢出,并不意味着中间结果不溢出.
          • 也就是从左到右不溢出,而从右到左有可能溢出.
    • 3) : actually the problem is : a[0]+a[1]+..+a[19] =?? a[1]+a[2]+...a[20] ?
      • 你没有用过C语言吧
        • i was wrong, I checked it
          #include "stdio.h"

          int aa[20];
          int pad=2;
          main()
          {
          int i=20;
          int sum = 0;

          aa[0]=1;

          while (i--)
          sum +=aa[i];

          printf("sum=%d\n",sum);
          }

          result:

          debian:~/src/tmp$ vi tmp.c
          debian:~/src/tmp$ !gcc
          gcc -o tmp tmp.c

          debian:~/src/tmp$ ./tmp
          sum=1