Question

Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...

Data Structures using C++

Consider the classes QueueADT and ArrayQueueType:

QueueADT:

#ifndef QUEUEADT_H

#define QUEUEADT_H

template <class ItemType>

class QueueADT

{

public:

       // Action responsibilities

       virtual void resetQueue() = 0;

          // Reset the queue to an empty queue.

          // Post: Queue is empty.

       virtual void add(const ItemType& newItem) = 0;

          // Function to add newItem to the queue.

          // Pre: The queue exists and is not full.

          // Post: The queue is changed and newItem is added to the

          //       back of the queue.

       virtual void remove() = 0;

          // Function to remove the first element of the queue.

          // Pre: The stack exists and is not empty.

          // Post: The queue is changed and the first (i.e., front) item is

          //       removed from the queue.

       // Knowledge responsibilities

       virtual bool isEmpty() const = 0;

          // Function to determine whether the queue is empty.

          // Post: Returns true if queue is empty; false otherwise.

       virtual bool isFull() const = 0;

          // Function to determin whether the queue is full.

          // Post: Returns true if there is room for another item;

          //       false otherwise.

       virtual ItemType front() const = 0;

          // Function to return the first element of the queue.

        // Pre: The queue exists and is not empty.

          // Post: If the queue is empty, the program terminates; otherwise,

          //       the first element of the queue is returned.

      virtual ItemType back() const = 0;

          // Function to return the last element of the queue.

        // Pre: The queue exists and is not empty.

          // Post: If the queue is empty, the program terminates; otherwise,

          //       the last element of the queue is returned.

};

ArrayQueueType:

#ifndef ARRAYQUEUETYPE_H

#define ARRAYQUEUETYPE_H

#include <iostream>

#include <new>        // for bad_alloc exception

#include <cstdlib>    // for the exit function

#include "QueueADT.h"

using namespace std;

const int MAX_QUEUE_SIZE = 100; // Maximum number of components

template <class ItemType>

class ArrayQueueType: public QueueADT<ItemType>

{

public:

       // Constructor

       ArrayQueueType();

          // Default constructor.

          // Post: An empty queue has been created. The variable list holds

          //       the base address of the array.

       ArrayQueueType(const ArrayQueueType<ItemType>& otherQueue);

          // Copy constructor for deep copy of the array-based queues.

       // Destructor

       ~ArrayQueueType();

          // Delete all the queue items.

          // Post: The array (list) holding the queue elements is deleted.

       // Action responsibilities

       void resetQueue();

          // Reset the queue to an empty queue.

          // Post: Queue is empty.

       void add(const ItemType& newItem);

          // Function to add newItem to the queue.

          // Pre: The queue exists and is not full.

          // Post: The queue is changed and newItem is added to the

          //       back of the queue.

       void remove();

          // Function to remove the first element of the queue.

          // Pre: The stack exists and is not empty.

          // Post: The queue is changed and the first (i.e., front) item is

          //       removed from the queue.

       // Knowledge responsibilities

       bool isEmpty() const;

          // Post: Returns true if queue is empty; false otherwise.

       bool isFull() const;

          // Post: Returns true if there is room for another item;

          //       false otherwise.

       ItemType front() const;

          // Function to return the first element of the queue.

        // Pre: The queue exists and is not empty.

          // Post: If the queue is empty, the program terminates; otherwise,

          //       the first element of the queue is returned.

       ItemType back() const;

          // Function to return the last element of the queue.

          // Pre: The queue exists and is not empty.

          // Post: If the queue is empty, the program terminates; otherwise,

          //       the last element of the queue is returned.

       // Operator overloading

       const ArrayQueueType<ItemType>& operator=

                     (const ArrayQueueType<ItemType>& otherQueue);

          // Overload the assignment operator.

protected:

       int length; // variable to store the number of items in the queue

       int queueFront; // variable to store the index of the first item

                       // of the queue

       int queueBack; // variable to store the index of the last item

                       // of the queue

       ItemType *list; // pointer to the array that holds the queue items

private:

       void copyQueue(const ArrayQueueType<ItemType>& otherQueue);

          // Function to make a copy of otherQueue.

          // A deep copy of otherQueue is created and assigned to this queue.

};

//**************************************************************

template <class ItemType>

ArrayQueueType<ItemType>::ArrayQueueType()

{

       try

       {

              queueFront = 0;

              queueBack = MAX_QUEUE_SIZE - 1;

              length = 0;

              list = new ItemType[MAX_QUEUE_SIZE]; // create the array to

                                                   // hold the queue items

       }// end try

       catch (bad_alloc)

       {

              cout << "ERROR: Cannot allocate memory!\n";

              exit(EXIT_FAILURE);

       }

}

//**************************************************************

template <class ItemType>

ArrayQueueType<ItemType>::ArrayQueueType

                (const ArrayQueueType<ItemType>& otherQueue)

{

       try

       {

              list = new ItemType[MAX_QUEUE_SIZE];

       }// end try

       catch (bad_alloc)

       {

              cout << "ERROR: Cannot allocate memory!\n";

              exit(EXIT_FAILURE);

       }

       copyQueue(otherQueue);

}

//**************************************************************

template <class ItemType>

