C++和C#及JAVA的继承性差异


问题是这样的:下面的程序是否有错?如果没错的话,程序的输出是什么?

#include <iostream>

class bar

{

public: vi 
rtual void f() { std::cout << "I am in bar." << std::endl; }

};

class foo : public bar

{

private: void f() { std::cout << "I am in foo." << std::endl; }

};

class wa : public foo

{

public: void f() { std::cout << "I am in wa." << std::endl; }

};

void main()

{

   bar* me = new foo;

   me->f();

   delete me;

   me = new wa;

   me->f();

   delete me;

   return;

}

答案是:程序没有错,输出如下:

I am in foo.

I am in wa.

这是一道很有意思的面试题。一方面它测试了C++中继承性和多态性;另一方面,它也触及了C++和C#或JAVA有关继承方面的一些不同之处。

  • 1
  • 2
  • 3
  • 下一页

相关内容