Question: HW11: Project OOP Inheritance and Polymorphism Part 2 Create a queueclass by priviate inherting the unorderedArrayListTypeclass. A queue class is a First-In-First-Out data structure. To

HW11: Project OOP Inheritance and Polymorphism

Part 2

Create a queueclass by priviate inherting the unorderedArrayListTypeclass. A queue class is a First-In-First-Out data structure. To understand a queue, think of a cafeteria line: the person at the front is served first (removed), and people are added to the line at the back.

class queue : privateunorderedArrayListType

{

public:

bool isEmpty() const; // test whether queue is empty

// Post: returns true if queue is empty, otherwise returns false

int size() const; // return size

// Post: returns the number of elements in the queue

int front() const; // returns the element at the front of the queue. This should be the "oldest" element, the same element that will be removed if deque() is called next

// Pre: the queue is not empty

// Post: returns the element at the front of the queue

int back() const; // returns the element at the back of the queue. This should be the "youngest" element, the same element that was added into the queue most recently using enque()

// Pre: the queue is not empty

// Post: returns the element at the back of the queue

void enque(intnewItem); // insert one element at the back of the queue, after its current last element (the "youngest" element before this enque)

// Post: newItem added at the end of the queue, after the current last element

int deque(); // remove one element from the front of the queue. The "oldest" element should be removed

// Pre: the queue is not empty

// Post: the item at the front of the queue is removed from the queue and returned

queue(int size = 100);

// Post: queue initialized with capacity being size and contains 0 elements.

// if no size is specified, 100 is used for the capacity

};

Overload the output operator <

Add appropriate code into mainto test your queue class (you may delete the existing code in main at this point). Be sure to test each function and print out current content of a queue object.

Additional requirements:

You should not directly access the int* listdata member of the arrayListTypeclass in your implementation (0 credits if you did). Instead, you should call existing functions provided in the .cpp file.

You should not modify the given arrayListType/ unorderedArrayListTypecode (0 credits if you did), except for deleting the given maincode and breaking the code into header + implementation (see next requirement).

Code that needs to be worked is included below:

#include

//--------------

// arrayListType (super)

//--------------

classarrayListType

{

friendstd::ostream& operator<<(std::ostream&, constarrayListType&);

public:

boolisEmpty()const;

boolisFull()const;

intlistSize()const;

intmaxListSize()const;

boolisItemAtEqual(intlocation, intitem) const;

voidremoveAt(intlocation);

voidretrieveAt(intlocation, int& retItem) const;

voidclearList();

virtualintseqSearch(intsearchItem) const= 0;

virtualvoidinsertAt(intlocation, intinsertItem) = 0;

virtualvoidinsertEnd(intinsertItem) = 0;

virtualvoidreplaceAt(intlocation, intrepItem) = 0;

virtualvoidremove(intremoveItem) = 0;

arrayListType(intsize = 100);

arrayListType(constarrayListType& otherList);

virtual~arrayListType();

protected:

int*list; //array to hold the list elements

intlength; //length of the list

intmaxSize; //the maximum size of the list

};

//--------------

// unorderedArrayListType (sub)

//--------------

classunorderedArrayListType: publicarrayListType

{

public:

voidinsertAt(intlocation, intinsertItem);

voidinsertEnd(intinsertItem);

voidreplaceAt(intlocation, intrepItem);

intseqSearch(intsearchItem) const;

voidremove(intremoveItem);

unorderedArrayListType(intsize = 100); //Constructor

};

//--------------

// queue (grand-sub)

//--------------

classqueue: privateunorderedArrayListType

{

public:

boolisEmpty()const; // test whether queue is empty

// Post: returns true if queue is empty, otherwise returns false

intsize()const; // return size

// Post: returns the number of elements in the queue

intfront()const; // returns the element at the front of the queue. This should be the "oldest" element, the same element that will be removed if deque() is called next

// Pre: the queue is not empty

// Post: returns the element at the front of the queue

intback()const; // returns the element at the back of the queue. This should be the "youngest" element, the same element that was added into the queue most recently using enque()

// Pre: the queue is not empty

// Post: returns the element at the back of the queue

voidenque(intnewItem); // insert one element at the back of the queue, after its current last element (the "youngest" element before this enque)

// Post: newItem added at the end of the queue, after the current last element

intdeque();// remove one element from the front of the queue. The "oldest" element should be removed

// Pre: the queue is not empty

// Post: the item at the front of the queue is removed from the queue and returned

queue(intsize = 100);

// Post: queue initialized with capacity being size paramand contains 0 elements.

// if no size is specified, 100 is used for the capacity

};

//--------------

// client code

//--------------

