LB, Thanks. i have changed to -> operator. :) I meant to say Assn2.cpp has the main() function. Sorry to have confused you. I'm still trying to familiarise with the terms and all that C++ stuff.
doug4: My base class ShapeTwoD and derived classes have individual header and source files.
And another thing is....
I have 3 derived classes: Cross, Rectangle and Square. Each have different x and y values input by user. Eg, Cross has 12 sets of x,y coordinates, Rectangle and Square have 4 sets each. Can I know how to store this array of data x and y in ShapeTwoD by using pointers?
For this sets of code below, how do I write such that 4 sets of x,y coordinates will be stored in ShapeTwoD?
1 2 3 4 5 6 7 8 9 10 11 12 13
|
if(shape == "Square" || shape == "square")
{
pShape2d[nPos] = new Square();
for (int i=0; i<4; i++)
{
cout << "Please enter x-ordinate of pt." << i+1 << " : ";
cin >> x ;
pShape2d[nPos]->setX(x);
cout << "Please enter y-ordinate of pt." << i+1 << " : ";
cin >> y;
pShape2d[nPos]->setY(y);
}
}
|
I will also need to pass the x and y values to derived classes to compute area.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
//Square.cpp
//this is virtual function
double Square::computeArea()
{
for (int i=0; i<4; i++)
{
double length = sqrt(pow((x[i] - x[i+1]), 2) + pow((y[i] - y[i+1]), 2));
double area = pow(length, 2);
cout << "Square Area: " << area << endl;
break;
}
}
|
This is what I'm trying to achieve. When computeArea4All() is called, computeArea() function in derived classed will be implemented.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
//Assn2.cpp
int Assn2::computeArea4All()
{
int nCount=0;
for (int i = 0; i<nPos; i++)
{
double area = pShape2d[i]->computeArea();
pShape2d[i]->setArea(area);
nCount++;
}
cout << "\nComputation completed! ( " << nCount << " records were updated)" << endl;
return nCount;
}
|
I hope I'm making sense here. Thanks so much for helping. Have been stuck with this for days and still can't figure out.