I have a base class Shape, and a derived class Circle which inherits Shape publically:
class Circle : public Shape
I made a C++ vector of Shape pointers, and I assigned Circle pointers to them. I'd read up a lot on object slicing so expected the code to treat the Circle in the vector as a Circle, not a Shape.
Can anyone point out what's wrong with this, given the output?
int main(void) {
vector<Shape*> shapes;
Circle* ncl = new Circle(-8, -8, 0.2f);
shapes.push_back(ncl);
cout << "Expected: " << *ncl << endl;
cout << "Reality: " << *shapes[0] << endl;
}
outputs:
Expected: Circle is at: -8,-8 and has a radius of: 0.2
Reality: Shape centered at point: -8,-8
I have overridden the << operator for both classes out of scope, so I think that's not the problem, but still - here's the code context of my overrides:
inline std::ostream& operator<< (std::ostream& stream, const Shape& shape) {
std::cout << "Shape centered at point: " << shape.getX() << "," << shape.getY();
return stream;
}
inline std::ostream& operator<< (std::ostream& stream, const Circle& circle) {
std::cout << "Circle is at: " << circle.getX() << "," << circle.getY() <<
" and has a radius of: " << circle.getR();
return stream;
}
All in all - I want to be able to access my Circle variables properly while they're stored in the Shape vector (with pointers or otherwise).