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?
please advice..
I think you're mashing two separate concepts into one. The fact that you are storing your shapes in a vector has no bearing on the fact that you are using polymorphism to calculate the area of a shape. What is it you're having trouble with? And can we see the header files? There maybe something that I'm missing.
@Computergeek01 Hi, yup you're right i'm actually using polymorphism to calculate the area of a shape. so how that I have 3 different classes of shape 1) square 2) rect) 3) cross and I need to store them all in a vector so that I can do a sort later..
I gotcha, then yes you could make an array of pointers to the base class and store them that way. As for calculating the area of each shape, you would make the area function virtual and define it for each struct\class.
I would personally put that in my main function but that's really more of an organizational question.
EDIT: You seem to have a functional idea of what you are doing and other then some evidence suggesting that you may be including the std namespace in your header files I don't see any problems.
@Computergeek01 but it's ok to put implement it this way right? am i right to say that if the user choose square.
after keying in all the coord and computing the area, all the coordinates, name of shape etc would all be stored in Shape2D vector because of "objs.push_back(new Square(*s));"
@Computergeek01 yup I'm have already compiled it and i'm getting some compilation error
Shape2DLink.cpp: In member function 'void Shape2DLink::InputSensor()':
Shape2DLink.cpp:46:31: error: no match for 'operator*' in '*square'
Shape2DLink.cpp:52:33: error: no match for 'operator*' in '*rect'
Shape2DLink.cpp:58:29: error: no match for 'operator*' in '*cross'
Are 's', 'r' and 'c' pointers to 'square', 'rect' and 'cross'? If they are you can get rid of them because the "new" keyword creates the new object as a pointer. You may need to write a copy constructor for these though, I can never remember that one.
EDIT: Also that error maybe because you're using the wrong operator to assign your pointers maybe? For example, you want Square* s = □ not something like Square* s = *square;