Hello, everybody. I'm having some terrible task for homework and I'm just out of my mind.
Here it is:
Define a class Deque that represents a deque of strings by keeping its elements in a dynamically allocated array (with maximum capacity) and make the deque as a "circular array" (the first element follows the last one).
Supply the "big three" memory management functions. Use this class to demonstrate
(a) the difference between initialization
Deque s;
Deque t = s;
and assignment operation
Deque s;
Deque t;
s = t;
(b) the fact that all constructed objects are automatically destroyed
(c) the fact that the copy constructor is invoked if an object is passed by value to a function
(d) the fact that the copy constructor is not invoked when a parameter is passed by reference
(e) the fact that the copy constructor is used to copy a return value to the caller.
Supply member functions size, push_front and push_back. Overload the -- unary prefix operator (--s) for pop_front operation and -- postfix operator (s--) for pop_back operation. Overload the stream operators << and >> for output and input deque elements. Demonstrate all these functions and operators.
I've tried to implement something on my own, but what I get is some great deal of errors.
That's the header:
#ifndef _DEQUE_H
#define _DEQUE_H
#include <iomanip>
class Deque
{
friend std::ostream& operator<<(std::ostream& out, const Deque& a);
friend std::istream& operator>>(std::istream& in, const Deque& b);
private:
char *arr;
int size;
public:
Deque(); //default constructor
Deque(int size); //overloaded
~Deque(); //destructor
Deque(const Deque&v); //copy constructor
int range();
Deque& operator=(const Deque& b); //copy assignment operator
Deque operator--(); //prefix
Deque operator--(int a); //postfix
/*void push_front();
void push_back();*/
};
#endif
And this is the source:
#include <iostream>
#include "Deque.h"
#include <ostream>
#include <istream>
#include <iomanip>
using namespace std;
Deque::Deque()
{
arr = NULL;
size = 0;
}
Deque::Deque(int size)
{
this->size=size;
arr = new char[size];
for (int i=0; i<size; i++) arr[i]=' ';
}
Deque::~Deque()
{
if (arr)
{
delete []arr;
arr=NULL;
size=0;
}
}
Deque::Deque(const Deque& v)
{
size = v.size;
arr = new char[size];
for (int i=0; i<size; i++) arr[i]=v.arr[i];
}
int Deque::range()
{
return size;
}
/*void Deque::push_front()
{
}
void Deque::push_back()
{
}*/
Deque & Deque::operator=(const Deque& b)
{
if (arr)
{
delete []arr;
arr = NULL;
size = 0;
}
size = b.size;
arr = new char[size];
for (int i=0; i<size; i++) arr[i]=b.arr[i];
return *this;
}
Deque Deque::operator--() // prefix for pop_front ?!
{
*this = *this - 1;
return *this;
}
Deque Deque::operator--(int a) //postfix for pop_back
{
Deque b = *this;
*this = *this -1;
return b;
}
friend std::ostream& operator<<(std::ostream& out, const Deque& a)
{
out << "Deque of strings: ";
for (int i=0; i<a.size; i++) cout << a[i] << endl;
return out;
}
friend std::istream& operator>>(std::istream& in, const Deque& b)
{
for (int j=0; j<b.size; j++) in >> b[j];
return in;
}
I'm a rookie in programming and I just can't get it all. My main problems are with the operators' overloading. I know that you guys don't write full code and I respect that. I'm not asking you for doing this. I would appreciate some instructions on how to repair the errors and how do you get the idea of the task at all.
Thank you! :)