Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. As mentioned in the starting template I have given you a starting class de_nition, some class constructors and the destructor, and the copy operator=.

1. As mentioned in the starting template I have given you a starting class

de_nition, some class constructors and the destructor, and the copy

operator=. You _rst need to write two simple getter methods in order

to access the size and allocSize class member values. These should be

called getSize() and getAllocSize() respectively. These functions

should be class const functions (you guarantee that calling them will

not cause the class to be modi_ed. These functions take no parameters

as input. They both return an int value, because the size and allocSize

member parameters are both integer values.

2. Write a function called tostring(). This function will be a const class

function (the class is not modi_ed when it is called). This function

takes no parameters as input, and it returns a string. We use this

function in our testing, so you need to get it correct, but you have

implemented versions of this type of function in previous assignments.

The function should only create a string of the items currently in the

list. So it will return a string like "[3, 5, 4, 2]" if those are the 4 items

currently in the list. See the test code for speci_cs.

3. Overload the operator<<() to provide the ability for the ListType

class to be output to a stream. This will be a friend function, and again

it will be pretty similar to several examples we have seen of overloading

the output stream operator. You should use the tostring() method

in this function, but it outputs additional information, such as the id,

size and allocSize of the list to the output stream.

4. Create a function named appendItem(). This function takes an int

value as its only parameter, and it does not return a value. The indi-

cated integer value should be appended to the end of your list when

this function is called. You need to correctly handle causing the size of

your memory allocation to grow if needed in this function, if your list

is currently using all of the allocated memory. Once this is working,

overload the operator&() operator. We will de_ne the & operator to

mean list appending. For example, if you do list & 5 it will cause 5

to be appended to the end of the list (assuming list is a variable of List-

Type). This function will simply call appendItem() to do the work,

the only di_culty is getting the syntax correct to declare you are over-

loading the operator. This is not a friend function, like operator<<().

Read our textbook about binary operators to see examples of how to

overload a binary operator like this as a member function of a class.

5. Create a function name prependItem(). This works the same as the

append, but it prepends the indicated integer parameter to the front

of the list instead of to the end. However, you still need to check and

grow your allocated memory before prepending if your list is currently

full. Also prepend is a bit more complicated, because since we are

implementing an array based list, you need to shift all of the current

items 1 index up in your items before you can prepend to the beginning

of the list. We will also overload the operator|() for our class to

represent prepending an item. Thus if you do list | 5 this will cause

5 to be prepended to the beginning of the list.

6. Overload the operator+() to implement concatenation of two lists.

This operator is probably the trickiest I have given you to implement.

This operator should take a const reference to another ListType as

its parameter for input. This is the list on the right hand side of

the + operation. This function should return a reference to a new

ListType as its result. It is important that both the input parameter

and the return type be both reference parameters for this function.

This function should be a const function, as it does not cause the

original list to change. Instead you should dynamically allocate a new

ListType in this function, _ll it with the items from the two lists being

concatenated, and then return it as the result from this overloaded

function. You should read our textbook example of overloading the

operator+() and try and follow that pattern for implementing this

function.

7. Overload the operator[] indexing operator. This is NOT a const

member function, your list can change as a result of calling this func-

tion. This function takes an int value as its input parameter. This

function should return an int& reference. Again it is very important

that this overloaded operator return a reference. If this operator cor-

rectly returns an int&, it can actually be used as a setter to set/change

the values in the list. This operator works to index your ListType like

an array. You should perform bounds checking in this function. If the

given input index is not valid (it is bigger than the end of your list, or

it is < 0), you should display an error message and simply exit.

If you get all of these 7 steps and member functions mostly working, you

should save/submit your work at that point. However, I am also o_ering

the opportunity to earn 10 bonus points on this assignment, which may be

helpful for many of you to make up for some previous program grades. As

demonstrated in our video for this week, it is usually better if you want

to create a class template to start from a non-template working version of

the class. As I showed in the video this week, I usually templatize each

member function 1 at a time, starting with the class de_nition and the class

constructors. I will give up to 10 bonus points for a partial or complete

templatized ListType that supports appending, prepending, indexing, and

output to streams using the overloaded operators, but for any type, not just

the int type.

*******************************************

Main. cpp

*******************************************

#include

#include

#include

#include "ListType.hpp"

using namespace std;

/** main

* The main entry point for this program. Execution of this program

* will begin with this main function.

*

* @param argc The command line argument count which is the number of

* command line arguments provided by user when they started

* the program.

* @param argv The command line arguments, an array of character

* arrays.

*

* @returns An int value indicating program exit status. Usually 0

* is returned to indicate normal exit and a non-zero value

* is returned to indicate an error condition.

*/

int main(int argc, char** argv)

{

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

// test constructors and getters

cout << "--------- Test constructors and getters -------------------------" << endl;

ListType l1;

cout << "l1 size: " << l1.getSize()

<< " allocSize: " << l1.getAllocSize() << endl;

assert(l1.getSize() == 0);

assert(l1.getAllocSize() == 0);

ListType l2(7); // empty list but with room for 7 items

cout << "l2 size: " << l2.getSize()

<< " allocSize: " << l2.getAllocSize() << endl;

assert(l2.getSize() == 0);

assert(l2.getAllocSize() == 7);

int size = 5;

int items[] = {3, 9, 2, 7, 5};

ListType l3(size, items);

cout << "l3 size: " << l3.getSize()

<< " allocSize: " << l3.getAllocSize() << endl;

assert(l3.getSize() == 5);

assert(l3.getAllocSize() == 5);

cout << endl << endl;

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

// test output stream operator implementation

cout << "--------- Test output stream operator ---------------------------" << endl;

cout << "l2 items: " << l2.tostring() << endl;

assert(l2.tostring() == "[]");

cout << l2 << endl << endl;

cout << "l3 items: " << l3.tostring() << endl;

assert(l3.tostring() == "[3, 9, 2, 7, 5]");

cout << l3 << endl << endl;

cout << endl << endl;

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

// test appending and operator& overloading

cout << "--------- Test append and operator& -----------------------------" << endl;

// append to empty list

l1.appendItem(1);

cout << "append to empty l1: " << endl << l1 << endl;

assert(l1.tostring() == "[1]" );

assert(l1.getSize() == 1);

assert(l1.getAllocSize() == 10);

// append to non empty list

l3.appendItem(12);

cout << "append to nonempty l3: " << endl << l3 << endl;

assert(l3.tostring() == "[3, 9, 2, 7, 5, 12]" );

assert(l3.getSize() == 6);

assert(l3.getAllocSize() == 15);

// append 2 items using operator& and test

l3 & 6;

l3 & 11;

cout << "operator& test l3: " << endl << l3 << endl;

assert(l3.tostring() == "[3, 9, 2, 7, 5, 12, 6, 11]" );

assert(l3.getSize() == 8);

assert(l3.getAllocSize() == 15);

// some more, mix up append function and operator

l1.appendItem(4);

l1 & 3;

l1 & 7;

l1.appendItem(0);

cout << "mixing append and operator& l1: " << endl << l1 << endl;

assert(l1.tostring() == "[1, 4, 3, 7, 0]");

assert(l1.getSize() == 5);

assert(l1.getAllocSize() == 10);

cout << endl << endl;

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

// test prepending operator| overloading

cout << "--------- Test prepend and operator| ----------------------------" << endl;

// prepend to empty list

l2.prependItem(8);

cout << "prepend to empty l2: " << endl << l2 << endl;

assert(l2.tostring() == "[8]" );

assert(l2.getSize() == 1);

assert(l2.getAllocSize() == 7);

// prepend to nonempty list

l3.prependItem(8);

cout << "prepend to nonempty l3: " << endl << l3 << endl;

assert(l3.tostring() == "[8, 3, 9, 2, 7, 5, 12, 6, 11]" );

assert(l3.getSize() == 9);

assert(l3.getAllocSize() == 15);

// operator| test

l3 | 13;

l3 | 4;

cout << "operator| test l3: " << endl << l3 << endl;

assert(l3.tostring() == "[4, 13, 8, 3, 9, 2, 7, 5, 12, 6, 11]");

assert(l3.getSize() == 11);

assert(l3.getAllocSize() == 15);

// some more, mix up prepend function and operator

l2.prependItem(7);

l2 & 11;

l2 | 5;

l2.appendItem(9);

l2 | 13;

l2 | 0;

l2 | 4;

cout << "mixing prepend and append and operators l2: " << endl << l2 << endl;

assert(l2.tostring() == "[4, 0, 13, 5, 7, 8, 11, 9]");

assert(l2.getSize() == 8);

assert(l2.getAllocSize() == 17);

cout << endl << endl;

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

// test concatenation

cout << "--------- Test concatenation operator ----------------------------" << endl;

ListType l4 = l2 + l3;

cout << "Test basic append, new l4: " << endl << l4 << endl;

assert(l4.tostring() == "[4, 0, 13, 5, 7, 8, 11, 9, 4, 13, 8, 3, 9, 2, 7, 5, 12, 6, 11]");

assert(l4.getSize() == 19);

assert(l4.getAllocSize() == 19);

ListType l5 = l1 + l3 + l2;

cout << "Test basic append, new l5: " << endl << l5 << endl;

assert(l5.tostring() == "[1, 4, 3, 7, 0, 4, 13, 8, 3, 9, 2, 7, 5, 12, 6, 11, 4, 0, 13, 5, 7, 8, 11, 9]");

assert(l5.getSize() == 24);

assert(l5.getAllocSize() == 24);

ListType emptyList;

ListType l6 = l2 + emptyList;

cout << "Test concatentate emptyList, new l6: " << endl << l6 << endl;

assert(l6.tostring() == l2.tostring());

assert(l6.getSize() == 8);

assert(l6.getAllocSize() == 8);

ListType l7 = emptyList + l1;

cout << "Test concatentate emptyList, new l7: " << endl << l7 << endl;

assert(l7.tostring() == l1.tostring());

assert(l7.getSize() == 5);

assert(l7.getAllocSize() == 5);

cout << endl << endl;

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

// test operator[] indexing and setter

cout << "--------- Test operator[] indexing ------------------------------" << endl;

cout << "l1[0] == " << l1[0] << endl;

assert(l1[0] == 1);

cout << "l1[2] == " << l1[2] << endl;

assert(l1[2] == 3);

cout << "l1[4] == " << l1[4] << endl;

assert(l1[4] == 0);

cout << "Iterate over l2:" << endl << l2 << endl;

for (int index = 0; index < l2.getSize(); index++)

{

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions

Question

Contrast planned- order receipts and scheduled receipts.

Answered: 1 week ago