首页 > 其他 > 详细

虚函数与多态性

时间:2015-05-09 23:19:17      阅读:312      评论:0      收藏:0      [点我收藏+]
#include<iostream.h>
class shape 
{public:
  int a;
  shape(int t)
  {
      a=t;
  }
   virtual void area()
   {   
      cout<<"shape\n";
  }
};
class circle: public shape 
{
 public:
  void  area();
  circle(int x):shape(x){}
};
void circle::area()
{
    cout<<"\ncircle\n";
}

void f(shape &h)
{h.area();
}
void main()
{
 shape objs(1);
 f(objs);
 cout<<objs.a;
 circle objc(2);
 f(objc);
 cout<<objc.a<<endl;
}
技术分享

 

虚函数与多态性

原文:http://www.cnblogs.com/leijiangtao/p/4491358.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!