intmain()

{

unorderedArrayListTypeintList(25);

for(inti = 0; i < 8; i++)

intList.insertEnd(i * 10 + 5);

std::cout << "intList: "<< intList << std::endl;

//Create tempand initialize it using intList

unorderedArrayListTypetemp(intList);

std::cout << "temp: "<< temp << std::endl;

//Replace the first element of temp.

temp.replaceAt(0, -75);

std::cout << "tempfirst element replaced: "<< temp << std::endl;

std::cout << "intList: "<< intList << std::endl;

return0;

} // end main

//---------------------

// Implementation

//---------------------

boolarrayListType::isEmpty()const

{

return(length== 0);

} //end isEmpty

boolarrayListType::isFull()const

{

return(length== maxSize);

} //end isFull

intarrayListType::listSize()const

{

returnlength;

} //end listSize

intarrayListType::maxListSize()const

{

returnmaxSize;

} //end maxListSize

boolarrayListType::isItemAtEqual(intlocation, intitem) const

{

if(location < 0 || location >= length)

{

std::cout << "The location of the item to be removed "

<< "is out of range. ";

returnfalse;

}

else

return(list[location] == item);

} //end isItemAtEqual

voidarrayListType::removeAt(intlocation)

{

if(location < 0 || location >= length)

std::cout << "The location of the item to be removed "

<< "is out of range. ";

else

{

for(inti = location; i < length- 1; i++)

list[i] = list[i + 1];

length--;

}

} //end removeAt

voidarrayListType::retrieveAt(intlocation, int& retItem) const

{

if(location < 0 || location >= length)

std::cout << "The location of the item to be retrieved is "

<< "out of range ";

else

retItem = list[location];

} //end retrieveAt

voidarrayListType::clearList()

{

length= 0;

} //end clearList

arrayListType::arrayListType(intsize)

{

if(size <= 0)

{

std::cout << "The array size must be positive. Creating "

<< "an array of the size 100. ";

maxSize= 100;

}

else

maxSize= size;

length= 0;

list= newint[maxSize];

//std::cout<< "constructor of alt: " << this << " "; // testing

} //end constructor

arrayListType::~arrayListType()

{

delete[] list;

//std::cout<< "destructorof alt: " << this << " "; // testing

} //enddestructor

arrayListType::arrayListType(constarrayListType& otherList)

{

maxSize= otherList.maxSize;

length= otherList.length;

list= newint[maxSize]; //create the array

for(intj = 0; j < length; j++) //copy otherList

list[j] = otherList.list[j];

//std::cout<< "copy constructor of alt: " << this << " "; // testing

}//end copy constructor

//--------------------

// non-member, friend

//--------------------

std::ostream& operator<<(std::ostream& out, constarrayListType& obj)

{

for(inti = 0; i < obj.length; i++)

out << obj.list[i] << " ";

returnout;

} //end operator<<

//---------------------

//---------------------

voidunorderedArrayListType::insertAt(intlocation,

intinsertItem)

{

if(location < 0 || location >= maxSize)

std::cout << "The position of the item to be inserted "

<< "is out of range. ";

elseif(length>= maxSize) //list is full

std::cout << "Cannot insert in a full list ";

else

{

for(inti = length; i > location; i--)

list[i] = list[i - 1]; //move the elements down

list[location] = insertItem; //insert the item at

//the specified position

length++; //increment the length

}

} //end insertAt

voidunorderedArrayListType::insertEnd(intinsertItem)

{

if(length>= maxSize) //the list is full

std::cout << "Cannot insert in a full list. ";

else

{

list[length] = insertItem; //insert the item at the end

length++; //increment the length

}

} //end insertEnd

intunorderedArrayListType::seqSearch(intsearchItem) const

{

for(intloc = 0; loc < length; loc++)

if(list[loc] == searchItem)

returnloc;

return-1; // not found

} //end seqSearch

voidunorderedArrayListType::remove(intremoveItem)

{

if(length== 0)

std::cout << "Cannot delete from an empty list. ";

else

{

intloc = seqSearch(removeItem);

if(loc != -1)

removeAt(loc);

else

std::cout << "The item to be deleted is not in the list .";

}

} //end remove

voidunorderedArrayListType::replaceAt(intlocation, intrepItem)

{

if(location < 0 || location >= length)

std::cout << "The location of the item to be "

<< "replaced is out of range. ";

else

list[location] = repItem;

} //end replaceAt

unorderedArrayListType::unorderedArrayListType(intsize)

: arrayListType(size)

{

//std::cout<< "constructor of unorderedalt: " << this << " "; // testing

} //end constructor

//---------------------

//---------------------

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!