inheritance with copy constructors

I'm working on a final project for school, and I can't get my inherited copy constructors to work. I have a ton of code from the project, so I'll only post the two classes that I'm having troubles with. Maybe someone can see or point me in the right direction. The code will compile, but when I try to do an actual copy of the object, it starts to do it, and then I get the error:

 
*** glibc detected *** ./run: double free or corruption (fasttop): 0x099e90f8 ***


Base class "StevenRectangle.cpp"

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "StevenFraction.h"
#include "StevenPoint.h"
#include "StevenRectangle.h"
#include <iostream>
using namespace std;

StevenRectangle::StevenRectangle( void ) {

   lowerLeftPtr = new StevenPoint( 0, 0 );
   widthPtr = new StevenFraction( 1 );
   lengthPtr = new StevenFraction( 1 );
}

StevenRectangle::StevenRectangle( StevenPoint arg1, StevenFraction arg2, 
                                  StevenFraction arg3 ) {

   lowerLeftPtr = new StevenPoint( arg1 );
   widthPtr = new StevenFraction( arg2 );
   lengthPtr = new StevenFraction( arg3 );

}

StevenRectangle::StevenRectangle( int arg1, int arg2 ) {

   lowerLeftPtr = new StevenPoint( 0, 0 );
   widthPtr = new StevenFraction( arg1 );
   lengthPtr = new StevenFraction( arg2 );
}

StevenRectangle::StevenRectangle( StevenPoint arg ) {

   lowerLeftPtr = new StevenPoint( arg );
   widthPtr = new StevenFraction( 1 );
   lengthPtr = new StevenFraction( 1 );
}

// copy constructor
StevenRectangle::StevenRectangle( const StevenRectangle& arg ) {

   lowerLeftPtr = arg.lowerLeftPtr;
   widthPtr = arg.widthPtr;
   lengthPtr = arg.lengthPtr;
}

StevenRectangle::~StevenRectangle( void ) {

   delete lowerLeftPtr;
   delete widthPtr;
   delete lengthPtr;

}

void StevenRectangle::setPoint( StevenPoint arg ) {

   *lowerLeftPtr = arg;

   return;
}

StevenPoint* StevenRectangle::getPoint( void ) {

   return lowerLeftPtr;

}

void StevenRectangle::setWidth( StevenFraction arg ) {

   *widthPtr = arg;
  
   return;
}

StevenFraction* StevenRectangle::getWidth( void ) {

   return widthPtr;

}

void StevenRectangle::setLength( StevenFraction arg ) {

   *lengthPtr = arg;

   return;
}

StevenFraction* StevenRectangle::getLength( void ) {

   return lengthPtr;
}

StevenFraction StevenRectangle::computeArea( void ) {

   return *lengthPtr * *widthPtr;

}

bool StevenRectangle::operator>( StevenRectangle& arg ) {

   if ( computeArea() > arg.computeArea() )
      return true;

   return false;
}

bool StevenRectangle::operator<( StevenRectangle& arg ) {

   if ( computeArea() < arg.computeArea() )
      return true;

   return false;
}

bool StevenRectangle::operator==( StevenRectangle& arg ) {

   if ( computeArea() == arg.computeArea() )
      return true;

   return false;
}

bool StevenRectangle::operator!=( StevenRectangle& arg ) {

   if ( computeArea() != arg.computeArea() )
      return true;

   return false;
}