ArrayQueueType<ItemType>::~ArrayQueueType()

{

       delete [] list; // Deallocate the memory occupied by the array.

}

//**************************************************************

template <class ItemType>

void ArrayQueueType<ItemType>::resetQueue()

{

       queueFront = 0;

       queueBack = MAX_QUEUE_SIZE - 1;

       length = 0;

}

//**************************************************************

template <class ItemType>

void ArrayQueueType<ItemType>::add(const ItemType& newItem)

{

       if ( !isFull() )

       {

              queueBack = (queueBack + 1) % MAX_QUEUE_SIZE; // use the mod

                 // operator to advance queueBack because the array is circular

              list[queueBack] = newItem; // add newItem to the back

              length++;

       }

       else

              cout << "Cannot add to a full queue." << endl;

}

//**************************************************************

template <class ItemType>

void ArrayQueueType<ItemType>::remove()

{

       if ( !isEmpty() )

       {

              length--;

          queueFront = (queueFront + 1) % MAX_QUEUE_SIZE; // use the mod

                 // operator to advance queueFront because the array is circular

       }

       else

              cout << "Cannot remove from an empty queue." << endl;

}

//**************************************************************

template <class ItemType>

bool ArrayQueueType<ItemType>::isEmpty() const

{

       return ( length == 0 );

}

//**************************************************************

template <class ItemType>

bool ArrayQueueType<ItemType>::isFull() const

{

       return ( length == MAX_QUEUE_SIZE );

}

//**************************************************************

template <class ItemType>

ItemType ArrayQueueType<ItemType>::front() const

{

       if ( !isEmpty() )

              return list[queueFront]; // the first item is indexed

                                   // by queueFront

       else // if queue is empty, terminate the program

       {

              cout << "ERROR: Cannot return first item from an empty queue!\n";

              exit(EXIT_FAILURE);

       }

}

//**************************************************************

template <class ItemType>

ItemType ArrayQueueType<ItemType>::back() const

{

       if ( !isEmpty() )

              return list[queueBack]; // the last item is indexed

                                   // by queueBack

       else // if queue is empty, terminate the program

       {

              cout << "ERROR: Cannot return last item from an empty queue!\n";

              exit(EXIT_FAILURE);

       }

}

//**************************************************************

template <class ItemType>

const ArrayQueueType<ItemType>& ArrayQueueType<ItemType>::operator=

                    (const ArrayQueueType<ItemType>& otherQueue)

{

       if ( this != &otherQueue ) // avoid self-copying

              copyQueue(otherQueue);

       return *this;

}

//**************************************************************

template <class ItemType>

void ArrayQueueType<ItemType>::copyQueue

                    (const ArrayQueueType<ItemType>& otherQueue)

{

       length = otherQueue.length;

       queueFront = otherQueue.queueFront;

       queueBack = otherQueue.queueBack;

       for (int i = 0; i < length; i++)

              list[ (queueFront + i) % MAX_QUEUE_SIZE ]

              = otherQueue.list[ (queueFront + i) % MAX_QUEUE_SIZE ];

}

#endif

What is the effect of the following statements? If a statement is invalid, explain why it is invalid.

  (a)    QueueADT<int> newQueue;

(b) ArrayQueueType<double> sales;

(c) ArrayQueueType<string> names;

Homework Answers

Answer #1

(a) QueueADT<int> newQueue;
QueueADT is an abstract class. And an abstract class is to provide appropriate base
to the inherited classes. And it is not allowed to have any instances of type Abstract class.

(b) ArrayQueueType<double> sales;
Creates an object sales of type ArrayQueueType which takes a variables of type double.

(c) ArrayQueueType<string> names;
Creates an object names of type ArrayQueueType which takes a variables of type string.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
Given the following specifications for an array-based unsorted list, implement all of the functions (declared below)...
Given the following specifications for an array-based unsorted list, implement all of the functions (declared below) and a write a driver code to test all of your implementations. // Define a structure to use as the list item struct ListItem { int key; int Data; }; #define MAX_SIZE 50 // Define maximum length of the list class UnsortedArray { private: int head; // Index to head of the list ListItem theList[MAX_SIZE]; // The list public: UnsortedArray(); // Class constructor ~...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity  ...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity   Code   Description   Project   File   Line   Suppression State Warning   C6385   Reading invalid data from 'DynamicStack': the readable size is '(unsigned int)*28+4' bytes, but '56' bytes may be read.   Here is the C++ code were I'm having the warning. // Sstack.cpp #include "SStack.h" // Constructor SStack::SStack(int cap) : Capacity(cap), used(0) {    DynamicStack = new string[Capacity]; } // Copy Constructor SStack::SStack(const SStack& s) : Capacity(s.Capacity), used(s.used)...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
Data Structures using C++ Consider the definition the following function template: template <class Type> Type func(Type...
Data Structures using C++ Consider the definition the following function template: template <class Type> Type func(Type list[], int size) {        Type x = list[0];        Type y = list[size - 1];        for (int j = 1; j < size / 2; j++)        {               if (x < list[j])                      x = list[j];               if (y > list[size - 1 - j])                      y = list[size - 1 - j];        }        return x + y; }...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT