Dynamic memory

I wrote this program for practice, and to test my code. There are a few problems.

First, I tried to make my copy constructor and assignment function correctly (not allow pointers to point to the same address). I did this incorrectly, they all end up pointing to the same address and I don't know what I did wrong.

Second, on lines 74, 75, I originally wrote *length=cpy.length; and *width=cpy.width; but got an error message and switched it to the way you see now. why is that correct, is it even correct?

Lastly, is all the dynamic memory being released?

If there is anything else wrong with my code please tell me, and as always thanks for your help.

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
#include <iostream>

using namespace std;

//calculates area, uses dynamic memory
class Area
{
    private:
        int *length;     //length
        int *width;      //width
    public:
        //constructors
        Area();
        Area(int l, int w);
        Area(const Area &cpy);

        //destructor
        ~Area();

        //operations
        Area& operator=(const Area &assign);

        //other member functions
        int area();                 //retruns the area
};

int main()
{
    //test the code
    Area house(7, 9);
    Area yard(house);
    Area street(20, 5);

    house=street;

    cout<<house.area()<<endl;
    cout<<yard.area()<<endl;

    street=house=yard;
    yard=yard;

    return 0;
}

//----------------------------------
//Area class functions

Area::Area()        //constructor
{
    //assign dynamic memory
    length=new int;
    width=new int;

    *length=0;
    *width=0;
}

Area::Area(int l, int w)        //constructor
{
    //assign dynamic memory
    length=new int;
    width=new int;

    *length=l;
    *width=w;
}

Area::Area(const Area &cpy)     //copy constructor
{
    //assign dynamic memory
    length=new int;
    width=new int;

    length=cpy.length;
    width=cpy.width;
}

Area::~Area()       //destructor
{
    delete length;
    delete width;
}

Area& Area::operator=(const Area &assign)       //assignment operator
{
    if(this != &assign)     //if not the same object
    {
        //release dynamic memory
        delete length;
        delete width;

        //assign new dynamic memory
        length=new int;
        width=new int;

        //copy the memory
        length=assign.length;
        width=assign.width;
    }

    //return the value assigned
    return *this;
}

int Area::area()        //return the area
{
    return *length * *width;
}

//----------------------------------------- 
You need *length=*(cpy.length);, since the length variable of cpy is also a pointer. Fix that everywhere, and the rest looks good. Note, you should have gotten a pretty big hint from the compiler in the error message it generated - learn to read and understand those errors, and you will usually be able to solve your own problems.
Last edited on
Topic archived. No new replies allowed.