首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++//三个有关问题

2012-03-09 
c++//三个问题!#include iostreamusing namespace stdclass capacity{public:virtual void foo(){}}cl

c++//三个问题!
#include <iostream>
using namespace std;

class capacity{
public:
  virtual void foo(){}
  };
class sluggish{
public:
  void foo()
  {
  cout<<"hello"<<endl;
  }
};

int main()
{
  capacity capacity01;
  capacity01.foo();//为啥不输出?
 }
//还有一个问题,循环队列可以在栈区实现吗?
//基类& ex,可以用派生类的对象代替?

例如
  函数名
  {
  --------------
  throw OverFlow();//抛出匿名对象
  ------------------
  }

   

  class OverFlow:public exception{//异常派生类
  public:
  const char* what() const throw()
  {
  return "stack::over_flow";
  }
  };

int main()
{
  try{

  }catch(exception&ex)//基类对象的引用ex,怎么可以用派生类的对象代替?
  {

  }
}

[解决办法]
多态是通过指针和引用来实现的。你用基类对象调用基类函数没有发生任何多态现象。


C/C++ code
class capacity{public:    virtual void foo() {}};class sluggish : public capacity{public:    void foo()    {        cout << "hello" <<endl;    }};int main(){    sluggish sluggish_01;    capacity *capacity_01 = &sluggish_01;    capacity_01->foo();    return 0;} 

热点排行