Question

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 function defination to overload the operator test for equality: == operator to determine if two objects that have identical components of their dynamic arrays used in above question .

Homework Answers

Answer #1

Deep copy and its pointer :

  • In deep copy, pointer should point to the different dynamically allocated memory to avoid disasterous consequences.
  • So we need to implement user defined copy constructor.

Shallow copy and its pointer :

  • In shallow copy, pointer point to the original allocated memory.
  • Default copy constructor uses the concept of shallow copy by default.

When we increment pointer by using pointer Arithmetic it uses formula internally how many bytes to increment.

int x=10;

int *p=&x;

p++;   // p=p+1*(sizeof(premitive_datatype)); --> suppose x address is 100 so p contains 100. 100+1*sizeof(int) i.e 4 so p=104 ( 4 bytes are incremented).

Copy Constructur to check identical items in object using overloading of == :

#include<iostream>

using namespace std;
class Demo
{
private:
   int * arr;
   int size;
public:
   Demo(int size)   // Parameterized Constructor
   {
       this->size = size;
       arr = new int[size];
   }
   Demo(const Demo& obj) // User defined Copy constructor
   {
       size = obj.size;
       arr = new int[obj.size];
       for (int i = 0; i<obj.size; i++)
           arr[i] = obj.arr[i];

   }
   void InsertValues() {   // Insert elements into dynamic arrays
       for (int i = 0; i < this->size; i++)
       {
           cin >> arr[i];
       }
       cout << "\n";
          
   }
   void PrintValues()   // Display dynamic array elements.
   {
       for (int i = 0; i < this->size; i++)
       {
           cout <<"\t"<<arr[i];
       }
       cout << "\n";
          
   }
   bool operator ==(Demo obj)   // == Operator overloading to check objects elements.
   {
       for (int i = 0; i < size; i++)
       {
           if (this->arr[i] != obj.arr[i])
               return false;
       }
       return true;
   }
  
};
int main()
{

   Demo obj1(3);            // Object 1
   obj1.InsertValues();   
   obj1.PrintValues();

   Demo obj2 = obj;       // Object 2    --> Here call goes to Copy Constructor
   obj2.PrintValues();

   obj1.InsertValues();   // Insert new elements in Obj1

   if (obj1 == obj2)     // Compare Objects
       cout << "\nBoth are same";
   else
       cout << "\nBoth are different";


   return 0;

}

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
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.
Write in C++. Define a class called Text whose objects store lists of words. The class...
Write in C++. Define a class called Text whose objects store lists of words. The class Text will be just like the class StringVar except that the class Text will use a dy- namic array with base type StringVar rather than base type char and will mark the end of the array with a StringVar object consisting of a single blank, rather than using '\0' as the end marker. Intuitively, an object of the class Text represents some text consisting...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write an implementation file to implement the namespace declared in the attached CSCI361Proj5.h. Name the implementation file as YourNameProj5.cpp and add it to the project. Run the project to see your grade. .h file: // Provided by: ____________(your name here)__________ // Email Address: ____________(your email address here)________ // FILE: link.h // PROVIDES: A toolkit of 14 functions for manipulating linked lists. Each // node of...