Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment Description: Call your driver \"array_class.cpp\"; call your array_cl

ID: 3666567 • Letter: A

Question

Assignment Description:

Call your driver "array_class.cpp"; call your array_class header file "arrray.h"; call your array class implementation file "array.cpp". Define the following behavior for ARRAY:

1. Overload the "=" operator as a member function with chaining which assigns the state of one ARRAY object to the state of another ARRAY object. Include the message "operator=" invoked" when the function is called.

2.Overload the "+" operator as a member function with chaining to add a string to the ARRAY object's dynamic array. If the array is full, double its size by calling the function "Size_Times_Two". Include the message "operator+ invoked" everytime the function is called.

3. Implement a copy constructor to perform a deep copy of an ARRAY object. Include the message, "Copy Constructor Invoked" whenever the function is called.

4. Overload the "<<" operator as a friend function with chaining which prints all the elements stored in the list of an ARRAY object with chaining.

5. Implement the member function called "Search" to search the list for an item. If the item is there it returns the item's location; otherwise it returns -1. The function should print the message "Search Invoked" everytime the function is called.

6. Implement a member function called "Remove" to remove a string from the list of an ARRAY object. The function should print the message, "Remove Invoked". If the item is in the list print the message "String found"; otherwise print message "String not found".

7. Implement the member function "Size_Times_Two" to double the size of the list of an ARRRAY object when it is full. The function should print the message "Size_Times_Two Invoked" everytime the function is called.

8. Implement the boolean function "Is_Full" that returns true if the list of an ARRAY object is full; otherwise it returns false. The funtion should print the message "Is_Full Invoked" everytime the function is invoked.

9. Implement the boolean funciton "Is_Empty" that returns true if the list of an ARRAY object is empty; otherwise it returns false. The funcion should print the message "Is_Empty Invoked" everytime the function is called.

10. Implement a function called "Sort" to sort the strings in ascending order (a-z, A-Z) that are stored in the list of an ARRAY object. The function should print the message "Sort Invoked" everytime the function is called, and print the array in sorted order.

11. Implement a default constructor. The list of the ARRAY objects should have an initialize capacity of 2. The message "Default Constructor Invoked" should be displayed everytime the function is invoked.

12. Implement an explicit-value constructor to allow the user to enter the initial capacity of the list of an ARRAY object. The message "Explicit-Value Constructor Invoked" should be displayed everytime the function is invoked.

13. Implement a function called ReadFile which reads a list of strings from a file called "array_strings.txt" and stores them in the list of an ARRAY object.

Your program should test the functionality defined above.   Consider the following skeleton class to help you implement the class ARRAY:

class ARRAY

{

public:

//ARRAY();

//ARRAY(int );

//ARRAY(const ARRAY &);

//bool IsEmpty();

//bool IsFull();

//int Search(const string &);

//void Sort();

//ARRAY & operator+(const string );

//void Remove(const string);

//ARRAY & operator=(const ARRAY &);

//friend ostream & operator<<(ostream &, ARRAY &);

//void Size_Times_Two();

//void ReadFile( );

//other functions you may want to implement

private:

//string *DB;

//int count;

//int capacity

//additonal state variables you may wish add

};

Explanation / Answer

array_class.cpp


// header files
#include <iostream>
#include "array.h"
using namespace std;

void fun(ARRAY a) {}

int main ()
{

   ARRAY me, nother, rad;
  
   me.ReadFile();
   nother = rad = me;
   me + "some" + "more" + "stuff";
   nother.Sort();
   cout<<me<<endl<<rad<<endl<<nother<<endl;

   cout<<me.Search("bleh")<<endl;
   me.Remove("stuff");
   me.Remove("bleh");
   fun(nother);
   return 0;
}

array.cpp

// header and namespace
#include "array.h"
using namespace std;

