Copy Constructor Implementation

Copy Constructor Implementation

Medium C++ Classes & Objects 27 views
Explanation Complexity

Problem Statement

Create copy constructor to make deep copy of object. This teaches difference between shallow and deep copy.
Logic: Initialize new object using existing object's data

Input Format

No user input.
An object is created and copied using a copy constructor.

Output Format

Values of original and copied object (show they are independent).

Example

Original object value = 10
Original value = 10
Copied value   = 10

Constraints

• Class has a pointer data member

• Deep copy must be done in copy constructor

Concept Explanation

A shallow copy copies only the pointer address.
A deep copy creates new memory and copies the actual value.
Deep copy avoids two objects sharing the same memory.

Step-by-Step Explanation

1.Create a class with a pointer member (e.g., int* data).

2.Allocate memory for data in the constructor and assign a value.

3.Create a copy constructor that takes another object as parameter.

4.Inside copy constructor:

• Allocate new memory for data.

• Copy the value from the other object’s data.

5.Now both objects have separate memory with same value.

6.Changes in one object do not affect the other.

Concept Explanation

A shallow copy copies only the pointer address.
A deep copy creates new memory and copies the actual value.
Deep copy avoids two objects sharing the same memory.

Step-by-Step Explanation

1.Create a class with a pointer member (e.g., int* data).

2.Allocate memory for data in the constructor and assign a value.

3.Create a copy constructor that takes another object as parameter.

4.Inside copy constructor:

• Allocate new memory for data.

• Copy the value from the other object’s data.

5.Now both objects have separate memory with same value.

6.Changes in one object do not affect the other.

Input / Output Format

Input Format
No user input.
An object is created and copied using a copy constructor.
Output Format
Values of original and copied object (show they are independent).
Constraints
• Class has a pointer data member

• Deep copy must be done in copy constructor

Examples

Input:
Original object value = 10
Output:
Original value = 10 Copied value = 10

Example Solution (Public)

C++
class Array {
public:
    int* data;
    int size;
    
    Array(int s) {
        size = s;
        data = new int[size];
        for(int i = 0; i < size; i++) {
            data[i] = i + 1;
        }
    }
    
    Array(const Array& other) {
        size = other.size;
        data = new int[size];
        for(int i = 0; i < size; i++) {
            data[i] = other.data[i];
        }
    }
    
    void display() {
        for(int i = 0; i < size; i++) {
            cout << data[i] << " ";
        }
        cout << endl;
    }
    
    ~Array() {
        delete[] data;
    }
};

void question6_copy_constructor() {
    Array arr1(5);
    Array arr2 = arr1;
    
    cout << "Array 1: ";
    arr1.display();
    cout << "Array 2 (copy): ";
    arr2.display();
}

Official Solution Code

class Array {
public:
    int* data;
    int size;
    
    Array(int s) {
        size = s;
        data = new int[size];
        for(int i = 0; i < size; i++) {
            data[i] = i + 1;
        }
    }
    
    Array(const Array& other) {
        size = other.size;
        data = new int[size];
        for(int i = 0; i < size; i++) {
            data[i] = other.data[i];
        }
    }
    
    void display() {
        for(int i = 0; i < size; i++) {
            cout << data[i] << " ";
        }
        cout << endl;
    }
    
    ~Array() {
        delete[] data;
    }
};

void question6_copy_constructor() {
    Array arr1(5);
    Array arr2 = arr1;
    
    cout << "Array 1: ";
    arr1.display();
    cout << "Array 2 (copy): ";
    arr2.display();
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.