Templates and classes

Sorry if the title is misleading, as wasnt sure how to correctly name the question I have.

Im working on a 2 part assignment, where first you need to create a QUEUE template that imitates a queue (FIFO) with enqueue and dequeue. I've done this ( I believe)
And the 2nd part - creating a Human class with any data that is declared withing queue template (i.e. Queue <Human> HumanQueue; ) - This is the part Im having some trouble with. I understand the premise of it, but I cant wrap my head around how to go about implementing this.

Heres the code I have at the moment for the queue template.

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
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <cstdlib>
using namespace std;

// Default queue size
#define SIZE 10

// Queue class
template <class X>
class queue
{
	X *arr; 	// array where to hold the elements
	int capacity;   // queue max size
	int front;  	// first element in array
	int rear;   	// last element in array
	int count;  	// queue size

public:
	queue(int size = SIZE);		

	void dequeue();
	void enqueue(X x);
	X peek();
	int size();
	bool isEmpty();
	bool isFull();
};

// Constructor
template <class X>
queue<X>::queue(int size)
{
	arr = new X[size];
	capacity = size;
	front = 0;
	rear = -1;
	count = 0;
}

// Removing first element from the queue
template <class X>
void queue<X>::dequeue()
{
		if (isEmpty())
	{
		cout << "Queue is empty";
	}

	cout << "Removing " << arr[front] << '\n';

	front = (front + 1) % capacity;
	count--;
}

// Adding to the queue
template <class X>
void queue<X>::enqueue(X item)
{
	if (isFull())
	{
		cout << "Queue is full";
	}

	cout << "Adding " << item << '\n';

	rear = (rear + 1) % capacity;
	arr[rear] = item;
	count++;
}

// returns queues first element
template <class X>
X queue<X>::peek()
{
	return arr[front];
}

// returns queue size
template <class X>
int queue<X>::size()
{
	return count;
}

// Checks if empty
template <class X>
bool queue<X>::isEmpty()
{
	return (size() == 0);
}

// Checks if full
template <class X>
bool queue<X>::isFull()
{
	return (size() == capacity);
}



int main()
{
	// Creates a queue with size 4
	queue<string> q(4);

	q.enqueue("a");
	q.enqueue("b");
	q.enqueue("c");

	cout << "First element in queue is: " << q.peek() << endl;
	q.dequeue();

	q.enqueue("d");

    cout << "First element now is: " <<q.peek() <<endl;

	cout << "Queue size is " << q.size() << endl;

	q.dequeue();
	q.dequeue();
	q.dequeue();

	if (q.isEmpty())
		cout << "Queue is empty\n";
	else
		cout << "Queue is not empty\n";

	return 0;
}


Essentially Im looking for some pointers on how to create a class Human (with data such as Name, surname and age) and then using the queue template to imitate a queue where each Human is placed in it.

Thank you for any help on this
how to create a class Human (with data such as Name, surname and age)
1
2
3
4
5
6
class Human
{
  string name;
  string surname;
  int age;
};


then using the queue template to imitate a queue where each Human is placed in it.
 
queue<Human> humanQueue;
Last edited on
I understand that part, its more about how do I then ''queue' a person with a name, surname, and age in main.cpp?
You create a Human object. You set the name, surname and age in that object. Then you use the queue class function enqueue.
1
2
3
4
5
6
struct Human
{
  string name;
  string surname;
  int age;
};



1
2
3
4
5
6
7
8
Human a_human; // Create a human
a_human. name = "Mike";  // set the values in the human
a_human. surname= "Takahashi";
a_human. age= "40";

queue<Human> humanQueue; // Make a queue

humanQueue.enqueue(a_human); // Put that human in the queue. 


Topic archived. No new replies allowed.