- 2
I need a idea for this program
It's time to update your Queue management system. The previous version supports only integer numbers and we need to support more types, such as strings, to store customer names in the queue. Transform the given Queue class to a class template, which can work with different data types.
22 ответов
+ 13
This code works
#include <iostream>
using namespace std;
template <class T>
class Queue {
int size;
T* queue;
public:
Queue() {
size = 0;
queue = new T[100];
}
void add(T data) {
queue[size] = data;
size++;
}
void remove() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
else {
for (int i = 0; i < size - 1; i++) {
queue[i] = queue[i + 1];
}
size--;
}
}
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<" <- ";
}
cout << endl;
}
Queue operator+(Queue &obj) {
Queue res;
for(int i=0;i<this->size;i++) {
res.add(this->queue[i]);
}
for(int i=0;i<obj.size;i++) {
res.add(obj.queue[i]);
}
return res;
}
};
int main() {
Queue<int> q1;
q1.add(42); q1.add(2); q1.add(8); q1.add(1);
q1.print();
Queue<string> q2;
q2.add("Dave"); q2.add("John"); q2.add("Amy");
q2.print();
return 0;
}
+ 6
#include <iostream>
using namespace std;
class Queue {
int size;
int* queue;
public:
Queue() {
size = 0;
queue = new int[100];
}
void add(int data) {
queue[size] = data;
size++;
}
void remove() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
else {
for (int i = 0; i < size - 1; i++) {
queue[i] = queue[i + 1];
}
size--;
}
}
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<" <- ";
}
cout << endl;
}
Queue operator+(Queue &obj) {
Queue res;
for(int i=0;i<this->size;i++) {
res.add(this->queue[i]);
}
for(int i=0;i<obj.size;i++) {
res.add(obj.queue[i]);
}
return res;
}
};
int main() {
Queue<int> q1;
q1.add(42); q1.add(2); q1.add(8); q1.add(1);
q1.print();
Queue<string> q2;
q2.add("Dave");
+ 2
the correct code is asfollow :
#include <iostream>
using namespace std;
template <class T>
class Queue {
int size;
T* queue;
public:
Queue() {
size = 0;
queue = new T[100];
}
void add(T data) {
queue[size] = data;
size++;
}
void remove() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
else {
for (int i = 0; i < size - 1; i++) {
queue[i] = queue[i + 1];
}
size--;
}
}
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<" <- ";
}
cout << endl;
}
Queue operator+(Queue &obj) {
Queue res;
for(int i=0;i<this->size;i++) {
res.add(this->queue[i]);
}
for(int i=0;i<obj.size;i++) {
res.add(obj.queue[i]);
}
return res;
}
};
int main() {
Queue<int> q1;
q1.add(42); q1.add(2); q1.add(8); q1.add(1);
q1.print();
Queue<string> q2;
q2.add("Dave"); q2.add("John"); q2.add("Amy");
q2.print();
return 0;
}
+ 1
#include <iostream>
using namespace std;
template <class T>
class Queue {
int size;
T* queue;
public:
Queue() {
size = 0;
queue = new T[100];
}
void add(T data) {
queue[size] = data;
size++;
}
void remove() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
else {
for (int i = 0; i < size - 1; i++) {
queue[i] = queue[i + 1];
}
size--;
}
}
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<" <- ";
}
cout << endl;
}
Queue operator+(Queue &obj) {
Queue res;
for(int i=0;i<this->size;i++) {
res.add(this->queue[i]);
}
for(int i=0;i<obj.size;i++) {
res.add(obj.queue[i]);
}
return res;
}
};
int main() {
Queue<int> q1;
q1.add(42); q1.add(2); q1.add(8); q1.add(1);
q1.print();
Queue<string> q2;
q2.add("Dave"); q2.add("John"); q2.add("Amy");
q2.print();
return 0;
}
Good Luck
0
Is this working sir
0
Yes sir absolutely your Right
0
This program is not working...
0
Isn't working
0
Because a class name is not a template
0
#include <iostream>
using namespace std;
//change the class to a template
template <class T>
class Queue
{
private:
T *arr;
int count;
public:
Queue(int size) {
arr = new T[size];
count = 0;
}
void add(T elem) {
arr[count] = elem;
count++;
}
void get(int index) {
cout << arr[index]<<endl;
}
};
int main()
{
Queue<string> q(4);
q.add("James");
q.add("Andy");
q.add("Amy");
q.get(2);
Queue <int> n(2);
n.add(42);
n.add(33);
n.get(1);
return 0;
This code works for me
0
It not working
0
0
this code not working for me sir
0
#include <iostream>
using namespace std;
template<class T>
class Queue {
int size;
T* queue;
public:
Queue() {
size = 0;
queue = new T[100];
}
void add(T data) {
queue[size] = data;
size++;
}
void remove() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
else {
for (int i = 0; i < size - 1; i++) {
queue[i] = queue[i + 1];
}
size--;
}
}
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<" <- ";
}
cout << endl;
}
Queue operator+(Queue &obj) {
Queue res;
for(int i=0;i<this->size;i++) {
res.add(this->queue[i]);
}
for(int i=0;i<obj.size;i++) {
res.add(obj.queue[i]);
}
return res;
}
};
int main() {
Queue<int>q1;
q1.add(42); q1.add(2); q1.add(8); q1.add(1);
q1.print();
Queue<string>q2;
q2.add("Da
0
It's correct code
0
#include <iostream>
using namespace std;
template <class T>
class Queue {
int size;
T* queue;
public:
Queue() {
size = 0;
queue = new T[100];
}
void add(T data) {
queue[size] = data;
size++;
}
void remove() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
else {
for (int i = 0; i < size - 1; i++) {
queue[i] = queue[i + 1];
}
size--;
}
}
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<" <- ";
}
cout << endl;
}
Queue operator+(Queue &obj) {
Queue res;
for(int i=0;i<this->size;i++) {
res.add(this->queue[i]);
}
for(int i=0;i<obj.size;i++) {
res.add(obj.queue[i]);
}
return res;
}
};
int main() {
Queue<int> q1;
q1.add(42); q1.add(2); q1.add(8); q1.add(1);
q1.print();
Queue<string> q2;
q2.add("Dave"); q2.add("John"); q2.add("Amy");
q2.print();
return 0;
}
this is the correct code worked for me
0
#include <iostream>
using namespace std;
template <class T>
class Queue {
int size;
T* queue;
public:
Queue() {
size = 0;
queue = new T[100];
}
void add(T data) {
queue[size] = data;
size++;
}
void remove() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
else {
for (int i = 0; i < size - 1; i++) {
queue[i] = queue[i + 1];
}
size--;
}
}
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<" <- ";
}
cout << endl;
}
Queue operator+(Queue &obj) {
Queue res;
for(int i=0;i<this->size;i++) {
res.add(this->queue[i]);
}
for(int i=0;i<obj.size;i++) {
res.add(obj.queue[i]);
}
return res;
}
};
int main() {
Queue<int> q1;
q1.add(42); q1.add(2); q1.add(8); q1.add(1);
q1.print();
Queue<string> q2;
q2.add("Dave"); q2.add("John"); q2.add("Amy");
q2.print();
return 0;
}
0
Let me know guys if it works well in your program
#include <iostream>
using namespace std;
template <class T>
class Queue {
int size;
T* queue;
public:
Queue() {
size = 0;
queue = new T[100];
}
void add(T data) {
queue[size] = data;
size++;
}
void remove() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
else {
for (int i = 0; i < size - 1; i++) {
queue[i] = queue[i + 1];
}
size--;
}
}
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<" <- ";
}
cout << endl;
}
Queue operator+(Queue &obj) {
Queue res;
for(int i=0;i<this->size;i++) {
res.add(this->queue[i]);
}
for(int i=0;i<obj.size;i++) {
res.add(obj.queue[i]);
}
return res;
}
};
int main() {
Queue<int> q1;
q1.add(42); q1.add(2); q1.add(8); q1.add(1);
q1.print();
Queue<string> q2;
q2.add("Dave"); q2.add("John"); q2.add("Amy");
q2.print();
return 0;
}