Array with Deep Copy& Copy Constructor

Hi, I am trying to get my code to run, and I can't figure out the problem.
This program is suppose ask the user to enter and array and then print out the array while using the assignment operator, shallow copy, and copy constructor.
I think the problem is with the SetAll and Print functions definitions.

Please help me make it run!

//PointerData.h file
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
#ifndef POINTERDATA_H
#define POINTERDATA_H

class PointerDataClass
{
private:
    int x;
    int lenp;
    int *p;
public:
    void SetAll( int num, int sizeP, int *ptr);
    PointerDataClass(int sizeP = 10);       //constructor
    ~PointerDataClass();                    //destructor
    void print();
    const PointerDataClass & operator=(const PointerDataClass & right);     //Assignment operator function
    void destroyP();            //Destructor function
    PointerDataClass(const PointerDataClass &);     //copy constructor
};


#endif // POINTERDATA_H


//PointerData.cpp
#include "pointerData.h"
#include<iostream>
using namespace std;

//constructor
PointerDataClass::PointerDataClass(int sizeP)
{
    int x=0;
    if(sizeP > 0)
        lenp = sizeP;
    else
        lenp = 10;

    p = new int[lenp];
}

PointerDataClass::~PointerDataClass()
{
    delete [] p;
}

void PointerDataClass::destroyP()
{
    x= 0;
    lenp = 0;
    delete[] p;
    p = NULL;
}

const PointerDataClass & PointerDataClass::operator=(const PointerDataClass & right)
{
    if(this != &right)
    {
        destroyP();
        x= right.x;
        lenp= right.lenp;

        if(lenp > 0)
        {
            p= new int[lenp];
            for(int i = 0; i< lenp; i++)
                p[i] = right.p[i];
        }

        return *this;
    }
}

PointerDataClass::PointerDataClass(const PointerDataClass & right)
{
    x = right.x;
    lenp = right.lenp;

    if(lenp > 0)
    {
        p = new int[lenp];
        for(int i= 0; i< lenp; i++)
        {
            p[i]= right.p[i];
        }

        p= NULL;
    }

}

void PointerDataClass::print()
{
    cout << "These are the numbers you entered. \n";
    for(int i = 0; i < lenp; i++)
        cout << p[i] <<" ";

    cout << endl;
}


void PointerDataClass::SetAll( int num, int sizeP, int *ptr)
{
    //int x=0;
    if(sizeP > 0)
        lenp = sizeP;
    else
        lenp = 10;
    p = new int[lenp];
    
    cout << "Please enter 10 numbers. \n";
    for(int i= 0; i< lenp; i++)
    {
        cout << "# "<<i+1 <<": ";
        cin >> p[i];
    }
}


//main.cpp

#include "pointerData.h"
#include<iostream>
using namespace std;



int main()
{

    PointerDataClass *arr, *arr2;
    arr = new PointerDataClass[10];

    *arr->SetAll(arr, 10)
    arr->print();

    arr2= new PointerDataClass[10];
    for(int i= 0; i < 10; i++)
    {
        arr[i] = arr2[i];
    }

    cout << " These are the numbers for the second array (arr2). \n";
    *arr2.print();

    cout << endl;



    return 0;
}


Last edited on
Please use code tags: [code]Your code[/code]
Read this: http://www.cplusplus.com/articles/z13hAqkS/


The problem with SetAll(...) is that you provide the wrong parameter. Why does it require three parameter and what are the meanings?
In other words: Use more descriptive names.

This
1
2
*arr->SetAll(arr, 10)
arr->print();
can impossibly be right.

array while using the assignment operator, shallow copy, and copy constructor
Is this really correct? I would think it is deep copy.

http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/
Quite frankly, I don't know what I am doing in the SetAll function. I want to set the array and the array's size from the main function. That's why I thought I needed three parameters.

Is there can specific suggestion as to what I can do to fix the problem?

And yes, I meant to say "Deep Copy."
I don't know what's the meaning of int num in SetAll(...), but you don't use it.

You may change it like so:
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
void PointerDataClass::SetAll( int num, int sizeP, int *ptr)
{
    //int x=0;
    if(sizeP > 0)
        lenp = sizeP;
    else
        lenp = 10;
    p = new int[lenp]; // call destroyP(); before this in order to avoid memory leak
    
    if(sizeP > 0) // Note: copy the values provided
    {
        for(int i= 0; i< lenp; i++)
        {
            p[i] = ptr[i];
        }
    }
    else // It seems that it is not necessary to provide data
    {
        cout << "Please enter 10 numbers. \n";
        for(int i= 0; i< lenp; i++)
        {
            cout << "# "<<i+1 <<": ";
            cin >> p[i];
        }
    }
}

...


int main()
{

    PointerDataClass *arr, *arr2;
    arr = new PointerDataClass[10];

    int num[] = { 1, 2, 3, ... }; // Note: ... -> fill with as many numbers as you want

    // Note: sizeof(num) / sizeof(num[0]) -> The number of values of the array
    arr[0].SetAll(sizeof(num) / sizeof(num[0]), num); // Note: arr[0]
    arr[0].print();
...
Topic archived. No new replies allowed.