Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

intQueue.h // Specification file for the IntQueue class #ifndef INTQUEUE_H #defi

ID: 3706013 • Letter: I

Question

intQueue.h

// Specification file for the IntQueue class
#ifndef INTQUEUE_H
#define INTQUEUE_H

class IntQueue
{
private:
int *queueArray; // Points to the queue array
int queueSize; // The queue size
int front; // Subscript of the queue front
int rear; // Subscript of the queue rear
int numItems; // Number of items in the queue
bool isFull();
public:
// Constructor
IntQueue(int);

// Copy constructor
IntQueue(const IntQueue &);

// Destructor
~IntQueue();

// Queue operations
void enqueue(int);
void dequeue(int &);
bool isEmpty();
void clear();
};

#endif

intQueue.cpp

// Implementation file for the IntQueue class
#include <iostream>
#include "IntQueue.h"
using namespace std;

//***************************************************************
// This constructor creates an empty queue of a specified size. *
//***************************************************************

IntQueue::IntQueue(int s)
{
queueArray = new int[s];
queueSize = s;
front = -1;
rear = -1;
numItems = 0;
}

//***************************************************************
// Copy constructor *
//***************************************************************

IntQueue::IntQueue(const IntQueue &obj)
{
// Allocate the queue array.
queueArray = new int[obj.queueSize];

// Copy the other object's attributes.
queueSize = obj.queueSize;
front = obj.front;
rear = obj.rear;
numItems = obj.numItems;

// Copy the other object's queue array.
for (int count = 0; count < obj.queueSize; count++)
queueArray[count] = obj.queueArray[count];
}

//***************************************************************
// Destructor *
//***************************************************************

IntQueue::~IntQueue()
{
delete [] queueArray;
}

//***************************************************************
// Function enqueue inserts a value at the rear of the queue. *
//***************************************************************

void IntQueue::enqueue(int num)
{ try{
if (isFull())
throw exception();

// Calculate the new rear position
rear = (rear + 1) % queueSize;
// Insert new item
queueArray[rear] = num;
// Update item count
numItems++;
}

catch (exception &) {
cout << "Queue is overflow and the values are: " << endl;
}
}

//***************************************************************
// Function dequeue removes the value at the front of the queue *
// and copies t into num. *
//***************************************************************

void IntQueue::dequeue(int &num)
{ try{
if (isEmpty())
throw exception();

// Move front
front = (front + 1) % queueSize;
// Retrieve the front item
num = queueArray[front];
// Update item count
numItems--;
}

catch (exception &){
cout << "Queue is underflow" << endl;
}
}

//***************************************************************
// isEmpty returns true if the queue is empty, otherwise false. *
//***************************************************************

bool IntQueue::isEmpty()
{
bool status;

if (numItems)
status = false;
else
status = true;

return status;
}

//***************************************************************
// isFull returns true if the queue is full, otherwise false. *
//***************************************************************

bool IntQueue::isFull()
{
bool status;

if (numItems < queueSize)
status = false;
else
status = true;

return status;
}

//*****************************************************************
// clear sets the front and rear indices, and sets numItems to 0. *
//*****************************************************************

void IntQueue::clear()
{
front = queueSize - 1;
rear = queueSize - 1;
numItems = 0;
}

C++

Why numItems shouldn't be public?

quickly please I just have 30 minutes to the due.

Explanation / Answer

Encapsulation is the one of the popular feature of the Object oriented concept of C++. In this we can have data and methods combined together in single unit called class. Data items are accessed through member functions of the class. Here outside world or application does not need to know how many items are there in queue. Application only needs to know the enqueue , dequeue operation. Hence we are protecing the data members from being accessed outside class.