Object Oriented Programming in C++ - Lab 7

 

In this lab you will write another version of your line editor.

In this new version, you will use the String class presented in class, instead of the built in string class. The array data member of the Line Editor will now be an array of our String objects, rather than an array of the built in string objects. Since the set of member functions is different for the new String class, you will have to rewrite some of the member functions of the Line Editor.

You will also need to write the find member function and the substring member function for the new String class. I will give you the code for the rest of the member functions.

The find member function has one parm, a character. The function searches for that character in the invoking object, and returns the subscript where that character was found. If the character is in the invoking object more than once, find should return the first position where the character was found. If the character is not found anywhere in the invoking object, find should return -1. For example, in the following code:

   String phrase("The Great Pretender");
   int loc1 = phrase.find('a');
   int loc2 = phrase.find('r');
   int loc3 = phrase.find('s');

loc1 is set to 7, loc2 is set to 5, and loc3 is set to -1.

The substring member function has 3 parms: a String, and two ints. The ints specify a starting position and number of characters. The substring function sets the first parm to contain the String obtained by taking characters from the invoking object, from the starting position (given by the second parm). The number of characters to copy into the first parm is given by the third parm. If the starting position or the length is invalid for the invoking object, substring returns false and doesn't change the first parm. Otherwise substring returns true. For example, in the following code:

   String phrase("The Great Pretender");
   int ok1 = phrase.substring(word1,4,5);
   int ok2 = phrase.substring(word2,0,6);
   int ok3 = phrase.substring(word3,16,5);

word1 contains "Great", and ok1 contains 1, word2 contains "The Gr" and ok2 contains 1, word3 is unchanged by substring, and ok3 contains 0.

Also, modify your client program, if necessary, so that it prints the list of edit commands once, when the program first starts. After that, prompt the user for commands by printing the string ">> ", rather than a menu of edit commands or a message. This is the way that real line editors work.


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2002