Question

In C++ please. 2.Explain the differences between shallow copy and deep copy. Explain the kind of...

In C++ please.

2.Explain the differences between shallow copy and deep copy. Explain

the kind of classes where the two copies differ. State the language

constructs that eliminate problems that this difference creates.

Homework Answers

Answer #1

1) shallow copy: copying the some address part of other object is known as shallow copy. changing the one object will automatically change the other object. shallow copy simply states that the two objects will have some shared memory.

2)deep copy: copying the value in the other object address is known as deep copy. changing the one object will not effect other object.

when a class is having a pointer to any type as a member, then copying the object of that class to the other object of same class type will leads to shallow copy.

class which aren't having pointes as memeber (directly or indirectly), then copying the objects of that class type leads to deep copy.

shallow copy and deep copy ex :

struct shallow { int∗ p; // a pointer };

shallow x {new int{0}};

shallow y {x}; // ‘‘copy’’ x

∗y.p = 1; // change y; affects x

∗x.p = 2; // change x; affects y

delete y.p; // affects x and y

y.p = new int{3}; // OK: change y; does not affect x

∗x.p = 4; // oops: write to deallocated memory

Deep copy:

y.p = new int{0};

*y.p = *x.p; // changing y doesn't effect x

user defined copy constructor (member wise initialization) eliminates the problems that this difference creates

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
explain the relationship between a deep copy and its pointer. explain the relationship between a shallow...
explain the relationship between a deep copy and its pointer. explain the relationship between a shallow copy and its pointer. how many bytes are incremented if a pointer such as *p is incremented using pointer arithmetic such as (depending upon which primitive type it point to; int, double, char etc). write a copy constructor to copy a class object containing a dynamic array. use a member by member copy techniques shown in the textbook. using correct c++ syntax, write the...
PLEASE DONT COPY AND PASTE. NO PLAGIARISM PLEASE. Identify the major similarities and differences between prokaryotic...
PLEASE DONT COPY AND PASTE. NO PLAGIARISM PLEASE. Identify the major similarities and differences between prokaryotic and eukaryotic cells. Where is the DNA housed in a prokaryotic cell? Where is it housed in a eukaryotic cell? What is the difference between the rough and smooth endoplasmic reticulum? Would an animal cell be able to survive without a mitochondria? Why or why not?
Enumerate 2 differences between fiscal policy monetary policy. Please explain it clearly and comprehensively
Enumerate 2 differences between fiscal policy monetary policy. Please explain it clearly and comprehensively
2. List five key differences between civil law and criminal law. What two factors constitute criminal...
2. List five key differences between civil law and criminal law. What two factors constitute criminal liability? Once you have created this material, explain what the term "the criminal act" means in this context as well as what "state of mind" refers to. What is the difference between these two identifiers?
Foot talks about moral significance of differences between doing and allowing, and connects this to the...
Foot talks about moral significance of differences between doing and allowing, and connects this to the concepts of negative duties and positive duties. Please explain one of Foot's examples where two negative duties clash. State which duty she takes to be the morally prevailing duty in that case and explain why it prevails.
Explain the differences between GC (gas chromatography) and LC (liquid chromatography) [principally and technically] Please give...
Explain the differences between GC (gas chromatography) and LC (liquid chromatography) [principally and technically] Please give me a detail answer at least two paragraphs.
1. In a few sentences: Explain the major differences between a specialty product and an unsought...
1. In a few sentences: Explain the major differences between a specialty product and an unsought product. 2. In a few sentences: Identify a product with different product types in two different countries. Explain the type in each country and the major reason behind the difference.
1. Explain 3 differences between Property, Plant and Equipment (PPE) and Intangible Assets. 2. After reading...
1. Explain 3 differences between Property, Plant and Equipment (PPE) and Intangible Assets. 2. After reading this chapter, how would you advise your friends in the following cases. Please EXPLAIN your responses using appropriate legal/accounting terms. a) Your friend, John, tells you that he wishes to open a coffee shop called, Starbucks. b) Your friend, Jill, tells you that she will use the "swoosh" logo on her coffee mugs and sell it on Amazon. c) Your friend, Alex, tells you...
Can you please explain the difference between OTC medication and prescription medications? And give 2 examples...
Can you please explain the difference between OTC medication and prescription medications? And give 2 examples of each.
C# Please just annswer all questions. if you going to answer 1 or 2 questions only...
C# Please just annswer all questions. if you going to answer 1 or 2 questions only please don't answer any. Thank you 1) To make it easy to test a class library, you can: a.   Make sure that all of the classes require only the System namespace b.   Put references to all of the .NET namespaces in the library c.   Add one class to the library at a time d.   Develop the class library project within a solution that contains...