What is deep and shalow copy?

Answer Posted / naga

Consider two objects, A and B, which each refer to two
memory blocks xi and yi (i = 1, 2,...). Think of A and B as
strings and of xi and yi (i = 1, 2,...) as the characters
they contain.





Shallow copy
One of them is the shallow copy. In this process B is
attached to the same memory block as A. This is otherwise
known as address copy

Deep copy
An alternative is a deep copy. Here the data is actually
copied over.

Example:
class base
{
public:
int i;
base()
{
i=0;
}
base(int j)
{
i=j;
}
};
main()
{
using namespace std;
base *p1=new base(23);
base *p2;
//Shallow copy, here we are using pointers and we are
copying content of one pointer to another.so address will
get copied.
p2=p1; // address is copied
cout<<"\naddress of P1:"<<p1;
cout<<"\nvalue at p1:"<<p1->i;
cout<<"\naddress of P2:"<<p2;
cout<<"\nvalue at p2:"<<p2->i;
delete p2;
cout<<"\naddress of P1 after delete:"<<p1;
cout<<"\nvalue in P2 after delete:"<<p2->i;

//DEEP copy, here we are creating objects and we are
copying content of one object to another.So content will
get copied.

base o1(67);
base o2;
o2=o1;//contents are copied. But, the addresses
remained different
cout<<"\nvalue in o1:"<<o1.i;
cout<<"\nvalue in o2 after copy:"<<o2.i<<endl;
return 0;
}

output:
address of P1:0x00323C88
value at p1:23
address of P2:0x00323C88
value at p2:23
address of P1 after delete:0x00323C88
value in P2 after delete:-572662307
value in o1:67
value in o2 after copy:67

Is This Answer Correct ?    18 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What does sksksk mean in text slang?

1545


What is property in oops?

573


Who invented oop?

659


They started with the brief introduction followed by few basic C++ questions on polumorphism, inheritance and then virtual functions. What is polymorphims? How you will access polymorphic functions in C? How virtual function mechanism works?

1401


Will I be able to get a picture in D drive to the c++ program? If so, help me out?

1661






what is difference between class template and template class?

2165


What is class in oop with example?

625


Write a program to compute for numeric grades for a course. The course records are in a file that will serve as the input file. The input file is in exactly the following format: Each line contains a student's first name, then one space, then ten quiz scores all on one line. The quiz scores are in whole number and are separated by one space. Your program will take it input from this file and sends it output to a second file. The data in the output file will be exactly the same as the data in the input file except that there will be one additional number (of type double) at the end of each line. This number will be the average of the student's ten quiz scores. Use at least one function that has file streams as all or some of its arguments.

2582


Can main method override?

589


What is a class oop?

596


What is persistence in oop?

677


What is the real time example of encapsulation?

602


IS IT NECESSARY TO INITIALIZE VARIABLE? WHAT IF THE INSTANCE VARIABLE IS DECLARED final ? IS IT NECESSARY TO INITIALIZE THE final VARIABLE AT THE TIME OF THEIR DECLARATION?

1585


Where You Can Use Interface in your Project

1433


How to improve object oriented design skills?

573