1// Copy constructor
2 Point(const Point &p2) {x = p2.x; y = p2.y; }
3
4 int getX() { return x; }
5 int getY() { return y; }
6};
1/*
2 * The copy constructor in C++ is used to construct one object based on another.
3 * Specifically, a 'deep' copy is made, such that any heap-allocated memory
4 * is copied fresh. Thus, the copied-to object doesn't rely on the copied-from
5 * object after the constructor is called.
6 *
7 * The copy constructor is called as follows:
8 *
9 * Array a;
10 * // ... put data in a ...
11 * Array b(a);
12 *
13 * See below for implementation details.
14 */
15
16/*
17 * Function: Example copy constructor for an Array class.
18 * Parameters: An Array to make a deep copy of
19 * Effects: This is now a deep copy of other
20 * Notes: It's often convenient to use a 'copy' function, as this is also done in the =operator overload.
21 */
22Array::Array(const Array& other) {
23 copy(other); // make a deep copy of all memory in 'other' to 'this'
24}
25
26/*
27 * Function: copy
28 * Parameters: An array to make a deep copy of
29 * Returns: None
30 * Effects: Makes a deep copy of other into this.
31 */
32void Array::copy(const Array& other) {
33 for (int = 0; i < other.len; i++) {
34 this->data[i] = other.data[i];
35 }
36 this->len = other.len;
37}
38