Ad Code

Define deep copy?

Define deep copy.


We write our own deep copy code in copy constructor so that when we create new object from an existing object using copy constructor we also allocate new dynamic memory for data members involving dynamic memory as shown below,

Student::Student( const Student & obj){

            int len = strlen(obj.name);
            name = new char[len+1];
             // assignming new dynamic memory to data member name of char * type for newly created object*/
            strcpy(name, obj.name);
            …
            //copy rest of the data members in the same way
}
Reactions

Post a Comment

0 Comments