Question

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.

– A function named getSize that returns size.
– A function named addEntry that takes a string as input. The function should create a new dynamic array one element larger than dynamicArray, copy all elements from dynamicArray into the new array, add the new string onto the end of the new array, increment size, delete the old dynamicArray, and then set dynamicArray to the new array.
– A function named deleteEntry that takes a string as input. The function should search dynamicArray for the string. If not found, return false. If found, create a new dynamic array one element smaller than dynamicArray. Copy all elements except the input string into the new array, delete dynamicArray, decrement size, and return true.
– A function named getEntry that takes an integer as input and returns the string at that index in dynamicArray. Return NULL if the index is out of dynamicArray’s bounds.
– A copy constructor that makes a copy of the input object’s dynamic array.
– Overload the assignment operator so that the dynamic array is properly copied to the target object.
– A destructor that frees up the memory allocated to the dynamic array.

HomeWork 3

#include <iostream>

#include <string>

using namespace std;

class DynamicStringArray

{

private:  

string* dynamicArray;  

int size;

public:  

DynamicStringArray();  

DynamicStringArray(string* dynamicArray, int size);  

DynamicStringArray(const DynamicStringArray &copy);  

DynamicStringArray& operator =(const DynamicStringArray& rgtSide);  

~DynamicStringArray();  

void setDynamicArray(string* dynamicArray);  

void setSize(int size);  

string* getDynamicArray();  

int getSize();  

void addEntry(string input);  

bool deleteEntry(string input);  

string getEntry(int index);  

void output();

};

int main() {

   DynamicStringArray version1;

DynamicStringArray version2;  

string* strArr;

string entry, repeat;

int size, num, index;   

while(size <= 0) { cout << "how long do you want the array's size to be? "<< endl; cin >> size;

}

cin.ignore();

strArr = new string[size];  

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

{    

cout << "String " << i << ": ";    

getline(cin, strArr[i]);

}

   version1.setDynamicArray(strArr);

version1.setSize(size);   

do {

cout << "If you want to add Entry press 1" << endl;  

cout << "If you want to delete Entry press 2" << endl;  

cout << "If you want to get Entry press 3" << endl;  

cout << "If you want to copy the whole array press 4" << endl;  

cout << "Your choice is: " << endl;  

cin >> num;  

cin.ignore();  

cout << endl;       

if(num == 1)   

{    

cout << "Entry you want to add to array: " << endl;    

getline(cin, entry);      

version1.addEntry(entry);    

version1.output();   

}

    else if(num == 2)     

{      

cout << "Entry you want to delete from array: " << endl;     

getline(cin, entry);             

if(version1.deleteEntry(entry))       

cout << "This Entry has been deleted" << endl;      

else        

cout << "Out Of Range" << endl;               

version1.output();    

}    

else if

(num == 3)  

    {
      cout << "Prss a number within the array's size range to look at it's index: " << endl;

       cin >> index;      

cin.ignore();      

cout << "The Entry is : " << version1.getEntry(index) << endl;      

version1.output();   

}   

else if

(num == 4)     

{      

version2 = version1;      

cout << "The copy version of your array is : " << endl;      

version2.output();      

version2.~DynamicStringArray();   

}         

cout << endl;    

cout << "press ENTER to continue and 0 to exit : " << endl;    

getline(cin, repeat);  

}     

while(repeat != "0");  

version1.~DynamicStringArray();  

}

DynamicStringArray::DynamicStringArray()

{  

dynamicArray = NULL;   size = 0;

}

DynamicStringArray::DynamicStringArray(string* arr, int s)

{  

dynamicArray = arr;   size = s;

}

DynamicStringArray::DynamicStringArray(const DynamicStringArray &copy)

{  

dynamicArray = new string;  

dynamicArray = copy.dynamicArray;  

size = copy.size;

}

DynamicStringArray& DynamicStringArray::operator =(const DynamicStringArray& rgtSide)
{  

if(this == &rgtSide)    

return *this;  

else    {    

size = rgtSide.size;    

delete [] dynamicArray;    

dynamicArray = new string[size];    

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

dynamicArray[i] = rgtSide.dynamicArray[i];    

return *this;  

}

}

DynamicStringArray::~DynamicStringArray()

{  

delete [] dynamicArray;  

dynamicArray = NULL;

}

void DynamicStringArray::setDynamicArray(string* arr)

{  

dynamicArray = arr;

}  

void DynamicStringArray::setSize(int s)

{  

size = s;

}

string* DynamicStringArray::getDynamicArray()

{

   return dynamicArray;

}

int DynamicStringArray::getSize()

{

return size;

}

void DynamicStringArray::addEntry(string input)

{

string* temp;

   temp = new string[size+1];  
for(int i = 0; i < size; i++)

   {

     temp[i] = dynamicArray[i];

   }     

temp[size] = input;  

dynamicArray = temp;    

size++;

}

bool DynamicStringArray::deleteEntry(string input)

{

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

{

    if(dynamicArray[i] == input)    

{

for(int j = i; j < size-1; j++)   

{

      dynamicArray[j] = dynamicArray[j+1];

}  

     size--;      

return true;   

}  

}   

return false;

}

string DynamicStringArray::getEntry(int index)

{  

if(index >= size)    

return "NULL";  

else    

return dynamicArray[index];

}

void DynamicStringArray::output()

{  

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

{    

cout << "String " << i << ": " << dynamicArray[i] << endl;  

}

}


Homework Answers

Answer #1

//Modified c++ code

#include <iostream>

#include <string>

using namespace std;

template<typename T>
class DynamicStringArray

