Hi Pethead,
Hope you don't mind this very long post..
I believe the process of debugging gives a lot more valuable experience than knowing the bug / correct solution.
(hopefully you'll get something more from this post... apart from where you went wrong)
okay, how do we debug your code ? Let's Try testing it incrementally, part by part...
You were doubtful about the copy constructor. So Lets test it first ..
(Refer Note 1)
[Test1]: Testing the Copy Constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
int main()
{
Polygon polygon1(3);
polygon1[0] = Point(0,0);
polygon1[1] = Point(1,1);
polygon1[2] = Point(2,2);
Point point1(6,5);
// Testing copy constructor
cout << polygon1[0] << endl;
cout << polygon1[1] << endl;
cout << polygon1[2] << endl;
cout << endl;
Polygon p1 = polygon1; // This invokes the copy constructor
cout << p1[0] << endl;
cout << p1[1] << endl;
cout << p1[2] << endl;
// polygon1 = polygon1 + point1;
// cout << polygon1 << endl;
system("pause");
return 0;
}
|
(refer Note 2)
output:
This is obviously wrong..
Now, Why is it wrong ?
Problem 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
Polygon(const Polygon &polygon) // Copy constructor
{
index = polygon.index;
for(int i = 0; i < index; i++) // index number of times
{
if(polygon._point) // if polygon._point is not NULL
{
_point = new Point[index]; // allocate memory for 'index' Point objects
// and store the starting address in _point
*_point = *polygon._point; // see below ..
}
else
_point = 0;
}
}
|
couple of points regarding the above:
1. you are allocating memory for 'index' Point objects each time you are passing through the loop and you run the loop 'index' times. Totally you are allocating memory for 'index' * 'index' Point objects of which only the last 'index' are accessible and rest are leaked memory.
2. better to use *(polygon._point) so we don't have to worry about precedence
Solution:
I don't understand how you came up with the above code for the copy constructor. So, I couldn't 'correct' it.
When you have dynamic content in an object. In its (i.e. it's class's) copy constructor you want to do the following:
1. copy static members
2. allocate memory for dynamic members
3. copy the dynamic members
Here is my version of the code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
Polygon(const Polygon &polygon) // Copy constructor
{
// copy static members
index = polygon.index;
// allocate memory for dynamic members
_point = new Point[index];
// copy the dynamic members
for(int i=0; i<index; i++)
_point[i] = (polygon._point)[i];
}
|
Test this using [Test1] above and the copy constructor should be fine ..
PROBLEM 2:
but there is one more simple problem with your code on line 57
polygon[i] = polygon[i] + point;
and on line 70
polygon[i] = polygon[i] * point;
try solving it and if you cant post in this thread again .. i'll help you with it :)
[HINT
1. Try thinking about what you want to add/multiply to what and where you want to store the result
2. check if this is what your code does]
---------
Note [1]:
Ideally you'd start with the smallest independent blocks, like the point class functions, etc. that the rest of the program depends on and proceed from there ... but for now, let's start with the constructor
---------
-----------
Note [2]:
I have intentionally avoided using something like ..
cout << polygon1 << endl;
because passing an object by value to a function invokes the copy constructor to copy actual parameter into the formal parameter.
For example:
1 2 3 4 5 6 7 8 9 10 11 12
|
void func(Polygon obj1)
{
....
....
}
int main()
{
...
func(poly_obj); // This invokes the copy constructor
...
}
|
And if you don't define a copy constructor explicitly, the compiler provides a default copy constructor that copies member by member. Therefore objects with dynamic memory may not copied properly.
case in point:
ostream& operator <<(ostream& os, Polygon polygon) // this invokes copy constructor
You can verify this by adding something like ...
cout << "\tcopy constructor invoked .." << endl;
in your copy constructor
Try and verify yourself ... After all, not everything posted on the internet is true :P
-----------