In C Write the definitions of the functions to overload the

In C++,

Write the definitions of the functions to overload the assignment operator and copy constructor for the class queueType. Also, write a program to test these operations.

The main function would prompt the user with the following menu options:

1. enqueue

2. dequeue

3. print queue

4. debug queue

5. quit

Solution

#include<iostream>
#include<cstdlib>
#define MAX 50
using namespace std;
class Queue{
private:
int item[MAX];
int rear;
int front;
public:
//copy constructor
Queue( Queue &queue)
{
    rear=queue.rear;
    front=queue.front;
    for(int i=front; i<=rear; i++)
item[i]=queue.item[i];
}

Queue(){
rear = -1;
front = 0;
}
void enqueue(int data){
item[++rear] = data;
}
int dequeue(){
return item[front++];
}
void printQueue(){
if(!this->isEmpty()){
for(int i=front; i<=rear; i++)
cout<<item[i]<<endl;
}else{
cout<<\"Queue Underflow\"<<endl;
}
}
int size(){
return (rear - front + 1);
}
bool isEmpty(){
if(front>rear){
return true;
}else{
return false;
}
}
bool isFull(){
if(this->size()>=MAX){
return true;
}else{
return false;
}
}
void operator = (Queue queue)
       {
       rear=queue.rear;
        front=queue.front;
        for(int i=front; i<=rear; i++)
item[i]=queue.item[i];
       }
};

int main(){
Queue queue;
int choice, data;
while(1){
cout<<\"\ 1. Enqueue\ 2. Dequeue\ 3. Print Queue \ 4. Quit\";
cout<<\"\ Enter your choice: \";
cin>>choice;
switch(choice){
case 1:
if(!queue.isFull()){
cout<<\"\ Enter data: \";
cin>>data;
queue.enqueue(data);
}else{
cout<<\"Queue is Full\"<<endl;
}
break;
case 2:
if(!queue.isEmpty()){
cout<<\"The data dequeued is :\"<<queue.dequeue();
}else{
cout<<\"Queue is Emtpy\"<<endl;
}
break;
case 3:
queue.printQueue();
break;
case 4:
exit(0);
break;
}
}
return 0;
}

In C++, Write the definitions of the functions to overload the assignment operator and copy constructor for the class queueType. Also, write a program to test t
In C++, Write the definitions of the functions to overload the assignment operator and copy constructor for the class queueType. Also, write a program to test t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site