纯虚函数的和抽象类

  • virtual double area() = 0; 纯虚函数和虚函数的区别在与后面是不是有 = 0,如果没有 = 0 上面的函数就是一个虚函数
  • 虚函数在运行的时候会动态绑定对象实际运行的函数,比如下面的例子中,main函数运行的时候,cout << "Shape " << i << ": " << shapes[i]->area() << endl;通过virtual关键字,选择真正的area来执行
  • 纯虚函数的出现是为了实现面向对象中的多态,比如方形和三角形都是shape,那么shape可以作为基类保留他们的共同特点,而他们又各自有自己的实现
  • 纯虚函数内是一定没有定义的
  • 成为抽象类的条件是必须有至少一个纯虚函数,只要在类里面有纯虚函数,这个类就被叫做抽象类。
  • 抽象类的每一个延申类都必须有纯虚函数的定义,否则延伸类就是一个抽象类
  • 抽象类不能有实例,但是可以有指针Shape *shapes[] =
    {
    new Square(5),
    new Triangle(8,10),
    new Square(7),
    new Triangle(3,4)
    };

#include 
using namespace std;

// Shape is a base class for Square and Triangle, and we want to be able to 
// use polymorphism to work with arrays of pointers to Square, Triangle and 
// other object instances so that we can execute member functions like 
// area() to calculate the area of shapes.  The problem is that there is NO 
// "generic shape area", the area calculation is really only defined for 
// specific types of shapes.  So instead we create a pure virtual function 
// area() in our base class shape, which makes the Shape class an abstract 
// class (having 1 or more pure virtual member function makes any class a 
// an abstrac class).  We then leave it to Square, Triangle and any other 
// derived class to override the area member function (if they do not, they 
// will also become abstract classes).  Notably, we cannot create an instance
// of an abstract class like Shape, but we CAN have pointers and references 
// of the type of an abstract class.
//
class Shape
{
public:

  // The virtual keyword will make the area() function a virtual function that 
  // allows for dynamic binding (i.e. runtime polymorphism).  The = 0 assignment
  // will make it a pure virtual function, and makes Shape an abstract class.
  virtual double area() = 0;
};

// Square inherits from Shape, and overrides the area pure virtual function. 
// Because Square overrides all of the pure virtual functions of its base 
// class, we can create Square object instances, and we would call Square 
// a concrete class.
class Square : public Shape
{
public:
  double side;
  
  Square(double side) : side(side) {}
  
  double area()
  {
    return side * side;
  }
};

// Triangle also inherits from Shape, and also overrides the area pure virtual
// function with a calculation that works for triangles.
class Triangle : public Shape
{
public:
  double base;
  double height;
  
  Triangle(double base, double height) :
    base(base), height(height) {}
  
  double area()
  {
    return 0.5 * base * height;
  }
};

int main()
{
  // We *cannot* make a Shape object instance because it is an abstract class.
  // So the below code will cause a compiler error if we uncomment it.
  //
  // Shape shape;

  // We CAN create pointers and references of the type of an abstract class,
  // allowing us to create an array of pointers to object instances of 
  // derived classes of Shape.
  Shape *shapes[] =
  {
    new Square(5),
    new Triangle(8,10),
    new Square(7),
    new Triangle(3,4)
  };
  
  // Because we have an array of pointers to Shapes, which support dynamic 
  // binding of the area() member function, we can use runtime polymorphism 
  // to loop through our array of pointers to Shapes and call the area 
  // member function for each object!  This allows us to utilize polymorphism,
  // BUT without implementing a "generic Shape" area member function that 
  // would not make any sense.
  //
  for (int i = 0; i < 4; i++)
    cout << "Shape " << i << ": " << shapes[i]->area() << endl;
   
  return 0;
}


相关链接

你可能感兴趣的:(c++,开发语言)