ostream& operator<<( ostream& out, StevenRectangle& arg ) {

   // ex
   // Anchor : ( 4 , 3 ) Width : ( 7 ) Length : ( 3 ) 
   out << "Anchor : "; 
   
   if ( arg.lowerLeftPtr->getX().getDenom() == 1 )
      out << "( " << arg.lowerLeftPtr->getX().getNum() << " , ";
   else
      out << "( " << arg.lowerLeftPtr->getX().getNum() << "/"
          << arg.lowerLeftPtr->getX().getDenom() << " , ";

   if ( arg.lowerLeftPtr->getY().getDenom() == 1 )
      out << arg.lowerLeftPtr->getY().getNum() << " )";
   else
      out << arg.lowerLeftPtr->getY().getNum() << "/"
          << arg.lowerLeftPtr->getY().getDenom() << " )";

   if ( arg.widthPtr->getDenom() == 1 )
      out << " Width : ( " << arg.widthPtr->getNum() << " )";
   else
      out << " Width : ( " << arg.widthPtr->getNum() << "/"
          << arg.widthPtr->getDenom() << " )";

   if ( arg.lengthPtr->getDenom() == 1 )
      out << " Length : ( " << arg.lengthPtr->getNum() << " )";
   else 
      out << " Length : ( " << arg.lengthPtr->getNum() << "/"
          << arg.lengthPtr->getDenom() << " )";

   return out;
}


sub class

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include "StevenFraction.h"
#include "StevenPoint.h"
#include "StevenRectangle.h"
#include "StevenBox.h"
using namespace std;

StevenBox::StevenBox( void ) : StevenRectangle() {

   heightPtr = new StevenFraction( 1 );

}

StevenBox::StevenBox( StevenPoint lowerLeftPtr, 
                      StevenFraction widthPtr, 
                      StevenFraction lengthPtr, 
                      StevenFraction myHeight )
                      : StevenRectangle( lowerLeftPtr, widthPtr, lengthPtr ) {

   heightPtr = new StevenFraction( myHeight );
}

// copy constructor. 
StevenBox::StevenBox( const StevenBox& arg ) : StevenRectangle( arg ) {

   heightPtr = arg.heightPtr;

}

StevenBox::~StevenBox( void ) {
 
   delete heightPtr;

}

ostream& operator<<( ostream& out, StevenBox& arg ) {

   out << "Anchor : "; 
   
   if ( arg.lowerLeftPtr->getX().getDenom() == 1 )
      out << "( " << arg.lowerLeftPtr->getX().getNum() << " , ";
   else
      out << "( " << arg.lowerLeftPtr->getX().getNum() << "/"
          << arg.lowerLeftPtr->getX().getDenom() << " , ";

   if ( arg.lowerLeftPtr->getY().getDenom() == 1 )
      out << arg.lowerLeftPtr->getY().getNum() << " )";
   else
      out << arg.lowerLeftPtr->getY().getNum() << "/"
          << arg.lowerLeftPtr->getY().getDenom() << " )";

   if ( arg.widthPtr->getDenom() == 1 )
      out << " Width : ( " << arg.widthPtr->getNum() << " )";
   else
      out << " Width : ( " << arg.widthPtr->getNum() << "/"
          << arg.widthPtr->getDenom() << " )";

   if ( arg.lengthPtr->getDenom() == 1 )
      out << " Length : ( " << arg.lengthPtr->getNum() << " )";
   else 
      out << " Length : ( " << arg.lengthPtr->getNum() << "/"
          << arg.lengthPtr->getDenom() << " )";

   if ( arg.heightPtr->getDenom() == 1 )
      out << " Height : ( " << arg.heightPtr->getNum() << " )";
   else
      out << " Height : ( " << arg.heightPtr->getNum() << "/"
          << arg.heightPtr->getDenom() << " )";

   return out;
}


I've tried sending everything as an argument list, I've tried using a dynamic_cast, I've tried using a function to call the rectangle object. I dunno, I think the teacher just hates us. He never went over ANYTHING like this in class.
Your copy constructors need to take "deep copies" of the pointers, not just copy
the pointers. Also, you need to write operator= that does the same.
One could define 2D objects as a subclass of 3D objects with one dimension always 0. But 2D objects are certainly not a superclass of 3D objects.

One might use a 2D object to define a 3D object. But that would be done using aggregation, not inheritance.
Topic archived. No new replies allowed.