I'm reading a book of Herbert Schildt (2002) and learning about virtual functions. There is one example program there that the author assures is compilable. However, compiler doesn't compile it. Compiler says the problem is in
shapes[0] = &Square(2.0);
It may have something to do with the array of pointers to objects, and maybe with C++11 (if so, I don't understand why to disable this functionality of arrays). The program is a little long, so I cut it and simplified it to isolate the problem and to make it simple to read.
#include <iostream>
using namespace std;
// Base class for two-dimensional objects:
class TwoDShape {
double side;
char name[20];
public:
TwoDShape(double x) {
side = x;
}
double getSide() { return side; }
void setSide(double w) { side = w; }
virtual double area() = 0;
};
// Derived class for Squares:
class Square : public TwoDShape {
public:
Square(double x) : TwoDShape(x) { };
double area() { return getSide() * getSide(); }
};
int main() {
TwoDShape *shapes[3];
shapes[0] = &Square(2.0);
shapes[1] = &Square(6.0);
shapes[2] = &Square(9.0);
return 0;
}