printing data from template class

I have created a template class called Queue that stores elements using linked list. I have also created a new type called Date.
is there any way to output Date date using Queue functions?
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
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <string>
#include <cctype>
using namespace std;


template <typename T> struct node{
	T value;
	node<T> *next;
};

template <typename T> class Queue{
	node<T> *list;
	int QueueSize;
public:
	explicit Queue(){
		list=0;
		QueueSize=0;
	}
	Queue(const Queue &queue){
		QueueSize= queue.QueueSize;
		list = queue.list;
	}
	~Queue(){
		delete list;
	}
	const Queue<T> &operator=(const Queue &queue) {
		if (this != &queue){
			list = queue.list;
			QueueSize = queue.QueueSize;
		}
		return *this;
	}
	void enqueue(const T &e){
		QueueSize++;
		node<T> *current;
		
		current=(node<T> *)malloc(sizeof(node<T>));

		current->value= e;
		current->next=0;

		if (list == 0){
			list = current;
		}
		else{
			node<T> *temp;
			temp=(node<T> *)malloc(sizeof(node<T>)); 
			temp = list;
			while (temp->next != 0){
				temp = temp->next;
			}
			temp->next = current;
		}
	}
	
	T &dequeue(){
		T lastElement;
		if (QueueSize == 0){
			cout << endl << "ERROR: dequeue element while Queue has no element";
			exit(EXIT_FAILURE);
		}
		else if (QueueSize == 1)
			list = NULL;
		else{
			QueueSize--;
			node<T> *temp;
			temp=(node<T> *)malloc(sizeof(node<T>)); 
			temp = list;
			while (temp->next->next != NULL){
				temp = temp->next;
			}
			lastElement = temp->value;
			temp->next=NULL;
		}
		return lastElement;
	}
	const void print(){
		if (QueueSize == 0)
			cout << "No elements to prints";
		node<T> *temp;
		temp=(node<T> *)malloc(sizeof(node<T>)); 
		temp = list;
		while (temp != 0){
			cout << temp->value << " ";
			temp = temp->next;
		}
		cout << endl;
	}
	void clear(){
		list = 0;
		QueueSize = 0;
		cout << "Queue has been cleared" << endl;
	}
	const bool isEmpty(){
		if (QueueSize == 0)
			return true;
		else
			return false;
	}
	const int size(){
		return QueueSize;
	}
	
};

class Date{
	int day, month, year;
public:
	Date(){
	}
	Date(int d, int m, int y)
		:day(d), month(m), year(y)
	{}

};
You can overload the operator<< for Date.

http://www.parashift.com/c++-faq/output-operator.html

Demonstration:

http://ideone.com/1TZHJI
Thanks. print function is working now with date :)
I now see some problems with your code (counted from worst to least bad).

1) You use std::malloc() instead of using the new operator.
As a C++ programmer you should use new as in: temp = new node<T>;.

The difference between them is that new allocates the memory and then calls the constructor, while std::malloc() just allocates the memory, without calling the constructor.

Same problems with delete versus std::free().
The operator delete would call the destructor, while the function std::free() would not.

2) By the way, you're not using std::free() or delete in your code.
This means that you have memory leaks.

A memory leak is when you allocate memory, and then don't deallocate it when you no longer need it.
Normally this just means your program "uses" more memory than it needs.
In very extreme cases, the computer will slow down or the system will crash.

https://en.wikipedia.org/wiki/Memory_leak
http://www.cplusplus.com/doc/tutorial/dynamic/

3) Do not overuse dynamic memory allocation.
You can probably rewrite your program to be more elegant, without using so many pointers and memory allocations.

4) You mark your const member functions the wrong way.

http://www.parashift.com/c++-faq-lite/const-member-fns.html

1
2
3
4
5
6
7
8
const void print() // wrong
{
    // ...
}

void print() const // correct
{
}


5) The explicit keyword is used for constructors that take one parameter, and that parameter is not of the class' type, in this case Queue.

The purpose of using explicit is to prevent automatic conversions between types.

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
class A
{
public:

    A(int ) // not giving the int parameter a name because we don't use it
    {
    }
};

class B
{
public:

    explicit B(int )
    {
    }
};

void func_a(A )
{
}

void func_b(B )
{
}

int main()
{
    func_a(A(100)); // works
    func_a(200); // works!
    func_b(B(100)); // works
    func_b(200); // doesn't work!
}


http://ideone.com/Irrxxh

So in your code below there is no reason to use explicit.

1
2
3
4
	explicit Queue(){
		list=0;
		QueueSize=0;
	}
Last edited on
Topic archived. No new replies allowed.