I've been playing around with dynamic allocation and queue recently. Keep in mind that I am fairly new to data structures and I’ve been teaching myself by watching YouTube tutorials and reading some tutorials from websites so forgive my ignorance if I don’t know every single thing there is to know about this subject. However, while playing around with the implementation of an assignment operator for my queue, I seem to have made a mistake somewhere in its code. You see whenever I try and use the assignment operator
i.e.
Queue names2;
names2 = names;
the assignment operator copies everything already in the queue except the empty elements of the array. Am I implementing my copy constructor or my assignment operator wrong that is causing it to not add the empty unused capacity to the newly created queue?
main.cpp
#include "Queue.h";
int main()
{
Queue names;
names.enqueue("A");
names.enqueue("B");
names.enqueue("C");
names.dequeue();
names.enqueue("D");
names.showFullQueue();
Queue names2;
names2 = names;
names2.showFullQueue();
names2.enqueue("Tom");
names2.enqueue("Alice");
names2.showFullQueue();
return 0;
}
Queue.h
#ifndef Queue_H
#define Queue_H
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
class Queue {
public:
Queue(); //Defualt costructor
Queue(const Queue& source); //Copy constructor
~Queue(); //Destructor
Queue& operator=(const Queue& source); //Assignament Operator //NOT WORKING
void enqueue(string value); // add item to queue
string dequeue(); // remove item from queue
void showFullQueue() const;
//memory allocation
void memory(int currentCapacity);
private:
void shift();
string * arr;
const static int front = 0;
int back;
int capacity;
int usedCapacity;
};
#endif // !Queue_H
Queue.cpp
#include "Queue.h"
Queue::Queue()
{
back = -1;
capacity = 1;
usedCapacity = 0;
arr = new string[capacity];
}
Queue::Queue(const Queue & source)
{
cout << "Invoking copy constructor" << endl;
this->back = source.back;
this->capacity = source.capacity;
this->usedCapacity = source.usedCapacity;
arr = new string[source.capacity];
copy(source.arr, source.arr + usedCapacity, this->arr);
}
Queue::~Queue()
{
delete[] arr;
}
Queue & Queue::operator=(const Queue & source)
{
if (this == &source)
return *this;
cout << "Invoking Assignament operator" << endl;
Queue temp(source);
swap(temp.back, back);
swap(temp.capacity, capacity);
swap(temp.usedCapacity, capacity);
swap(temp.arr, arr);
return *this;
}
void Queue::enqueue(string value)
{
++back;
arr[back] = value;
usedCapacity++;
memory(usedCapacity);
}
string Queue::dequeue()
{
string returnValue = arr[front];
shift();
usedCapacity--;
memory(usedCapacity);
return returnValue;
}
void Queue::showFullQueue() const
{
cout << "Front: " << front << endl;
cout << "Back: " << back << endl;
for (int i = 0; i < capacity; i++)
{
cout << arr[i] << ", ";
}
cout << endl;
}
void Queue::memory(int currentCapacity)
{
if (currentCapacity >= capacity)
{
int newCapacity = (currentCapacity * 3) / 2 + 1;
string * arr2 = new string[newCapacity];
copy(arr, arr + usedCapacity, arr2);
delete[] arr;
arr = arr2;
capacity = newCapacity;
}
else
cout << "allocation error";
}
void Queue::shift()
{
for (int i = front; i <= back; i++)
{
arr[i] = arr[i + 1];
}
--back;
}
Any help would be greatly appreciated. Thank you in advanced.