Operator Overloading
Operator Overloading
Copy Constructors when your class has pointers
you will need in addition to the files provided for you .h and .cpp files for the DynamicArray class
you will add the following functions to overload the operators +,= and ==
NOTE: the = operator overloading is very similar to a copy constructor.. the only real difference is that you pointers will already set.
for the DynamicArray class data will already be set to an array so you wont need to set it to a new array but copying the data members will be the same
File: functions_to_add.txt
// private data members
//publc functions
bool operator == (const DynamicArray & );// prototype
/*
bool DynamicArray::operator == (const DynamicArray & rhs )
Given:
if ( a == b)
capacity is the capacity for a (lhs, left hand side of == operator)
rhs.capacity is the capacity for b (rhs, right hand side of == operator
data is the array of a,the left hand side
rhs.data is the array of the right hand side
compare two DynamicArray objects for equality
if the capacity is not the same there is no way they can be equal
so return false
if the capacity is the same use a for loop and compare
all the items in the array of each item
if any dont match return false
if you have made it this far return true
*/
DynamicArray & operator = (const DynamicArray & );// prototype
/*
DynamicArray & DynamicArray::operator = (const DynamicArray & rhs)
this is just like the copy constructor in the video BUT we dont want to set datta to a new string array right off the bat..
because it is already set to some memory other than that one thing it is the same
if the capacity of the LHS is greater than or equal to the capacity of the RHS
you are good and you have enough room
just set the capacity of LHS to that of RHS
if the capacity of the rhs is larger than the left you will need to re size the array of the lhs object
delete the lhs dynamic arrray data,
set data pointer to a new array the same capacity as the rhs
now copy rhs.capacity to capacity
if our class had other static data members we would copy them as well
use a for loop to copy the elements from rhs.data to data
return the LHS object;
in an overloaded operator function such as this we pass in the right hand side object
"this" is a special pointer that is automaticaly set up for you it points to the left hand side object
when we end this function we need to return the lhs object so we can just return "*this"
it is important that the assignment operator overload returns the lhs object for nested assignment
consider this totaly valid operation for ints , char double , string and any data type
a = b = c = d;
if we return the left hand side object it will work.
it will just process the list left to right and keep going till it is complete
it will first process c = d and return c
a = b = c;
then it processes b = c and returns b
a = b;
then finally processes a = b and returns a
*/
DynamicArray operator + (const DynamicArray & );// prototype
/*
DynamicArray DynamicArray::operator + (const DynamicArray & rhs)
returns a DynamicArray object that has all the elements of both the LHS (this) and the RHS objects
1. create a temporary DynamicArray object , its size will be the sum of the capacity of LHS + RHS, you can pass the size to the constructor
2. copy the values from LHS into the temp object,
loop index 0 to < capacity
temp.date[loop] = data[loop];
3. copy the values from RHS into the temp object,
loop 0 to < rhs.capacity
data[loop+ capacity ] = rhs.data[loop];
4. return the temp object
example scenario
A+B
A is the LHS it has a capacity of 4
B is the RHS it has a capcity of 6
1 create a DynamicArray with size of 10 (6+4), pass to construcotor
2 copy 4 values from LHS to temp.data at index 0-3
3 copy 6 values from RHS to temp.date at index 4-9
4 return DynamicArray temp
*/
a Makefile,example and four mains have been provided for you
make sure the output to the console and the logfile match for all of the examples