/*****************************************************************************************************
Function Name: ARRAY

Preconditon: The dynamic array DB has not been intialized and no space has been allocated to it.

Postcondition: The dynamic array DB has been initialzied with the contents of the formal parameters

Description: This is the default constructor.

*******************************************************************************************************/
ARRAY::ARRAY()
{
   // initialize the ARRAY object
   cout << "Default constructor invoked" << endl;
   count=0;
   capacity=2;
   DB = new string[capacity];
}

/*****************************************************************************************************
Function Name: ARRAY

Preconditon: The dynamic array DB has not been intialized and no space has been allocated to it. The starting capacity will be i.

Postcondition: The dynamic array DB has been initialzied with the contents of the formal parameters

Description: This is the explicit value constructor.

*******************************************************************************************************/
ARRAY::ARRAY(int i)
{
   // initialize an ARRAY object with a specific starting capacity
   cout << "explicit value contructor invoked" << endl;
   count=0;
   capacity=i;
   DB = new string[i];
}


/*****************************************************************************************************
Function Name: ~ARRAY

Preconditon: The dynamic array DB is initialized.

Postcondition: The memory is deallocated and returned to the heap

Description: This is the destructor and deallocates the dynamic array.

*******************************************************************************************************/
ARRAY::~ARRAY()
{
   cout << "Destructor invoked" << endl;

   // destruct and free up the allocated memory
   delete [] DB;
}


/*****************************************************************************************************
Function Name: ARRAY

Preconditon: The dynamic array DB has not been intialized and no space has been allocated to it.

Postcondition: The dynamic array DB has been initialzied with the contents of the formal parameters

Description: This is the copy constructor which is used to perform a deep copy.

*******************************************************************************************************/
ARRAY::ARRAY(const ARRAY &a)
{
   cout << "copy contstructor invoked" << endl;
   (*this).capacity = a.capacity;
   (*this).count = a.count;
   (*this).DB = new string[(*this).capacity];
   for (int i=0; i<count; i++)
   {
       (*this).DB[i] = a.DB[i];
   }
}


/*****************************************************************************************************
Function Name: ReadFile

Preconditon: The dynamic array DB is initialized.

Postcondition: The file is opened and the strings are stored in the array

Description: The file is opened and fed into the array one line at a time using the overloaded '+' operator.

*******************************************************************************************************/
void ARRAY::ReadFile()
{
   string line; // holds each line

   // open the string file
   ifstream myfile ("array_strings.txt");

   // read the string file
   if (myfile.is_open())
   {
       while ( getline (myfile,line) )
       {
           (*this)+line;
       }
       myfile.close();
   }

else cout << "Unable to open file";
}


/*****************************************************************************************************
Function Name: ReadFile

Preconditon: The dynamic array DB is initialized. String s needs to be opened at read into the array

Postcondition: The file is opened and the strings are stored in the array

Description: Just in case you want to specify the file that you want to open.

*******************************************************************************************************/
void ARRAY::ReadFile(string s)
{
   string line; // holds each line until the is found

   // open the string file
   ifstream myfile (s);

   // read the string file
   if (myfile.is_open())
   {
       while ( getline (myfile,line) )
       {
           (*this)+line;
       }
       myfile.close();
   }

else cout << "Unable to open file";
}


/*****************************************************************************************************
Function Name: Print (Not Included in project)

Preconditon: The dynamic array DB is initialized.

Postcondition: The elements of the array are printed to the console in the order they are indexed.

Description: This function will print each element in the array followed by a newline using the overloaded '<<' insertion operator.

*******************************************************************************************************/
//void ARRAY::Print()
//{
//   cout<<(*this);
//   //for(int i=0;i<count;i++)
//   //{
//   //   cout<<DB[i]<<endl;
//   //}
//}


/*****************************************************************************************************
Function Name: IsEmpty

Preconditon: The dynamic array DB is initialized.

Postcondition: Returns a true boolean if the array is empty.

Description: Checks to see if the array has any elements added to it.

*******************************************************************************************************/
bool ARRAY::IsEmpty()
{
   cout << "is empty invoked" << endl;
   if (count==0) return true;
   return false;
}


