I have to create three shapes, Square, Cross, Rectangle. This makes up three classes.
There is one super class, Shape.
So basically when I did not try any of the polymorphism but I did declare:
class Shape{
private:
int x,y;
public:
...
}
class Square: public Shape{
}
If I have x,y in Shape, I do not need x,y to exist in Square right?
I have this all working, like other methods including setX, and setY.
Is there any good methods to store my x and y datas?
For now, it is working fine when I did not use any virtual functions.
Previously I did use virtual functions.
I had two arrays.
Shape *arrayShapes[100];
Shape *dataCoords[100][12];
I am using two arrays because I felt that this way I can store the data of the coords better, when I am trying to extract data out.
i.e
int count=0;
If a user enters the name of the shape, it will cin into "shapeName", then into arrayShapes[count]->setName(shapeName);
The first would be a square, hence when the user is trying to input the coords, I will send them into
dataCoords[count][sides];
So the user will enter (0,0), (0,2), (2,2), (2,0). This 4 points makes up the square.
The problem arises here. When I am using virtual function to do this, my program hangs and closes. But when I remove all virtual function and continue my work normally, it is fine. Everything prints out accordingly.
Is there any better way to store my coords and how do I work around this to include polymorphism and virtual function? I need it to be dynamic.
Any read-ups is also good for me. Need a reference/tutorial.
Please help!