Copy C`tor and operator=()

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.
Last edited on
operator = is supposed to first do what a destructor does and then do what copy constructor does.
Note that there is something wrong with your copy constructor. What is s? You need to first allocate as much space as you need for _contents and then copy elements form Q1._contents to it. Don't forget to copy other members.
Now, back to =. I don't think you should check the length of Q1. There is nothing wrong with assigning an empty Queue to something, is there? Though that's not really important.. You need to delete the old array, allocate a new one and copy elements from Q1 to it. Allocating things in a loop makes no sense.. Again, don't forget to copy other members.
Notes: You don't need to check if new returned 0. Firstly, on failure new throws an exception. Secondly unless you're doing something extreme, new is very unlikely to fail.
operator = returns a Queue& so you should replace "return;" with "return *this;"
thanks for the explainatoin :) i helped me a lot.
and another question, how i should copy the other members ?
Last edited on
Simply in a for loop do _contents[i] = Q1._contents[i];
thank you a lot :)
Topic archived. No new replies allowed.