I'm just a beginner on C++ and the object oriented stuffs. In transition from C. Please bear with my ignorance. This is in continuation with:
Can a pointer to base point to an array of derived objects?
#include <iostream>
//besides the obvious mem leak, why does this code crash?
class Shape
{
public:
virtual void draw() const = 0;
};
class Circle : public Shape
{
public:
virtual void draw() const { }
int radius;
};
class Rectangle : public Shape
{
public:
virtual void draw() const { }
int height;
int width;
};
int main()
{
Shape * shapes = new Rectangle[10];
for (int i = 0; i < 10; ++i)
shapes[i].draw();
}
The Class Shape
contains a pure virtual function and hence it becomes an abstract class. Hence, on the first place, there should be a compiler error on creating an instance for it. But I don't get any compiler error.