/*****************************************************************************************************
Function Name: IsFull

Preconditon: The dynamic array DB is initialized.

Postcondition: A true boolean is returned if the array is full

Description: If the capacity and the number of elements is the same the function will return true.

*******************************************************************************************************/
bool ARRAY::IsFull()
{
   cout << "is full invoked" << endl;
   if (count==capacity) return true;
   return false;
}


/*****************************************************************************************************
Function Name: Search

Preconditon: The dynamic array DB is initialized. Search is the string being sought out

Postcondition: The dynamic array DB has been initialzied with the contents of the formal parameters

Description: This is the copy constructor which is used to perform a deep copy.

*******************************************************************************************************/
int ARRAY::Search(string search)
{
   cout << "Search invoked" << endl;
   for (int i=0;i<count;i++)
   {
       if (DB[i] == search)
       {
           return i;
       }
   }
   return -1;
}


/*****************************************************************************************************
Function Name: Sort

Preconditon: The dynamic array DB is initialized. The array is in the order dictated by how they were added to the array.

Postcondition: The array is sorted alphebetically

Description: A simple sort that finds the smallest element and moves it to the front of the array, then the second to the second place, and so on.

*******************************************************************************************************/
void ARRAY::Sort()
{
   cout << "sort invoked" << endl;

   int place = 0; // holds the index for the smallest element
   string hold;    // holds the value of the current element being swapped
   string smallest;// string with the lowest value alphebetically
   for (int index = 0; index<count; index++)
   {
       // initialize the smallest value as the first element in the array
       smallest = DB[index];
       // run through the rest of the elements to see if another is smaller
       for (int i = index+1; i<count; i++)
       {
           // find the smallest alphebetical value string and remember its place for the swap
           if (DB[i] <= smallest) { smallest = DB[i]; place = i; }
       }
       // make sure the smallest value isn't already where it's supposed to be
       if (smallest != DB[index]) {
           // swap the lowest value with the current itteration of the sort
           hold = DB[index];
           DB[index] = smallest;
           DB[place] = hold;
       }
   }
}


/*****************************************************************************************************
Function Name: Remove

Preconditon: The dynamic array DB is initialized. String s needs to be removed from the array.

Postcondition: The string is removed, the count is 1 lower.

Description: The string is sought out through the search function. Once the place is determined the rest of the elements just move over top of it.

*******************************************************************************************************/
void ARRAY::Remove(string s)
{
   cout << "remove invoked" << endl;

   // find the string's index number within the array
   int place = Search(s);

   // check to see if the string was found
   if (place < 0)
   {
       cout << "string " << s << " not found" << endl;
       return;
   }

   // REMOVAL
   // this just shifts all the elements once to cover up the removed string
   count--;
   for (int i = place; i<count; i++)
   {
       DB[i] = DB[i+1];
   }
}


/*****************************************************************************************************
Function Name: Size_Times_Two

Preconditon: The dynamic array DB is initialized.

Postcondition: The capacity is doubled

Description: Another array is created with twice the capacity, the elements are moved over, then the old array is deleted.

*******************************************************************************************************/
void ARRAY::Size_Times_Two()
{
   cout << "size times two invoked" << endl;
   // double the capacity number
   capacity=capacity*2;

   // create a new larger array with the new capacity
   string *temp = new string[capacity];
  
   // shove everything into the new larger array
   for (int i=0; i<count; i++)
   {
       temp[i] = (*this).DB[i];
   }

   // delete the older small array
   delete []DB;

   // point DB towards the new larger array
   DB = temp;
}


/*****************************************************************************************************
Function Name: operator+

Preconditon: The dynamic array DB is initialized.

Postcondition: The element is added to the end of the array, count is incremented.

Description: The function checks to see if there is enough capacity and then adds the element to the end of the array.

*******************************************************************************************************/
ARRAY & ARRAY::operator+(const string s)
{
   // check to make sure there's room for more
   if (IsFull()) { (*this).Size_Times_Two(); }

   cout << "operator + invoked" << endl;
  
   // put the string in the array
   DB[count] = s;
   count++;

   return *this;
}


