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

CLASSES AND POINTERS Define a class called textLines that will be used to store

ID: 3844260 • Letter: C

Question

CLASSES AND POINTERS

Define a class called textLines that will be used to store a list of lines of text (each line can be specified as a string or a c-string, whichever you prefer).

Use a dynamic array to store the list.  In addition, you should have a private data member that specifies the length of the list.

Create a constructor that takes a file name as parameter, and fills up the list with lines from the file.  Make sure that you set the dynamic array at least large enough to hold all the lines from the file.  Also, create a constructor that takes an integer parameter that sets the size of an empty list.

Write member functions to:

1. remove and return the last  line from the list

2. add a new line onto the end of the list, if there is room for it, otherwise print an error message

3. empty the entire list

4. return the number of lines still on the list

5. take two lists and return one combined list (with no duplicates)

6. copy constructor to support deep copying

7. remember the destructor!

Explanation / Answer

#include #include #include using namespace std; // Define class called textLines (used to store list of lines) class textLines { public: // Main Constructor textLines(ifstream& myfile1){ pointer = new string[stringsize]; if (myfile1.fail()) { cout pointer[index]; } } // Constructor that takes an integer parameter that sets the size of an empty list. textLines(int){ pointer = new string[0]; } // Deconstructor ~textLines(){ delete[] pointer; } void printArray(); private: ifstream infile; ofstream outfile; static int stringsize; string* pointer; }; // Begin Main Function int main(){ string myfile = "Lab3Text.txt"; ifstream infile(myfile); textLines text(infile); text.printArray(); return 0; } // End Main int textLines::stringsize = 1000; void textLines::printArray(){ for (int index = 0; index