纯虚函数与抽象类

#include<iostream.h>
class shape 
{public:
  int a;
  shape(int t)
  {
      a=t;
  }
  virtual void area()=0;
};
class circle: public shape 
{
 public:
  void  area();
  circle(int x):shape(x){}
};
class square :public shape
{
 public:
 void area();
   square(int x):shape(x){}
};

void circle::area()
{
    cout<<"circle
";
}

void square::area()
{
  cout<<"
square
";
}

void f(shape &h)
{h.area();
}
void main()
{
 circle objc(2);
 f(objc);
 cout<<objc.a;
 square objs(3);
 f(objs);
 cout<<objs.a<<endl;
}

//有纯虚函数的类就是抽象类
//虚函数可以实现多态,当然纯虚函数也可以!
//为什么要引入纯虚函数就是因为有些基类无需实现,只需在其子类实现即可,所以就引出纯虚函数与抽象类!

 
原文地址:https://www.cnblogs.com/leijiangtao/p/4491333.html