If I want to initialize a class Square
class Square : public Rectangle {
public Square(int length)
{
this->length = length;
this->width = length;
}
}
derived from a class Rectangle
class Rectangle {
protected int length, width;
public Rectangle(int length, int width)
{
this->length = length;
this->width = width;
}
public int getArea() { return length * width; }
}
I'll do it like
Square * s = new Square(5);
cout << s->getArea() << endl;
What are the benefits of doing it like
Rectangle * r = new Square(5);
cout << r->getArea() << endl;
instead and initializing the object as a base class object?