All about C plus plus algorithms!

Thursday, 17 November 2016

Operators Overload || Assignment Operator || Creating your own operators in the class || C++ (C plus plus)

Previously, we had created a class in C++ called Dynamic Array with a number of utility functions (refer to Dynamic Array | Vector). But what would have happen if you had two objects of that class e.g.

DynamicArray obj1;
DynamicArray obj2;

And both had some data, say:

obj1 : 1,2,3,4,5,6
obj2 : 6,7,8,9,10,11

and if you do something like this :

obj1 = obj2;

This would result in calling for the standard assignment operator which will point the arr* in obj1 to the same location where arr* in obj2 is pointing, i.e. now both the objects are pointing to the same memory address in heap.


The image above shows the relation. The orange arrows show the data pointed by the arr* of each objects before assignment and the green pointers show the relation after the assignment i.e. now both the obj1 and obj2 are pointing to the arr {6,7,8,9,10,11} and the address of arr {1,2,3,4,5,6} has been lost (over-written). This results in two big errors:


  1. Leak of memory
  2. Double deletion of the same data
Say, the destructor first deallocates the data pointed by obj1 and then again attempts to deallocate it using obj2 as pointer. The causes the program to crash. Hence, you see, that you cannot use the standard assignment operator for this class. The method of copying of standard assignment operator is called Shallow Copy while what we need is Deep Copy which is the same procedure that we used in the function/operation where we doubled the size of dynamic arrays. (refer to Dynamic Memory allocation and DoubleCap() function in Dynamic Array Class).

What is Deep Copying?

Deep copying is copying element by element. In this method, there are the following steps:

  • If the object to be copied into (e.g. obj1 here) is not empty, first deallocate it.
  • Then reserve a new space on the heap memory.
  • Copy one by one elements (e.g. from obj2 to obj1 ).
We make operators in the public section.
Now let's write the code for assignment operator of DynamicArray class.

Code:

The prototype of it looks like :

<return type> operator <operator symbol> (<arguments>)

void operator = (const DynamicArray & obj)
//void because this operator won't allow cascading i.e. no such thing obj1 = obj2 = obj3
//const because we are not gonna make any changes in the argument passed
//& (passing by reference) because it avoids copying (refer to Passing by Reference in Functions)

The code:

void operator = (const DynamicArray & obj)
{
     if (arr == nullptr)
           delete [] arr;        //since we are calling it as obj1 = obj2 so obj2 is the argument passed
    //and obj1 one is the object that we are inside
   //which means when I write arr*, it is the private member of obj1

   arr = new int[obj.cap];  //to access functions or members for obj2, the argument,e.g. its capacity we write obj.cap (obj is the name of the argument... it is obj not obj1)
   
  for (int i = 0; i < obj.size; i++)
  {
      arr[i] = obj.arr[i];
  }

  size = obj.size;
  cap = obj.cap;
    
}

That's all for today. Now whenever you use = operator for the objects of this class, your own operator will be summoned. Isn't it cool? :P
You can also try making + , - operator or == and != operators. Try it yourself! ^^

Thank you for reading! Any questions or confusions? Comment below!
Share:

0 comments:

Post a Comment

Popular Posts

Blog Archive

Copyright by progrithms.blogspot.com. All Rights Reserved.. Powered by Blogger.

Copyright © Programs and Algorithms | Powered by Blogger

Design by ThemePacific | Blogger Theme by NewBloggerThemes.com