我对基类和子类函数继承感到非常困惑。我有这些课程:
#include <point.h>
class Polygon{
public:
Polygon();
virtual ~Polygon();
void addPoint(Point *p);
std::string getType();
Point* getPoint(int index);
int getNumOfPoints();
int getColor();
virtual int area()=0;
private:
std::vector<Point*> _points;
int color;
std::string type = "Polygon";
};
class Rectangle : public Polygon{
public:
Rectangle();
virtual ~Rectangle();
virtual int area();
private:
std::vector<Point*> _points;
int color;
std::string type = "Rectangle";
};
现在,我主要这样做:
Rectangle rect();
rect.getType();
这给了我“多边形”,而我想要“矩形”我很确定我对继承感到困惑。所以,根据我的理解,基类函数是被继承的,但是为什么当我运行它与对象基类的成员而不是实际对象(矩形)相关的函数时呢?
如果有人愿意帮忙,我会很高兴!非常感激