+ 2
Help me with "Queue Management part 3" solution in C++
16 Réponses
+ 19
#include <iostream>
using namespace std;
class Queue {
protected: int size;
protected: 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;
}
};
//your code goes here
class Queue2 : public Queue {
public:
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<endl;
}
cout << endl;
}
};
int main() {
Queue q1;
q1.add(42); q1.add(2); q1.add(8); q1.add(1);
q1.print();
Queue2 q2;
q2.add(3); q2.add(66); q2.add(128); q2.add(5);q2.add(111);q2.add(77890);
q2.print();
return 0;
}
+ 5
#include <iostream>
using namespace std;
class Queue {
protected: int size;
protected: 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;
}
};
class Queue2 : public Queue {
public:
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<endl;
}
cout << endl;
}
};
int main() {
Queue q1;
q1.add(42); q1.add(2); q1.add(8); q1.add(1);
q1.print();
Queue2 q2;
q2.add(3); q2.add(66); q2.add(128); q2.add(5);q2.add(111);q2.add(77890);
q2.print();
return 0;
}
+ 3
#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;
}
};
//your code goes here
class Queue2:public Queue{
int size;
int *queue;
public:
Queue2(){
size=0;
queue=new int[100];
}
void print(){
if(size==0){
cout<<"Queue is empty"<<endl;
return;
}
for(int i=0;i<size;i++){
cout<<queue[i]<<endl;
}
}
void add(int data){
queue[size]=data;
size++;
}
};
int main() {
Queue q1;
q1.add(42); q1.add(2); q1.add(8); q1.add(1);
q1.print();
Queue2 q2;
q2.add(3); q2.add(66); q2.add(128); q2.add(5);q2.add(111);q2.add(77890);
q2.print();
return 0;
}
+ 2
Do not forget to change the access specifier of the Queue members, as they won't be inherited if private.
+ 1
You forgot to change the access modifier for size and queue as noted. By default they're automatically private if not specified.
0
Please show your attempt here so that community can guide you. No one will provide you entire code. Also, you've posted same question twice se please delete it.
0
This is my attempt of the same. Try providing correction in the code.
0
Hi! You solved This part?
0
👍
0
Good job fixing your code Danush!
0
Thank you...😊
0
#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 &that) {
Queue result;
for (int i=0; i<this->size; i++) {
result.add(this->queue[i]);
}
for (int i=0; i<that.si
0
Please help me Queue Management part 1 in C++
0
short and good!
#include <iostream>
using namespace std;
class Queue
{protected: int size;
protected: 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;}};
class Queue2 : public Queue
{public:
void print()
{if (size == 0)
{cout << "Queue is empty"<<endl;
return;}
for (int i = 0; i < size; i++)
{cout<<queue[i]<<endl;}
cout << endl;}};
int main()
{Queue q1;
q1.add(42); q1.add(2); q1.add(8); q1.add(1);
q1.print();
Queue2 q2;
q2.add(3); q2.add(66); q2.add(128); q2.add(5);q2.add(111);q2.add(77890);
q2.print();
return 0;}