{

private:

T* dynamicArray;

int size;

public:

DynamicStringArray();

DynamicStringArray(T* dynamicArray, int size);

DynamicStringArray(const DynamicStringArray<T> &copy);

DynamicStringArray& operator =(const DynamicStringArray<T>& rgtSide);

~DynamicStringArray();

void setDynamicArray(T* dynamicArray);

void setSize(int size);

T* getDynamicArray();

int getSize();

void addEntry(T input);

bool deleteEntry(T input);

T getEntry(int index);

void output();

};


template<typename T>
DynamicStringArray<T>::DynamicStringArray()

{

dynamicArray = NULL;   size = 0;

}
template<typename T>
DynamicStringArray<T>::DynamicStringArray(T* arr, int s)

{

dynamicArray = arr;   size = s;

}
template<typename T>
DynamicStringArray<T>::DynamicStringArray(const DynamicStringArray<T> &copy)

{

dynamicArray = new T;

dynamicArray = copy.dynamicArray;

size = copy.size;

}
template<typename T>
DynamicStringArray<T>& DynamicStringArray<T>::operator =(const DynamicStringArray<T>& rgtSide)
{

if(this == &rgtSide)  

return *this;

else    {  

size = rgtSide.size;  

delete [] dynamicArray;  

dynamicArray = new T[size];  

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

dynamicArray[i] = rgtSide.dynamicArray[i];  

return *this;

}

}
template<typename T>
DynamicStringArray<T>::~DynamicStringArray()

{

delete [] dynamicArray;

dynamicArray = NULL;

}
template<typename T>
void DynamicStringArray<T>::setDynamicArray(T* arr)

{

dynamicArray = arr;

}
template<typename T>
void DynamicStringArray<T>::setSize(int s)

{

size = s;

}
template<typename T>
T* DynamicStringArray<T>::getDynamicArray()

{

   return dynamicArray;

}
template<typename T>
int DynamicStringArray<T>::getSize()

{

return size;

}
template<typename T>
void DynamicStringArray<T>::addEntry(T input)

{

T* temp;
temp = new T[size+1];
for(int i = 0; i < size; i++)

   {

     temp[i] = dynamicArray[i];

   }   

temp[size] = input;

dynamicArray = temp;  

size++;

}
template<typename T>
bool DynamicStringArray<T>::deleteEntry(T input)

{

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

{

    if(dynamicArray[i] == input)  

{

for(int j = i; j < size-1; j++)

{

      dynamicArray[j] = dynamicArray[j+1];

}

     size--;    

return true;

}

}

return false;

}
template<typename T>
T DynamicStringArray<T>::getEntry(int index)

{

if(index >= size)  

return "NULL";

else  

return dynamicArray[index];

}
template<typename T>
void DynamicStringArray<T>::output()

{

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

{  

cout << "Value " << i << ": " << dynamicArray[i] << endl;

}

}

int main() {
DynamicStringArray<string> version1;

DynamicStringArray<string> version2;

string* strArr;

string entry, repeat;

int size, num, index;

while(size <= 0) { cout << "how long do you want the array's size to be? "<< endl; cin >> size;

}

cin.ignore();

strArr = new string[size];

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

{  

cout << "String " << i << ": ";  

getline(cin, strArr[i]);

}

   version1.setDynamicArray(strArr);

version1.setSize(size);

do {

cout << "If you want to add Entry press 1" << endl;

cout << "If you want to delete Entry press 2" << endl;

cout << "If you want to get Entry press 3" << endl;

cout << "If you want to copy the whole array press 4" << endl;

cout << "Your choice is: " << endl;

cin >> num;

cin.ignore();

cout << endl;     

if(num == 1)

{  

cout << "Entry you want to add to array: " << endl;  

getline(cin, entry);    

version1.addEntry(entry);  

version1.output();

}

    else if(num == 2)   

{    

cout << "Entry you want to delete from array: " << endl;   

getline(cin, entry);           

if(version1.deleteEntry(entry))     

cout << "This Entry has been deleted" << endl;    

else      

cout << "Out Of Range" << endl;             

version1.output();  

}  

else if

(num == 3)

    {
      cout << "Prss a number within the array's size range to look at it's index: " << endl;

       cin >> index;    

cin.ignore();    

cout << "The Entry is : " << version1.getEntry(index) << endl;    

version1.output();

}

else if

(num == 4)   

{    

version2 = version1;    

cout << "The copy version of your array is : " << endl;    

version2.output();    

version2.~DynamicStringArray();

}       

cout << endl;  

cout << "press ENTER to continue and 0 to exit : " << endl;  

getline(cin, repeat);

}   

while(repeat != "0");

version1.~DynamicStringArray();

}

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
How to stop the program from exiting after display detail. When there is food detail, it...
How to stop the program from exiting after display detail. When there is food detail, it will display and exit the program. What can i do to make it not exit the program and back to main menu. #include <iostream> #include <iomanip> #include<string.h> using namespace std; struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; };    class Foodsystem{ food *head,*temp,*temp2,*end; static int id;    public: Foodsystem(){ head=NULL;end=NULL;} void Place_Order(); void View_food_details(); void Modify_food_details(); void Delete_food_details();...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
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...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
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)...
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
For some reason I followed the steps in my project and I am getting the incorrect...
For some reason I followed the steps in my project and I am getting the incorrect output and when I am submitting it, it gives me compilation error. Printing empty array -- next line should be blank Testing append: Shouldn't crash! Should print 100 through 110 below, with 110 on a new line: 100 101 102 103 104 105 106 107 108 109 110 Checking capacity of new array: OK Append test #2: Should print 100 through 120 below, on...
In the attached FlexArray Java class, implement a method public int delete (int location) { }...
In the attached FlexArray Java class, implement a method public int delete (int location) { } that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n. ***************************************************************************************************************************** public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Your code here ----------------- } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to...