+ 1
Queue Management 3
Your Queue class is up and working in a customer service company. The company opens up a new branch and asks you to make another version of the Queue for them. The only difference is the way the Queue is displayed: each number on a new line. You decide to create a new class called Queue2, which is derived from the Queue class and overrides the print() method, outputting each element of the queue on a new line.
27 Antworten
+ 25
#include <iostream>
using namespace std;
class Queue {
public:
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
{
public:
Queue2 () {
};
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<"\n";
}
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;
}
+ 12
class Queue2 : public Queue
{
int size;
int* queue;
public:
Queue2() {
size = 6;
queue = new int[100]{3,66,128,5,111,77890};
}
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<< endl ;
}
}
};
+ 5
class Queue {
protected:
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;
}
};
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;
}
};
+ 1
#include <iostream>
using namespace std;
class Queue {
public:
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
{
public:
Queue2 () {
};
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<"\n";
}
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;
}
Good Luck
+ 1
my solution:
#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{
protected:
int size;
int* queue;
public:
Queue2 () {
size = 0;
queue = new int[100];
}
void add(int data) {
queue[size] = data;
size++;
}
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;
}
+ 1
class Queue2 : public Queue
{
int size;
int* queue;
public:
Queue2() {
size = 6;
queue = new int[100]{3,66,128,5,111,77890};
}
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<< endl ;
}
}
};
0
0
Simplest code ever
#include <iostream>
using namespace std;
class Queue {
public:
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
{
public:
Queue2 () {
};
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<"\n";
}
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;
}
0
0
#include <iostream>
using namespace std;
class Queue {
public:
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;
}
};
class Queue2: public Queue
{
public:
Queue2 () {
};
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<"\n";
}
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;
}
0
you can try
virtual void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<< endl;
}
cout << endl;
in an inherited Class of Queu2 From public class Queue
you can also listen to YouTube Video : Queue Management , Inheritance , overriding ,Polymorphism in C plus plus C++
https://youtu.be/dxRSYV3VqLE
0
J
0
#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;
}
0
class Queue2 : public Queue
{
int size;
int* queue;
public:
Queue2() {
size = 6;
queue = new int[100]{3,66,128,5,111,77890};
}
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<< endl ;
}
}
};
0
//Make the class Queue to public or protected
class Queue {
protected:
int size;
int* queue;
//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;
}
};
0
//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;
}
};
0
#include <iostream>
using namespace std;
class Queue {
public:
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
{
public:
Queue2 () {
};
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<"\n";
}
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;
}
23rd Feb 2021, 4:37 PM
0
#include <iostream>
using namespace std;
class Queue {
public:
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
{
public:
Queue2 () {
};
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<"\n";
}
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;
}
0
#include <iostream>
using namespace std;
class Queue {
public:
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
{
public:
Queue2 () {
};
void print() {
if (size == 0) {
cout << "Queue is empty"<<endl;
return;
}
for (int i = 0; i < size; i++) {
cout<<queue[i]<<"\n";
}
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;
}