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;
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;
int points = 12;
int running_total =0;
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
running_total += xvalue[points-1]*yvalue - xvalue*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;
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?
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.
@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;