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();...
How do i stop the loop if no next value in the list and prevent it...
How do i stop the loop if no next value in the list and prevent it from exit the program instead back to main menu. Let say i add food detail then i display it. It will display the detail but it will exit after it. struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; }; void Foodsystem::Place_Order(){ temp=new food; cout<<endl; cin.ignore(); cout<<"Enter your Name"; getline(cin,temp->name); cout<<"enter contact number::"; cin>>temp->contact_number; cin.ignore(); cout<<"enter address::"; getline(cin,temp->address); cout<<"customer_id::"; getline(cin,temp->customer_id);...
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...
Utilize the code from last week Add a default, full, and copy constructor. Also add a...
Utilize the code from last week Add a default, full, and copy constructor. Also add a constructor that allows you to specify only the name of the video game with no high score or times played specified. Adjust your code to demonstrate use of all 4 constructors and output of the resulting objects. #include <iostream> #include <string> #include <iomanip> using namespace std; class VideoGame { private:     string name;     int highScore;     int numOfPlays; public:     VideoGame() {        ...
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);...
Hello, I feel like I am super close but I can not figure out why my...
Hello, I feel like I am super close but I can not figure out why my C++ code it not displaying the proper medium. Thank you! Here is an example of the output: Input : a[] = {1, 3, 4, 2, 6, 5, 8, 7} Output : Mean = 4.5 Median = 4.5 Code so far:   #include <iostream> using namespace std; int main() { int a[100]; int n,i,sum=0; float mean, medium; //read array size // read array cout<<"Enter array 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:...
The following program allows the user to enter the grades of 10 students in a class...
The following program allows the user to enter the grades of 10 students in a class in an array called grade. In a separate loop, you need to test if a grade is passing or failing, and copy the grade to an array to store passing or failing grades accordingly. A passing grade is a grade greater than or equal to 60. You can assume the use will enter a grade in the range of 0 to 100, inclusive. ...
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...