/*****************************************************************************************************
Function Name: operator=

Preconditon: The dynamic array DB is initialized.

Postcondition: All array objects in the chain will have equal count, sufficient capacity, and elements

Description: The function first increases the capacity to fit all the elements, matches the count, and copies the elements into a new array

*******************************************************************************************************/
ARRAY & ARRAY::operator=(const ARRAY &a)
{
   cout << "operator = invoked" << endl;
  
   // make enough room for the incoming elements
   while ( (*this).capacity < a.capacity )
   {
       (*this).Size_Times_Two();
   }

   // match the count
   (*this).count = a.count;

   // insert the elements
   for (int i=0; i<count; i++)
   {
       (*this).DB[i] = a.DB[i];
   }

   return (*this);
}


/*****************************************************************************************************
Function Name: operator<<

Preconditon: The dynamic array DB is initialized.

Postcondition: The array is fed into the output stream one element, followed by a newline, at a time.

Description: The elements of the array are fed into the output stream.

*******************************************************************************************************/
ostream & operator<<(ostream &out, ARRAY &a)
{
   for (int i=0; i<a.count; i++)
   {
       out << a.DB[i] << " ";
   }
   return out;
}

array.h

#pragma once
#include <string>
#include <iostream>
#include <fstream>


using namespace std;

class ARRAY
{

public:
   // Constructors and Destructors
   ARRAY();
   ARRAY(int );
   ARRAY(const ARRAY &);
   ~ARRAY();
  
   // Methods
   int Count() { return count; }
   bool IsEmpty();
   bool IsFull();
   int Search(string);
   void Sort();
   void Remove(string);
   void Size_Times_Two();
   void ReadFile();
   void ReadFile(string s);
   void Print();

   // Overloaded Operators
   ARRAY & operator+(const string );
   ARRAY & operator=(const ARRAY &);
   friend ostream & operator<<(ostream &, ARRAY &);

private:
   string *DB;    // pointer to the start of the dynamic array
   int count;     // amount of elements in the array
   int capacity; // current capacity of the dynamic array

};

array_strings.txt

excellent grades
study everyday
read before class
thanksgivings break
i can feel it
a
c
b
g
f
e
d
ek
k
l

Sample Output


Default constructor invoked                                                                                                                                 
Default constructor invoked                                                                                                                                 
Default constructor invoked                                                                                                                                 
is full invoked                                                                                                                                             
operator + invoked                                                                                                                                          
is full invoked                                                                                                                                             
operator + invoked                                                                                                                                          
is full invoked                                                                                                                                             
size times two invoked                                                                                                                                      
operator
operator + invoked                                                                                                                                          
is full invoked                                                                                                                                             
operator + invoked                                                                                                                                          
operator = invoked                                                                                                                                          
size times two invoked                                                                                                                                      
size times two invoked                                                                                                                                      
size times two invoked                                                                                                                                      
operator = invoked                                                                                                                                          
size times two invoked                                                                                                                                      
size times two invoked                                                                                                                                      
size times two invoked                                                                                                                                      
is full invoked                                                                                                                                             
operator + invoked                                                                                                                                          
is full invoked                                                                                                                                             
size times two invoked                                                                                                                                      
operator + invoked                                                                                                                                          

study everyday                                                                                                                                              
read before class                                                                                                                                           
thanksgivings break                                                                                                                                         
i can feel it                                                                                                                                               
a                                                                                                                                                           
c                                                                                                                                                           
b                                                                                                                                                           
g                                                                                                                                                           
f                                                                                                                                                           
e                                                                                                                                                           
d                                                                                                                                                           
ek                                                                                                                                                          
k                                                                                                                                                            

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote