Your task for this assignment is to build a “MaryPoppinsTote” data type. An init
ID: 3750640 • Letter: Y
Question
Your task for this assignment is to build a “MaryPoppinsTote” data type.
An initial implementation will allow “MaryPoppinsTote” to hold integers.
At a minimum it will have the following functions*: MaryPoppinsTote (){}
MaryPoppinsTote (const MaryPoppinsTote & other) {}
~ MaryPoppinsTote (){} void place(int item){}
int reach(){} int search(int item){}
MaryPoppinsTote& operator=( const MaryPoppinsTote & other){}
friend ostream& operator<<(ostream& os, const MaryPoppinsTote & me){}
*Note that the parameters and return types may change slightly once you use a template in order to store an abstract data type.
The Mary Poppins’ Tote The Mary Poppin’s Tote data type will be implemented as follows:
Objects can be inserted into the tote using the insert(item) member function. The tote can hold any number of items, within the bounds of the available RAM. Duplicate items are allowed. (So you cannot set an arbitrary limit like 500000.)
Objects are removed from the tote using the reach() member function. This function returns a random item and removes it from the tote. The returned item should be randomized. In other words, if there are 5 items in the tote, and only one of them is a 7, then there should be a 1 in 5 chance of drawing a 7 with the reach() function. If the tote is empty, the reach() function should throw an exception.
You can check if an object is in the tote using search(item), which should return 0 if there is no item in the tote matching the item parameter. If there are items matching the item parameter in the tote, the number (count) of matching items should be returned.
You can print the contents of the tote using the operator << function. This function should add the contents of the tote to the ostream in the form “[item1 -- item2 -- item3 -- ... -- itemN]” with no endl included. The header will be: o friend ostream& operator<<(ostream& os, const MaryPoppinsTote & me)
You should be able to assign the contents of a MaryPoppinsTote using the = operator. This should result in a copy of the tote that is not linked to the original tote. In other words, if a and b are MaryPoppinsTotes, the line "a = b;" should result in totes a and b having the same contents. If I then draw elements from tote b, this should not change the contents of tote a.
Use separate .hpp and .cpp files.
You should begin by constructing a MaryPoppinsTote that holds integers. This int only implementation must be submitted! Save as files MaryPoppinsToteInt.hpp and MaryPoppinsToteInt.cpp
Re-implement MaryPoppinsTote except make it work for different primitive data types by using a template.
Implement the MaryPoppinsTote using an array. You may not use C++ standard template library vectors for the underlying data structure. (An array should be the most advanced built in data structure you use.) This should be similar as to what we did in class with BetterArray.
No points will be awarded for submissions that do not compile.
Explanation / Answer
// Without template only for integer
// File Name: MaryPoppinsToteInt.h
#ifndef Rational_H
#define Rational_H
#include <iostream>
#define MAX 10
using namespace std;
// Class MaryPoppinsTote definition
class MaryPoppinsTote
{
// To store item
int *myArray;
// To store current index counter
int current;
// To store maximum size
int size;
public:
// Prototype of member function
MaryPoppinsTote ();
MaryPoppinsTote (int);
MaryPoppinsTote (const MaryPoppinsTote & other);
~ MaryPoppinsTote ();
int getSize();
void place(int item);
int reach();
int search(int item);
MaryPoppinsTote& operator=( const MaryPoppinsTote & other);
friend ostream& operator<<(ostream& os, const MaryPoppinsTote & me);
};// End of class
#endif
-----------------------------------------------------------------------------------
// File Name: MaryPoppinsToteInt.cpp
#include "MaryPoppinsToteInt.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
// Default constructor definition
MaryPoppinsTote::MaryPoppinsTote()
{
// Dynamically allocates memory of MAX size
myArray = new int[MAX];
// Checks if memory is not able to allocate displays error message
if(myArray == NULL)
cout<<" ERROR: Unable to allocate memory.";
// Set the current index counter to 0
current = 0;
// Sets the size to MAX
size = MAX;
}// End of default constructor
// Parameterized constructor definition
MaryPoppinsTote::MaryPoppinsTote(int no)
{
// Dynamically allocates memory of size parameter value no
myArray = new int[no];
// Checks if memory is not able to allocate displays error message
if(myArray == NULL)
cout<<" ERROR: Unable to allocate memory.";
// Set the current index counter to 0
current = 0;
// Sets the size to parameter value no
size = no;
}// End of parameterized constructor
// Copy constructor definition
MaryPoppinsTote::MaryPoppinsTote(const MaryPoppinsTote & other)
{
// Dynamically allocates memory of size parameter object other size value
myArray = new int[other.size];
// Checks if memory is not able to allocate displays error message
if(myArray == NULL)
cout<<" ERROR: Unable to allocate memory.";
// Loops till parameter object current index position
for(int x = 0; x < other.current; x++)
// Assigns each element of object other's array to implicit object's array
myArray[x] = other.myArray[x];
// Sets the parameter object other's current counter to implicit object's counter
current = other.current;
}// End of copy constructor
// Defines destructor to deallocate memory
MaryPoppinsTote::~MaryPoppinsTote()
{
// Deletes the array
delete myArray;
// Sets the current counter to 0
current = 0;
}// End of destructor
// Function to return the size
int MaryPoppinsTote::getSize()
{
return size;
}// End of function
// Function to insert an item to array current index position
void MaryPoppinsTote::place(int item)
{
// Assigns parameter value to current index position of the array
myArray[current] = item;
// Increase the current counter by one
current++;
}// End of function
// Function to delete an random number from the array
int MaryPoppinsTote::reach()
{
// To store the random number
int no;
// Sets the found position to -1
int pos = -1;
// Checks if the current index counter is 0 display error message
if(current == 0)
cout<<" No item to delete.";
// Otherwise items available
else
{
// Generates a random number between 1 and current count
no = rand() % current + 1;
// Loops till current index counter
for(int x = 0; x < current; x++)
{
// Checks if array current index position is equals to the random number
if(myArray[x] == no)
{
// Assigns the found index position
pos = x;
// Decrease the current index counter by one
current--;
// Loops from found index position to current length of the array
for(int y = pos; y < current; y++)
// Shift each number to left
myArray[y] = myArray[y + 1];
}// End of if condition
}// End of for loop
// Checks if found status is -1 then assign pos to number
if(pos == -1)
no = pos;
}// End of else
// Return the number
return no;
}// End of function
// Function to search an item in the array and return count of occurrence
int MaryPoppinsTote::search(int item)
{
// Initializes the found counter to 0
int found = 0;
// Loops till current length of the array
for(int x = 0; x < current; x++)
// Checks if current index position of the array is equals to the parameter item value
if(myArray[x] == item)
// Increase the found counter by one
found++;
// Returns the found counter
return found;
}// End of function
// Overloads the = operator
MaryPoppinsTote& MaryPoppinsTote::operator =( const MaryPoppinsTote &other)
{
// Dynamically allocates memory of size parameter object other size value
myArray = new int[other.size];
// Checks if memory is not able to allocate displays error message
if(myArray == NULL)
cout<<" ERROR: Unable to allocate memory.";
// Loops till current length of the array
for(int x = 0; x < other.current; x++)
// Assigns each element of the parameter object other's array to implicit object's array
myArray[x] = other.myArray[x];
// Updates the current length of the implicit object by assigning parameter object other's length
current = other.current;
}// End of function
// Overloads the << operator
ostream& operator<<(ostream& os, const MaryPoppinsTote & me)
{
// Loops till current length of the array
for(int x = 0; x < me.current; x++)
// Stores each element of the array in ostream object os
os<<me.myArray[x]<<", ";
// Assigns a new line
os<<endl;
// Returns the object
return os;
}// End of function
-----------------------------------------------------------------------------------
// File Name: MainMaryPoppinsToteInt.cpp
#include "MaryPoppinsToteInt.cpp"
#include <iostream>
using namespace std;
// main function definition
int main()
{
// Creates an object first and four using default constructor with default size 10
// Creates an object third using parameterized constructor of size 20
MaryPoppinsTote first, third(20), four;
int no;
// Loops till default size of the array
for(int x = 0; x < MAX; x++)
// Calls the function to add a random number
first.place(rand() % MAX + 1);
// Loops till size of the array (20)
for(int x = 0; x < third.getSize(); x++)
// Calls the function to add a random number
third.place(rand() % third.getSize() + 1);
// Displays the first object
cout<<" First array contents: ";
cout<<first;
// Calla the function to search 5 and displays number of occurrence
no = first.search(5);
// Checks the return value of the function if it is 0 item not found
if(no == 0)
cout<<" Item 5 not found.";
// Otherwise display number of occurrence
else
cout<<" Number of items matches - 5 is: "<<no;
// Creates an object second using copy constructor
cout<<" Creates second object by passing first object as parameter: ";
MaryPoppinsTote second(first);
// Displays the second object
cout<<" Second array contents: ";
cout<<second;
// Displays the third object
cout<<" Third array contents: ";
cout<<third;
// Assigns third object to fourth object using operator overloading =
cout<<" Assigns third object to fourth object: ";
four = third;
// Displays the fourth object
cout<<" Fourth array contents: ";
cout<<four;
// Call the function to delete an item from the array of fourth object
cout<<" Deleting a random item: ";
no = four.reach();
// Checks the return value of the function if it is -1 item not found
if(no == -1)
cout<<" Item not found.";
// Otherwise display the deleted item
else
cout<<" Deleted Item is: "<<no;
// Displays the fourth object
cout<<" After deletion fourth object: ";
cout<<four;
}// End of main function
Sample Output:
First array contents: 2, 8, 5, 1, 10, 5, 9, 9, 3, 5,
Number of items matches - 5 is: 3
Creates second object by passing first object as parameter:
Second array contents: 2, 8, 5, 1, 10, 5, 9, 9, 3, 5,
Third array contents: 6, 6, 2, 8, 2, 12, 16, 3, 8, 17, 12, 5, 3, 14, 13, 3, 2, 17, 19, 16,
Assigns third object to fourth object:
Fourth array contents: 6, 6, 2, 8, 2, 12, 16, 3, 8, 17, 12, 5, 3, 14, 13, 3, 2, 17, 19, 16,
Deleting a random item:
Deleted Item is: 8
After deletion fourth object: 6, 6, 2, 2, 12, 16, 3, 17, 12, 5, 3, 14, 13, 3, 2, 17, 19, 16,
---------------------------------------------------------------------------------------------------------------------------
// Template implementation
// File Name: MaryPoppinsToteTemplate.h
#ifndef Rational_H
#define Rational_H
#include <iostream>
#define MAX 10
using namespace std;
// Prototype of class
template <class Item>
class MaryPoppinsTote;
// Prototype of operator overloading function
template<class Item>
ostream& operator<< ( ostream & , const MaryPoppinsTote<Item>&);
// Template class MaryPoppinsTote definition
template <class Item>
class MaryPoppinsTote
{
// Template type pointer to store data
Item *myArray;
// To store current index counter
int current;
// To store maximum size
int size;
public:
// Prototype of member function
MaryPoppinsTote ();
MaryPoppinsTote (int);
MaryPoppinsTote (const MaryPoppinsTote<Item> &);
~ MaryPoppinsTote ();
int getSize();
void place(Item);
Item reach();
int search(Item);
MaryPoppinsTote& operator=( const MaryPoppinsTote<Item>&);
friend ostream& operator<< <>(ostream&, const MaryPoppinsTote &);
};// End of class
#endif
------------------------------------------------------------------------------------------
// File Name: MaryPoppinsToteTemplate.cpp
#include "MaryPoppinsToteTemplate.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
// Default constructor definition
template <class Item>
MaryPoppinsTote<Item>::MaryPoppinsTote()
{
// Dynamically allocates memory of MAX size
myArray = new Item[MAX];
// Checks if memory is not able to allocate displays error message
if(myArray == NULL)
cout<<" ERROR: Unable to allocate memory.";
// Set the current index counter to 0
current = 0;
// Sets the size to MAX
size = MAX;
}// End of default constructor
// Parameterized constructor definition
template <class Item>
MaryPoppinsTote<Item>::MaryPoppinsTote(int no)
{
// Dynamically allocates memory of size parameter value no
myArray = new Item[no];
// Checks if memory is not able to allocate displays error message
if(myArray == NULL)
cout<<" ERROR: Unable to allocate memory.";
// Set the current index counter to 0
current = 0;
// Sets the size to parameter value no
size = no;
}// End of parameterized constructor
// Copy constructor definition
template <class Item>
MaryPoppinsTote<Item>::MaryPoppinsTote(const MaryPoppinsTote<Item> &other)
{
// Dynamically allocates memory of size parameter object other size value
myArray = new Item[other.size];
// Checks if memory is not able to allocate displays error message
if(myArray == NULL)
cout<<" ERROR: Unable to allocate memory.";
// Loops till parameter object current index position
for(int x = 0; x < other.current; x++)
// Assigns each element of object other's array to implicit object's array
myArray[x] = other.myArray[x];
// Sets the parameter object other's current counter to implicit object's counter
current = other.current;
}// End of copy constructor
// Defines destructor to deallocate memory
template <class Item>
MaryPoppinsTote<Item>::~MaryPoppinsTote()
{
// Deletes the array
delete myArray;
// Sets the current counter to 0
current = 0;
}// End of destructor
// Function to return the size
template <class Item>
int MaryPoppinsTote<Item>::getSize()
{
return size;
}// End of function
// Function to insert an item to array current index position
template <class Item>
void MaryPoppinsTote<Item>::place(Item item)
{
// Assigns parameter value to current index position of the array
myArray[current] = item;
// Increase the current counter by one
current++;
}// End of function
// Function to delete an random number from the array
template <class Item>
Item MaryPoppinsTote<Item>::reach()
{
// To store the random number
Item no;
// Sets the found position to -1
int pos = -1;
// Checks if the current index counter is 0 display error message
if(current == 0)
cout<<" No item to delete.";
// Otherwise items available
else
{
// Generates a random number between 1 and current count
no = (rand() / (Item)RAND_MAX * 19) + 1;
// Loops till current index counter
for(int x = 0; x < current; x++)
{
// Checks if array current index position is equals to the random number
if(myArray[x] == no)
{
// Assigns the found index position
pos = x;
// Decrease the current index counter by one
current--;
// Loops from found index position to current length of the array
for(int y = pos; y < current; y++)
// Shift each number to left
myArray[y] = myArray[y + 1];
}// End of if condition
}// End of for loop
// Checks if found status is -1 then assign pos to number
if(pos == -1)
no = pos;
}// End of else
// Return the number
return no;
}// End of function
// Function to search an item in the array and return count of occurrence
template <class Item>
int MaryPoppinsTote<Item>::search(Item item)
{
// Initializes the found counter to 0
int found = 0;
// Loops till current length of the array
for(int x = 0; x < current; x++)
// Checks if current index position of the array is equals to the parameter item value
if(myArray[x] == item)
// Increase the found counter by one
found++;
// Returns the found counter
return found;
}// End of function
// Overloads the = operator
template <class Item>
MaryPoppinsTote<Item>& MaryPoppinsTote<Item>::operator =( const MaryPoppinsTote<Item> &other)
{
// Dynamically allocates memory of size parameter object other size value
myArray = new Item[other.size];
// Checks if memory is not able to allocate displays error message
if(myArray == NULL)
cout<<" ERROR: Unable to allocate memory.";
// Loops till current length of the array
for(int x = 0; x < other.current; x++)
// Assigns each element of the parameter object other's array to implicit object's array
myArray[x] = other.myArray[x];
// Updates the current length of the implicit object by assigning parameter object other's length
current = other.current;
}// End of function
// Overloads the << operator
template <class Item>
ostream& operator<< (ostream& os, const MaryPoppinsTote<Item> & me)
{
// Loops till current length of the array
for(int x = 0; x < me.current; x++)
// Stores each element of the array in ostream object os
os<<me.myArray[x]<<", ";
// Assigns a new line
os<<endl;
// Returns the object
return os;
}// End of function
---------------------------------------------------------------------------------------------------------
// File Name: MainMaryPoppinsToteTemplate.cpp
#include "MaryPoppinsToteTemplate.cpp"
#include <iostream>
using namespace std;
// main function definition
int main()
{
// Creates an object first and four using default constructor with default size 10
// Creates an object third using parameterized constructor of size 20
MaryPoppinsTote <double>first;
MaryPoppinsTote <double>third(20);
MaryPoppinsTote <double>four;
int no;
// Loops till default size of the array
for(int x = 0; x < MAX; x++)
// Calls the function to add a random number
first.place((rand() / (double)RAND_MAX * 19) + 1);
// Loops till size of the array (20)
for(int x = 0; x < third.getSize(); x++)
// Calls the function to add a random number
third.place((rand() / (float)RAND_MAX * 19) + 1);
// Displays the first object
cout<<" First array contents: ";
cout<<first;
// Calla the function to search 5 and displays number of occurrence
no = first.search(5);
// Checks the return value of the function if it is 0 item not found
if(no == 0)
cout<<" Item 5 not found.";
// Otherwise display number of occurrence
else
cout<<" Number of items matches - 5 is: "<<no;
// Creates an object second using copy constructor
cout<<" Creates second object by passing first object as parameter: ";
MaryPoppinsTote <double>second(first);
// Displays the second object
cout<<" Second array contents: ";
cout<<second;
// Displays the third object
cout<<" Third array contents: ";
cout<<third;
// Assigns third object to fourth object using operator overloading =
cout<<" Assigns third object to fourth object: ";
four = third;
// Displays the fourth object
cout<<" Fourth array contents: ";
cout<<four;
// Call the function to delete an item from the array of fourth object
cout<<" Deleting a random item: ";
no = four.reach();
// Checks the return value of the function if it is -1 item not found
if(no == -1)
cout<<" Item not found.";
// Otherwise display the deleted item
else
cout<<" Deleted Item is: "<<no;
// Displays the fourth object
cout<<" After deletion fourth object: ";
cout<<four;
}// End of main function
Sample Output:
First array contents: 1.02377, 11.7081, 4.67278, 16.3661, 12.1152, 10.1176, 7.65554, 18.0233, 16.634, 15.1855,
Item 5 not found.
Creates second object by passing first object as parameter:
Second array contents: 1.02377, 11.7081, 4.67278, 16.3661, 12.1152, 10.1176, 7.65554, 18.0233, 16.634, 15.1855,
Third array contents: 4.30805, 17.3199, 14.4995, 10.7572, 6.7759, 1.28471, 2.73666, 7.92459, 3.79894, 4.15207, 19.782, 9.46815, 3.26258, 1.08872, 1.16932, 8.17972, 11.1016, 11.8525, 12.4335, 12.5361,
Assigns third object to fourth object:
Fourth array contents: 4.30805, 17.3199, 14.4995, 10.7572, 6.7759, 1.28471, 2.73666, 7.92459, 3.79894, 4.15207, 19.782, 9.46815, 3.26258, 1.08872, 1.16932, 8.17972, 11.1016, 11.8525, 12.4335, 12.5361,
Deleting a random item:
Item not found.
After deletion fourth object: 4.30805, 17.3199, 14.4995, 10.7572, 6.7759, 1.28471, 2.73666, 7.92459, 3.79894, 4.15207, 19.782, 9.46815, 3.26258, 1.08872, 1.16932, 8.17972, 11.1016, 11.8525, 12.4335, 12.5361,
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.