The
constructor calls a set function that accepts four sets of coordinates and verifies that each one is
in the first quandrant with no single x or y coordinate larger than 20.0
Ok. So you must have the constructor make a call to a set function for verifying and initiating the coordinates. Each coordinate must be in quadrant 1 and no greater than 20.0. From this we know that we are looking for an unsigned float or double value ( 0.0 <= x <= 20.0); float is best here as we only go to 20.0.
The constructor will still need to read in the coordinates, grouped like in nvoight's example ( 4 structs of floats ), or ungrouped ( 8 floats ). Your choice.
Within the constructor, the values will be passed on to a set function for validation before being initialized.
Ok.
Code:
/*This code uses recursion so I am not certain if you follow
everything I am doing. This can be rewritten in another fashion
to accomplish the same thing, this is just more efficient. */
]// using a coordinate struct based on nvoight's example but with floats
Rectangle( Coordinate a, Coordinate b, Coordinate c, Coordinate d )
{
voSet_And_Validate_Coordinates( a,b,c,d );
}
void voSet_And_Validate_Coordinates(Coordinate xy1,
Coordinate xy2,
Coordinate xy3,
Coordinate xy4)
{
Coordinate xy[4] = {xy1, xy2, xy3, xy4};
for ( int i = 0; i < 4; i++)
{
if ((xy[i].x >= 0.0) && (xy[i].x <= 20.0))
{
if ((xy[i].y >= 0.0) && (xy[i].y <= 20.0))
{
m_xyCorner[i] = xy[i];
}
else
{
cout << "Coordinate " << xy[i].y << " is out of range.\n"
<< "Enter the new value: ";
cin >> xy[i].y;
// recursive call
voSet_And_Validate_Coordinates(xy[0], xy[1], xy[2], xy[3]);
return;
}
}
else
{
cout << "Coordinate " << xy[i].x << " is out of range.\n"
<< "Enter the new value: ";
cin >> xy[i].x;
// recursive call
voSet_And_Validate_Coordinates(xy[0], xy[1], xy[2], xy[3]);
return;
}
}
return;
}
The recursive calls allow for "on the fly" initialization. The function will continue to call itself until all coordinates are in the range of 0.0 to 20.0. This could probably be accomplished using another function call with a while loop to input data until the condition is met. This is more code though.
Hope this helps some. Again sorry for my earlier post.
David