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! :)

Recommended Answers

All 5 Replies

Member Avatar for iamthwee

I always use this when referencing operator overloading in c++.

That page doesn't tell you everything you need to know either, but it is a good start.

Thank you! I'll keep in mind that. ;]

A couple of additional tips:

For your own sanity, consider using the C++ string type instead of character-arrays:

#include <string>
...
std::string str;

Also, there's nothing in your Deque class to indicate how the nodes in your queue are connected to each other, or how the deque keeps track of the ends. You may want to define two classes, perhaps called Deque and DequeNode . Don't forget about the "circular" requirement -- what does that mean as far as pushing nodes onto the ends of the deque?

Almost all of the demonstration can be accomplished by printing appropriate messages from within the "big 3" memory management functions, and then showing that the correct messages are printed for various calls, e.g.:

cout << "Instantiation does not use the copy constructor:" << endl;
cout << "DequeNode t = s;" << endl;
DequeNode t = s;
cout << "DequeNode t(s);" << endl;
DequeNode t(s);
commented: Excellent suggestions +16

This was really helpful, thank you! I'll do my best.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.