// multiple inheritance
#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
public:
Polygon (int a, int b) : width(a), height(b) {}
virtual int area()=0;
virtual void print(){
cout << "area = " << area() << endl;
};
};
class Rectangle: public Polygon{
public:
Rectangle (int a, int b) : Polygon(a,b) {}
int area () { return width*height; }
void print(){
cout << "area = " << area() << endl;
};
};
class Square: public Rectangle{
public:
Square (int a, int b) : Rectangle(a,b) {}
int area () { return width*height/2; }
};
int main () {
Square sq (4,5);
sq.print ();
return 0;
}
在此功能中,打印调用Square(不是Rectangle)的区域()。为什么?由于Rectangle中的area()不是虚拟的,因此它应该从Rectangle调用area()。最终结果是10.据我说它应该是20。