Copy C`tor and operator=() implementation

Hi, i need some help with Copy C`tor and operator=() implementation, i don`t know how to implement operator=() with copy ctor when i have array of Customers*.
I need it for line 83 on main.cpp

Queue.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include "Customer.h"
using namespace std;

class Queue {
public:

	Queue();
	Queue(int size);
	Queue(const Queue& Q1): _size(Q1.size) {
		s = new Customer(Q1._contents->getId(), Q1._contents->getName());
	}
	~Queue();
	bool enqueue(Customer c);
	Customer dequeue();
	void print();
	Queue& operator= (const Queue& C1);

private:
	Customer* _contents;
	int _size;
	int _front;
	int _rear;
};

#endif 


Queue.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include "Queue.h"
#include "Customer.h"

#define DSIZE 10

Queue::Queue(){
	if ( (_contents = new Customer[DSIZE]) == 0 ) {
		cout << "Memmory allocation Failed." << endl;
		_size = 0;
	} else {
		_size = DSIZE;
	}
	_front = -1;
	_rear = -1;
}

Queue::Queue(int size){
	if ( (_contents = new Customer[size]) == 0 ) {
		cout << "Memmory allocation Failed." << endl;
		_size = 0;
	} else {
		_size = size;
	}
	_front = -1;
	_rear = -1;
}

Queue::~Queue() {
	if (_size != 0) 
		delete _contents;
}

bool Queue::enqueue(Customer c){
	if((_front==0 && _rear==(_size-1) || (_front==(_rear+1)))){
		cout << "Queue is Full." << endl; 
		return false;
	} else {
		if(_front==(-1) && _rear==(-1)){
			_rear=0;
			_front=0;
		} else {
			if(_front!=0 && _rear==_size){
				_rear=0;
			} else {
				_rear=_rear+1;
			}
		}
		_contents[_rear]=c;
		return true;
	}
	return false;
}

void Queue::print(){
	if (_front == -1){
		cout << "Queue is Empty" << endl;
		return;
	}
	int index = _front;
    int queuelength = (_rear+_size-_front) % _size;
    for(int index = _front; index <= _front + queuelength; index++)
    {
		cout << _contents[(index+_size) % _size].getId() << " ";
    }
	cout << endl;
}

Customer Queue::dequeue()
{
	Customer c;
	if(_front==(-1) && _rear==(-1))
		return 0;
	else
	{
		int tmp;
		if ((_front==_rear) || (_front==_size && _rear==_size)){
			tmp=_front;
			_front=_rear=(-1);
		}
		else {
			tmp=_front;			
			if (_front == (_size-1))
				_front = 0;
			else
				_front++;
		}
		c = _contents[tmp];
		_contents[tmp]=0;
	}
	return c;
}
//Here -->
Queue& Queue::operator= (const Queue& Q1){
	if (Q1._front == -1){
		cout << "Queue is Empty" << endl;
		return;
	}
	int i=0;
	delete _contents;
	for(i=0; i<Q1._size; i++){
		_rear = _front = -1;
		if ((_contents = new Customer[Q1._size]) == 0){
			cout << "Memmory allocation Failed." << endl;
			_contents=0;
			return;
		}


Customer.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef _CUSTOMER_H_
#define _CUSTOMER_H_

#include <string>
using namespace std;

class Customer {
public:
	Customer(int id=1111, string name="lili"):_id(id),_name(name){}

	int getId() const {return _id;}
	string getName() const {return _name;}

private:
	int _id;
	string _name;
};

#endif 


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include "Queue.h"
#include "Customer.h"

using namespace std;

int main() {
	Queue q1;
	Queue q2(3);
	Queue q3(5);

	for (int i = 0; i < 13; i++)
	{
		Customer c(i);
		bool in = q1.enqueue(c);
		if (i <= 9 && !in)
		{
			cout << "ERROR: default size is smaller than 10!!" << endl;
		}
		else if (i > 9 && in)
		{
			cout << "ERROR: default size is bigger than 10!!" << endl;
		}
	}
	q1.print();
	cout << "0 1 2 3 4 5 6 7 8 9 ***********" << endl;

	for (int i = 0; i < 10; i++)
	{
		Customer el = q1.dequeue();
		if (i != el.getId()){
			cout << "Error: dequeue order is not correct!!";
		}
	}
	cout << endl;

	Customer underflow = q1.dequeue();
	if (underflow.getId() != 0)
	{
		cout << "ERROR: underflow not taken care of!!" << endl;
	}
	Customer c1(12, "moni");
	if (!q3.enqueue(c1))
	{
		cout << "ERROR: cannot add element to queue 3!!" << endl;
	}
	Customer c2(14, "mobi");
	if (!q3.enqueue(c2)){
		cout << "ERROR: cannot add element to queue 3!!" << endl;
	}

	Queue q4(q3);

	if (q3.dequeue().getId() != 12)
	{
		cout << "ERROR: cdequeue should return the first element in line (12)!!" << endl;
	}

	if (!q4.enqueue(21)){
		cout << "ERROR: cannot add element to queue 4!!" << endl;
	}

	if (!q4.enqueue(7)){
		cout << "ERROR: cannot add element to queue 4!!" << endl;
	}

	if (!q4.enqueue(332)){
		cout << "ERROR: cannot add element to queue 4!!" << endl;
	}

	if (q4.enqueue(12)){
		cout << "ERROR: add element number 6 to queue with size 5 (q4)!!" << endl;
	}
          
	q4.print();
	cout << "12 14 21 7 332 ***********" << endl;
	q3.print();
	cout << "14 ***********" << endl;

	q2.print();
	cout << "queue is empty! ***********" << endl;

	q2 = q3;
	q2.print();
	cout << "14 ***********" << endl;

	if (!q2.enqueue(17)){
		cout << "ERROR: cannot add element to queue 2!!" << endl;
	}

	if (!q2.enqueue(18)){
		cout << "ERROR: cannot add element to queue 2!!" << endl;
	}

	if (!q2.enqueue(3521)){
		cout << "ERROR: cannot add element to queue 2!!" << endl;
	}

	q2.print();
	cout << "14 17 18 3521 ***********" << endl;
	q3.print();
	cout << "14 ***********" << endl;
	return 0;
}


Thank.
Maybe it is that you need:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Queue& Queue::operator = (const Queue &q)
{
   if(*this == q)
      return *this;
   
   try
   {
      if(_contents)
      {
         delete [] _contents;
         _contents = 0;
      }
      if(q._size)
         _contents = new Customer[q._size];
   }
   catch (const exception &exc)
   {
      cout << exc.what();
      exit(1);
   }

   _size = q._size;
   /* _rear & _front assignments */
   for(int i = 0; i < _size; i++)
      _contents[i] = q._contents[i];

   return *this;
}

And what should _rear and _front store?
it should be -1, i fixed it.
ny question now is what the copy ctor should do ?
It should initialize *this object using another. (It's is needed, for example, when you use dynamic memory).
http://en.wikipedia.org/wiki/Copy_constructor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Queue::Queue(const Queue &q)
: _size(q._size), _rear(q._rear), _front(q._front), _contents(0)
{
    if(_size)
    {
        try
        {
            _contents = new Customer[_size];
            for(int i = 0; i < _size; i++)
                _contents[i] = q._contents[i];
        }
        catch (const exception &exc)
        {
            cout << exc.what() << endl;
            exit(1);
        }
    }    
}
And two notes:
1. arrays must be deleted like this:
delete [] ptr;
2. if new operation failed, it throws an exception (bad_alloc), if you want it only to return a NULL-pointer, specify one more argument:
int *ptr = new(nothrow) int;
thank you man, it was very helpful :)
Topic archived. No new replies allowed.