Hi all, I need some help regarding dynamic vector.
Shape2DLink.cpp
void Shape2DLink::InputSensor()
{
string shape,type;
cout<<endl<<"\n"<<"[ Input sensor data ]"<<endl;
cout << "Please enter name of shape: " << endl;
cin >> shape;
shape2D.setName(shape);
cout << "Please enter special type : " << endl;
cin >> type;
shape2D.setWarpSpace(type);
if(shape == "Square")
{
square.setSquareCord();
square.computeArea();
}
else if(shape =="Rectangle")
{
rect.setRectCord();
rect.computeArea();
}
else if(shape == "Cross")
{
cross.setCrossCord();
cross.computeArea();
}
}
ShapeTwoD.cpp (parent class)
ShapeTwoD::ShapeTwoD()
{
string name = "";
bool containsWarpSpace = true;
}
ShapeTwoD::ShapeTwoD(string ShapeName, bool warpspace)
{
name = ShapeName;
containsWarpSpace = true;
}
double ShapeTwoD::computeArea()
{
return 0.00;
}
}
cross.cpp (children)
Cross::Cross()
{
xVal = 0;
yVal = 0;
}
Cross::Cross(string ShapeName, bool warpspace, int xval, int yval):ShapeTwoD(ShapeName, warpspace), xVal(xval), yVal(yval)
{
xVal = xval;
yVal = yval;
}
void Cross::setCrossCord()
{
for (int j=0; j<12; j++)
{
cout << "Please enter x-ordinate of pt " << j+1 << ": ";
cin >> xVal;
xvalue[j] = xVal;
cout << endl;
cout << "Please enter y-ordinate of pt " << j+1 << ": ";
cin >> yVal;
yvalue[j] = yVal;
cout << endl;
}
}
double Cross::computeArea()
{
int points = 12;
int running_total =0;
int i;
running_total = 0;
for (i=0; i<points-1; i++)
{
running_total += xvalue[i]*yvalue[i+1] - xvalue[i+1]*yvalue[i]; //cross calculation of coord in a cross
} //(x1*y2)-(y1*x1)
running_total += xvalue[points-1]*yvalue[0] - xvalue[0]*yvalue[points-1]; // traverse back to the origin point (xn*y1)-(yn*x1)
area = abs(running_total / 2); //purpose of absolute is to make sure result is positive.
//polygon are specified in counter-clockwise order (i.e. by the right-hand rule), then the area will be positive.
//cout << area;
return (area);
}
}
So i'm actually using polyphormism for calculating the area of the cross and other shapes.
so how do I actually make use of dynamically create an object this way?
do I create them in my Shape2DLink or in my individual child classes?