 So, last time we discussed use of classes in some applications. We specifically looked at the editor application and saw a interesting model where the text to be edited could be kept in two different stacks with an assumed cursor in between. So, if I want to edit a text, I assume a cursor, I move the cursor to some position and I arrange things such that whatever is to the left of the cursor goes into the left stack and whatever is to the right of the cursor goes into the right stack. That makes editing conceptually simpler. Whenever I want to move cursor forward or backward, I effectively pop characters from one stack and push them on to another. If I want to insert something before the cursor, I insert those characters onto the left stack by pushing them in and if I want to delete characters at a cursor position to the left of cursor again, I would just pop characters from the left stack and forget them. What we did was that we implemented the class stack and then used it in our main program for editing. We will extend that model further by first isolating the stack class implementation in a separate file and see how that file could be included in our program. Additionally, we will now define another class called the editor class where the interface functions to the editor could be implemented by us taking cognizance of whatever editor commands we have in mind. We will first define those commands and see how those commands are to be implemented. Then we will isolate the necessary general functions of the editor which are required to do so and we will implement them. We will write the code for them as member functions of the class editor. That way our main program would become much simplified. I also have some very important announcements to make which I will do at the end. First of all, if I define a class stack in a separate file, I could actually call that file as stack dot h. Dot h is a standard header file symbol. Any extension dot h typically means that it contains some headers. Header is not necessarily only text by the way, but header is the nomenclature given to files which will occupy the top position in any program when it is compiled. Essentially, you include header files through a preprocessor command like hash include. We have seen those commands elsewhere. We include the library functions like that. The difference is that the library functions are included at the linking time. Whenever your program is compiled, it is to be linked with the appropriate functions and then you use those library functions. But you could also include text files which will become part and parcel of your program before it is compiled. So the preprocessor works to include those files. So here is a possible implementation where I have this class stack. Notice that I am defining char s size, but the size itself is not defined here. You will recall that we had used hash defined size as 10000. We had defined these two. We continue to use that in our main program itself. So please note that this stack dot h file cannot be compiled unless size has been defined somewhere before this file is included. As usual, I have the top of stack integer which points to the top of any stack where a character can be inserted and then I have these three public functions. So the rest of the implementation is exactly identical to what we had seen except that the entire class definition is now removed from our program and it is kept in a separate file. So here I have the init stack. We just initializes the top of stack. Here I have push character c. It checks whether top is equal to size in which case there is no position to place the character. Please note that tops always denotes the next position empty in the implementation of stack as an array. So the next empty position is where you have to push the next character. However, if that next position itself is equal to size and that total element boundaries of the array is 0 and size minus 1, I can't implement it and therefore I say stack is full. Otherwise I assign c to the top of stack and increment it off. Notice that this array, etcetera, etcetera is part of this and is part of the private data members of the stack. So look at this, char s size that is the array which is going to implement the stack. The pop function just pops a character. So it has a return value called character. It will first reduce tops by 1 because tops points to the position which is an empty position and therefore the first character of the stack is actually 1 less than that. So I reduce that and return tops. When I return the character, when I have popped it off, the current value of tops becomes the next available position because that position has been logically made empty because that character has been popped out. Obviously if I have no character to pop, if the pointer points to 0, then I announce stack is empty. Notice that these implementations of push and pop do not permit me to examine such extreme conditions in my program from where I will invoke these functions. I will need some mechanism because I may not want in a more complex program where I use these stocks, I may not want error messages to be given by the class member functions themselves. I would rather have the member functions return some indication to me so that I can use it judiciously to decide what error message I want to give. That is the reason why you will find many of the member functions of such classes have a return value. In this case, char is the return value. Notice that I am returning an arbitrary character star so that if I locate a star then I can know that there is something wrong with the stack that the stack was programmed. However, it is quite possible that there could be a star as a legitimate character and therefore I have a problem. So we will see exactly how do we handle such things later when we discuss the editor class. The editor class itself is defined. It could be defined as another file called editor.h. In the example program that I am showing here, it is included as part of the main program itself but you could separate it out. This is the kind of documentation that is expected to be written incidentally. I have been repeatedly emphasizing it. I have seen some of your submissions of stage one. The programs are written and there is some in-line documentation but there is no comprehensive documentation at the beginning of every program code which says what that program intends to do, who wrote that program, for what purpose it was written etc. These may appear to be trivial things from a technical point of view but as I have said again and again these are as important and if not more important than the actual C++ code that you write. Without that the program is not very meaningful. Please note the acknowledgments. These are the kind of acknowledgments that are to be written. Particularly as you grow more familiar with programming and write larger programs, you will be using code which others have written. In fact, the best programmers are those who do very little coding themselves. They should be able to use the code that has been written by others effectively. And whenever you, because reinventing the field is far more costlier option in time and efforts, not required. However, wherever you use anybody's code, you must acknowledge that code and use it only if that code is in open source. Otherwise, if it is intellectually protected property such as non-open source code and if you lay your hands on it, you must seek the permission of the original author who holds the intellectual property to include that code in your program. There are variety of software licenses that exist in the world and sometimes should become familiar with the plethora of licenses even in the open source. It is important to reflect on the intellectual property being a valuable asset and its use without permission is akin to a criminal theft. There is no other word for that. In fact, Indian laws actually are quite strict on the intellectual property violation. This is the inline documentation of the purpose or objective of this program. So I am saying class underscore set. This is a set of programs to illustrate the definition and use of C++ classes. This set has three versions and this file contains the second. The version which we saw last time is actually version one. So I have called it set one underscore editor dot cpp. I will put that up on the model so you can see that version, this version and the third version which I will briefly talk about, but I will leave it to you to attempt its implementation and then I will put it up on the model. So notice how we build on the documentation that we had seen earlier. We say we continue to use the two stack model for editing. The stack class and functions are used in this set. Additionally, the editor is implemented as another class and the member functions of this editor class carry out certain predefined editor commands explained below. You remember I had said that I should be able to get commands from the keyboard such as insert this word, move cursor to 20 position or delete five characters, et cetera, et cetera. It is not adequate that you and I understand in our minds what those commands are. It is important to articulate the command format so that when we write the program, we would write correctly. Many times you tend to first write the program and then write commands of this kind. This is all right when you are under an exam tension or a submission tension, but this is perfectly incorrect if you are doing a project. So the way the project should progress is anybody who is writing any program should first write at least 20 lines of command for that program, at least before you write the first line of code, before you write int main, before you write include, before you write anything. That should be the method that you should follow. So here I have decided on the commands. This is my decision. Like I am doing a project like you are. Somebody has asked me to define editor classes or how to do editing. So I have assumed that I will, M followed by a number will mean move cursor to the specified position. I followed by a word will mean insert a word. Now I am simplifying the issue by saying that the word to be added should not contain a blank. Why? I am going to read the command input from the keyboard and in keyboard I can read a text string provided there is no space. Remember if I declared an array name of characters and if I read name as C in and if name had two parts first and second name, I could not read the two parts because after the first blank the C in operation will terminate. We shall see later in further versions of editor how do we handle such larger string to be inserted not just a single word. But for simplicity this particular version implements I followed by add word as insert the word to the left of cursor. D followed by a number will delete characters to the left of cursor and finally when I finish everything I will say Q which means quit editing is over and edited text is to be written to a file. The commands and their parameters are read from the keyboard. The main program illustrates the editing by using a text file and some sample edit commands. So this is the documentation. Now this is the set to partial implementation. This is not the complete implementation. I am now describing the classes for editor which are required. What am I required to do in my editing? I need to move cursor here and there. I need to insert a word at the cursor. I need to delete some characters and I need to quit. So I will have to obviously have editor functions which will implement these things. Here is the editor class that we are trying to define. I am defining size as 10,000, text size as 9,990. Why 9,999? Because when I read a text to be edited it is not necessary that there should be a backslash 0 at the end of that text. I might want to treat the entire text as a logical character string in which case I must have a place to insert backslash 0 myself and therefore I am keeping that one position extra there. It is not necessary but this is just a precaution that you can take. As we shall see as long as the number of characters are much less than 10,000 it is okay. Incidentally there is no reason to define this as 10,000. It could be 100,000. It could be 1 million. In fact the maximum text that you can edit at one time would be limited in this implementation by the main memory that can be allocated to a single character array. And if you can define two character arrays like that, one to hold the input text and another to hold the output text after editing, and a million character array is no big deal these days on most of the machines. And a million character is a lot of text. You can in fact read the entire file of text to be edited, push it into the character string buffer and then apply the editor commands to this. Notice the way editor class itself is defined. So these are some hash defines include IO stream, include C string. The editor class is not being defined as a separate file. It is being defined as part of my main program. Just like last time we defined the stack class as part of our main program but included the class definition at the beginning of main program after the include statement. We are going to do the same thing with the editor class. This will be followed by editor class definition, the class member function definitions and then the main problem. However the class stack has been implemented through a separate file. Observe that I have hash includes stack.h. So this will preprocessor of the C++ compiler will first read this file. Notice the difference. These are in less than, better than sign whereas this one is in double quotes. This typically means that this file is to be found in the current working directory where you are. So if I keep that in the current working directory that file will automatically be included by the preprocessor. We now proceed to define the editor class and its member functions. You notice that whatever code we wrote for the main program last time, essentially you are factoring out some of that code from the main program and putting it into implementation of some of the member functions of the editor class. To begin with the private data for the editor class would be two stacks, the left stack and right stack. Since we have defined the class stack we can say stack, stack, r stack. This defines the two stacks. Notice also that the moment I put stack, stack, r stack under the class editor and by implication this is private data member because there is no public word here. It means that in my main program I have no access to stacks. Last time we saw in our main program we had access to stack but we could not access the array which was implementing the stack. The array was hidden inside the stack class and I could only say pop or push. I am going one step further in the abstraction and hiding unnecessary details from the main program. I am saying that the main program itself will have nothing to do with these stacks because these are private members of the editor class. So consequently in my main program I will deal only with the text, commands and if I want to invoke anything with these stacks I will have to do that only through the editor member functions as we shall see them evolving. I additionally have a text of text size, size and a word which is to be added if you recall we had said insert a word. So we expect the word not to be a very large word. We assign ten character positions for the word. I have size and I have cursor. So notice even the cursor which was the mainstay of my editing conceptualization is now hidden inside the class editor. In my main program I have no cursor. So the physical variable which denotes the cursor now becomes part of the editor class. I will naturally have some other value denoting the cursor position in my mind which I will have to pass to a member function which will now handle the cursor because member functions have access to all the private data. So this is how I define the private data and in public I define all the interface functions. So for example there is an init function which I have defined which takes the text array from me in which I have the text to be edited. It takes its size. Notice that this time the array is passed as a pointer which is perfectly fine we shall see how to handle that. Then I have int moveCursor followed by int pass. So whatever position I give the editor member function is supposed to move the cursor to that point. What does moving the cursor mean? Well move the characters by popping them off the one stack and pushing them onto another stack. And since stacks are private members the editor class member function can do that well. But I can't do that in my main program. If I want to move the cursor I will have to depend upon moveCursor function. And actually this dependence is good because I am completely removed from the details of the implementation of this function. Similarly I have text insert so I have a character star add word and int length of that add word. And finally I have int text delete int num. So this is a number which I give so many characters will be deleted. I have added as a good measure a function called int text display. Hopefully particularly when I am debugging I would like to display the text. As we shall see later this function is not very useful here because it will have to display by calling the stack. And in order to display anything you can only display contents of a stack by popping the characters out. The moment you pop the characters out the cursor effectively loses its significance. So if you want to really implement it as a member function of the editor class to display a text. What will you display? You may display the following. You may say cursor position is this. Left of cursor these are the characters right of cursor these are the characters. Now how will you display the characters to the left of cursor and right of cursor? Only by invoking a stack and by pop comma. But if you pop characters then no further editing is possible. In fact you will lose the characters which are popped unless you store them. So you may have to pop and you may have to push them back. That is why this display is not a very meaningful function here. I have just added that to show that initially when I think of member functions the member function names and the functionality will not fall from the sky. Notice that each of these functions move cursor, text insert, text delete emanate from my decision to have those commands for editing. I for insert, M for move, D for delete. Additionally I know I will have to initialize something with the text therefore I have an init command. There are actually two member functions. One is called the creator and the destructor. The other is called the destructor. This creator and destructor functions usually do the initialization and destruction of the objects. Particularly when the objects are dynamically created we shall discuss those aspects at a later time. Now the member functions. So int editor colon colon init again remember the way in which the member functions are to be written. You have to write the type of the value that is written by the function. You have to write the class name followed by two colons indicating that this function init is a member of the class editor because init could be the name of any function either in your own program or a part of some other class. So you always have to distinguish by that. And then you have this care start text int size. What am I supposed to do here? I am supposed to initialize the editor with given text. So I say int i care start p. I check whether the size is greater than text size. If that is I can't edit that text. So I simply give a fact that text is larger than my capacity and I return one. Notice I could have written this function as void editor colon colon init as we had implemented the stack functions with void type. Here I am deliberately including int type as the return value. Ordinarily the initialization is not supposed to return anything. It's supposed to take the text and push it in the stacks. But in case it can't push anything in the stack, how will it let me know that the editor is unable to edit anything. Now this is my mechanism. If everything goes well I will return zero, otherwise I will return one. Exactly the same convention that we have been following in our main program. I could then whenever in my main program I invoke say whatever editor class object I create say myedit. If I say myedit.init, I could invoke it in standalone key way in which case it will just do its job. Or I could say some return equal to myedit.init and examine the return value. If the return value is zero, everything is fine. If the return value is not zero, I can give the error messages instead of giving this or in addition to giving these messages. This is the implementation that if that is not so, then whatever is the pointer I got from the main program, I will assign it to my local pointer p and for i equal to zero to size minus one, I will keep putting the value pointed to by p into ith element of text. Notice the use of pointers. Star p plus plus means first use the star p which is the contents of the p which will be the 0th element initially of the given array. I will push it into the 0th element here. Then I will take the next element because p plus plus will mean point to the next element and so on. When I can complete this loop, my text array would have been initialized. All that I have done is I have copied laboriously the text which was given to me by the local text array. Why did I do that? The correct answer is it is not necessary to do that but it so happens that I had written this particular code earlier if you recall. So I am retaining that code. This is slightly inefficient because generally when arrays are passed as parameters, I should use the original array itself and not make a copy but that's okay. This program is still work correct. Now I initialize the two stacks, l-stack.init and r-stack.init. Notice that here l-stack is the name of the object of the type stack class and therefore the function pop, push, init, etc. have to be qualified by the particular object name. So l-stack init will initialize l-stack, r-stack init will initialize r-stack and after initializing these two since I assume that the initial text is such that the cursor is at the beginning of that text. Therefore I have to push all the characters onto the right stack and that is what I am doing here. For i equal to size minus 1 to i greater than equal to 0 I push r-stack.push text i. So if I have character a, b, c, d I want to push d at the bottom then c then b then a and that is why I push it in the reverse direction. So I take the last character of text and push it first then I take the last but one character of text i minus minus, push it next and so on till all elements are pushed. Notice that this could very well be achieved instead of saying text i here I could have said star p plus plus it would have had the same effect. I have initialized cursor to 0 and return 0 so this will initialize my editor that means the text which has been given to me for editing has been pushed into the right stack properly cursor has been said. Notice again cursor, r-stack, r-stack, etc are visible only to the member functions of the editor class they are not visible outside. Here is a move cursor command exactly the same code that we had used in our main program earlier I have the position which has been given to me I collect it in a local variable newCursor I check whether the newCursor is less than 0 or newCursor is greater than size that means somebody is asking me to go beyond the file boundaries or text boundaries then I say cannot move cursor beyond this please give another cursor position in n and I return 1 here and you will notice this also is defined as int so that I can examine the return value and now the standard thing the newCursor could be greater than the originalCursor or it could be smaller than the originalCursor if the newCursor is greater than the originalCursor then I have to shift characters to left stack from right stack because the cursor is moving this way if it is smaller the cursor is moving that way so characters have to pop out of the left stack and go into the right stack accordingly I will implement this I will again use s-stack.push, r-stack.pop or r-stack.push, s-stack.pop extremely simple code really which will achieve whatever we want and we must not forget that after moving the cursor the cursor variable which is part of the editor class private data element must be reset properly otherwise if the cursor is at the old position I will have a problem so I am setting cursor is equal to newCursor there is a int editor text insert comma this will be used to insert my text again I have a add word and length again I am doing the same thing I define char star p I assign p to add word I move the contents of add word into word all this is not necessary the only reason this is happening is when I perhaps wrote the original code I probably used the word word now I have defined add word as a parameter the moment I do that I will have to either transfer it or change this code so this again I will say it is unnecessary but this is the kind of code that might get written first time when we write the functions because the functions of a member class will not fall from a sky you would usually implement the whole thing as a main program as a trial and you will extract code from your main program and push it into the function code so this is likely to happen the first time but when you clean up the mess you can actually write much optimal code here I am transferring the contents of add word here I am not sure that the add word will have a backslash 0 at the end I cannot assume that somebody has said insert these six characters the six characters may not be followed by a seventh backslash 0 so I will put one myself and that is the reason why I will define the word as 10 and I must specify in my condition that the given characters if it is not a string must not contain more than nine characters so that I have this position in any case I calculate the length of that word I define the new cursor to be equal to cursor plus length because observe that this word is going to get inserted into the left cursor so the new cursor will have to be defined appropriately and then I simply check if new cursor is greater than size I say I cannot accommodate word size so suppose the left tag is almost full to the brim the cursor is already on the other side there is a position for place for two characters it is not full yet so no report has been given to me by any mishap but if somebody now I say insert five characters at these positions I cannot that is why the new cursor is computed before this so I know where the new cursor is if that is greater than the size I will say sorry space only for so many size minus cursor characters and I will return one here otherwise I will push the characters from word to the left stack again for I equal to zero to length minus one I will say s tag dot push word I so I have character gets pushed remember on the left stack the words are the characters are pushed in the order in which they appear in the text first character first then next then next on the right stack on the other hand they get pushed in the reverse order so that together all of them make a sense as a continuous string notice that I must now push the value of new cursor to cursor because it is the cursor which is the determining variable for the position and of course I put the size remember the size of the text originally had some value thousand characters two thousand four hundred fifty characters whatever every time I insert or delete not only the cursor position will change but the size will also change and I must appropriately accommodate that only when I move the cursor there is no insertional deletion of characters then only the cursor value will change to new cursor whatever it is but the size of the text will remain whatever it was earlier so after every operation of insertion and deletion I must modify the size item here is a text delete this is very simple I have the numbers to be deleted I now set length equal to number again a stupid part of code because I could have used num itself directly I am assuming that I have written this code earlier in terms of length rather than changing my code I am simply saying length equal to num I say this is stupid because it results in efficient code but first time when you write it this kind of thing is perfectly all right if you have written a code, written that simply reassign values with parameters to the local thing just remember if you have to send back something then you have to worry about it but otherwise this is all and what do I do now? I check please note I am deleting characters so that means whatever are the characters on my left stack some of them will vanish now if I am supposed to delete 10 characters and if there are only 5 characters left in the stack I have 2 options I will say you ask me to delete 10 I have only 5, I have deleted all 5 but better would be to go back and tell that person that look you are probably under the wrong impression you may think that cursor is somewhere else and you are deleting 10 characters but that is not so there are no more 10 characters left in the so this is the approach that I have taken in this implementation says cannot delete up to given position and without changing anything I end again return 1 which can be used by the return value if I so want it having checked for the conditionalities I now proceed to delete actually and the deletion is very simple because on the left hand side I know how many characters are to be deleted in the left stack length number of character so for I equal to 0 to I less than length which will execute this iteration length time I simply pop from L stack obviously the pop function will return a character so I will collect it into CH which I have defined as a character but it is not important because it is getting deleted so I can simply forget it so I pop, pop, pop so many times and that's it but at the end after popping I know that my size has reduced I know my cursor has moved so I will set cursor to new cursor and I will set size to size minus length size minus length always keep track of cursor and size I have not implemented the display function but you can write it if you want to and you can examine how difficult it is because displaying contents of a editor it is not the original text array that you can display because that will still continue to contain whatever text you had we have not changed it what we are changing essentially are the contents of the two stacks so displayed edit, text being edited is not a easy command to implement in an editor class how will you implement it? you can't say that go to that stack and show me the seven characters which are there what you will have to do is pop characters from a stack and see out them but if you pop they are gone so you will have to pop them into an array display them and push them back in the same sequence back into there too much of Godagiri it's not a good way to implement it you might do better to have a display stack command because when you operate within the realm of the class stack remember you have access to the array itself which is implementing that stack so without pushing or popping you can directly display from that array whatever contents you want to display of course that is not a standard function for a stack implementation and therefore that has not been implemented here but in case you want to display why would you like to display that? you would like to display that only during the testing phase once you have tested that your editor works perfectly there is no need there is no need to display anything while you are editing except what string is being inserted what characters are being deleted if you want to display which characters are deleted you could add here ch equal to l stack dot pop see out ch it will display that correct or better still c out l stack dot pop so it will simultaneously pop and push it on to c out pop and push it on to c out you can choose what to do I would recommend doing this and followed by c out because if you don't want an output to be cluttering your interaction with the editor through commands you may simply blank out that c out position if the pop itself happens in c out then you remove c out pop also goes so that is one of the ways in which you can implement there are multiple ways in which you can handle now we write the main program what have we achieved so far? first we saw removal of all implementation code as well as independent variables required for implementation of stack in a class called stack log so that means we are no more burdened with the internal implementation of stack that would be an object which itself I was using in my main program last time now I am going one level further in hiding unnecessary details by defining editor class I will define some objects of the editor class here it is editor my edit my edit now is an object which comes into existence when I define my edit editor my edit what this means is that my edit now will have the complete internal information that is private to the editor class and I can interact with my edit using the interface function that I have so defined but all the definition of interface functions how to move cursor, how to insert, how to delete again becomes local and private to this object my edit my main program will therefore be that much cleaner notice that such implementation permits the stack class to be used by anybody for something which is a non-editing thing for a different thing somebody needs a character stack one can use it if somebody needs an editor not to implement these command line editing functions but maybe something else could use my editor class this is the kind of generalization that you can perform and this is the kind of reuse an object oriented programming permits you to do let us very quickly look at the main program I define the editor object my edit I define two arrays text and out text this 5000 size is a remnant of my last program notice that this is not necessary to have 5000 as the size what is the maximum size I can have capital S I Z E or capital text size that I have defined earlier it is better to define that using a hash define parameter said once I change that everything changing otherwise you may even make your stack worth 1 million characters you may make your editor class private element that text also 1 million but in the main program you can read and handle only 5000 characters it doesn't make sense now this does not happen when you are writing a single monolithic program because you are aware of all the different data structures and you will define all the necessary arrays with the appropriate size but when you isolated implementation when stack is implemented in stack.h tomorrow editor class itself which is now part of this main program gets implemented as editor.h then I won't know what those implementations are but it may be better therefore to use certain standard macros which are defined which gets replaced at the compile I have additionally an old cursor, new cursor text length, out text length and an integer variable I for my indexing all that I have tried to do in this particular main program this particular main program does not handle the commands that I listed at the beginning that will come in the proper version 2 or the set 2 of this set of program this is you can say set 2 trial what am I doing in this trial I am trying to do exactly what I did in my first main program namely take some text take some arbitrary word and see whether you can insert it at the designated place and see whether you can delete some characters basically I am testing each of the interface functions by using exactly what I did this kind of testing during the development process is an extremely useful thing because now I will know if this program works correctly that means the implemented member functions of the class editor are working correctly and then I can write later on replacing the part of this main program by a program which will take edit commands from me on the terminal and execute them appropriately so here is a sample text for editing I am just saying string copy a b c d a up to z I have calculated the text length I output the text to be edited so and so and its length is so and so and I put an ad word which I want to insert at some position as opening curly bracket 1 2 3 4 closing curly bracket notice that when I use either a string copy or I assign during the initialization the backslash 0 is automatically inserted by c++ at the end of this so I am equipped with a sample text to be edited I am equipped with a word which I want to insert and conceptually I know I am doing exactly the same thing that I did while developing my previous program so I am very clear in mind what code I need to written so what I need to do if you recall what we were doing there is we were initializing the editor arrays which means we are putting them in the right stack and so on but that job is now done by the init function so I have to call the init function then we are moving cursor to some position 10 then we are inserting this word then we are moving cursor somewhere else and then we were deleting some characters from that position observe how easy that implementation now becomes so those particular actions that we were taking earlier you will remember that for each of these actions we had tons of code all of that code has now become part of the editor class object and the editor class object that I have instantiated is called myedit so whenever I say myedit.init this object will be initialized with the text and text length that is my original text in this particular case the text is an arbitrary string a to z and the length is 26 but in actual practice this text could contain thousands of characters as I said next I want to move cursor to 10th position I simply say myedit.moveCursor 10 that's it it will move to 10th position next I want to insert the additional word I will say myedit.textInsert add word, string length add word notice that in this case I had explicitly calculated text length by invoking string length function elsewhere but it is not required I am going to pass this parameter as a read only parameter anyway so I can calculate that as part of the parameter definition itself next I move cursor to 0th position then I move cursor to 3rd position I delete text I delete 3 characters at that position and again I move cursor to 0 why 0? because when I move it to 0 the idea was that if the display function which I had in mind would assume that the cursor is at 0 and I will display everything from the right stack so while displaying instead of remembering what I have popped I will keep pushing it onto the left stack and display every character and at the end I will again pop from left and push it onto right however I have not implemented that function because by writing the program I realize that it's hell of a lot of work please remember if I am editing 1 million characters just to display characters I will have to push these 1 million characters out of one stack onto this and push them back but internally I know the stacks are implemented as an array so instead of the display text being a function as the editor class or I could have a display text function in the editor class but if I could additionally write a display stack function in my stack dot edge and from editor class I invoke that function then that function need not do any push or pop because that function is a part of the stack class will have access to the internal array which implements that stack but that is a matter of choice the question is in front of you how do you read a complete file how do you read data from file you first of all open a file for reading it has some name and let's say you have opened that file for reading with the name as this will be the definition so you can actually open the file how will you read we have had two ways of reading we said if it's a text file we can read it character by character line by line or string by string here we want to read all characters so we can read character by character what we will have to do we will have to set up an iteration which will start with whatever 0 it will read one character push it into the array text here you want to read that and go to read the next character at some point the file read operation will return no character to be read end of file so we have already seen how to test for end of file we will say function end of file so if f e o f is not in bracket f p is not true then we keep reading otherwise we stop reading remembering that we are planning to do editing for complete text that exists inside the file I know that I will always have to read the entire file can I not read the entire file in one shot because there is a direct access mechanism available to me even though if I open this file for read and even though it is not a binary file remember I can directly access any position in the file and read as many bytes as you specify to recall if this is your file and let's say this was the last position how is the last position named in file system a position pointer called set end how is the first position named how is any position elsewhere named current position are these pointers to these positions you have forgotten these are not pointers to these positions in fact these are flags used by the file system the set begin as a pointer value as a value 0 this has a value 1 and this has a value 2 these don't indicate the position the position is indicated by some kind of a long int say pos all that these flags indicate is I could stipulate the position with respect with relative to beginning relative to the current position related to it originally I have no clue how many bytes does the file have if I knew that let's say file has file size bytes suppose I knew the value of file size let's say 1 million or 2,20,000 or something then I know how to read all the bytes how would I do that I would do that by saying fread you remember the fread command what is the first parameter of fread fread does not have file pointer as the first sorry the string in which you want to store the bytes read from the file so let us call this text because we want to read everything in the text tab then we want to give the size of the block that is to be read and number of blocks are to be read I can read 4 byte blocks 20 of them I can read one block of the entire size or I can read one byte block number of the size number of times so if I say for example read fsize bytes block just one unit and do that from set what is the begin is just begin is it so seek set seeker and seekend and therefore I should say in fact if I don't say anything it will take it from the beginning I presume but I'll just say seek set the issue is I can read the entire file but how do I find the file size any idea how will I find the file size ah he is suggesting a method which we just thought about for displaying text in an editor how do you display a text in the editor you pop from the start and keep doing it so what he is saying is first you read every character keep counting whenever the file ends what are the total number of characters you have read that is now the size and now you again read the entire block I have already read one if in order to calculate the size of a file if I have to read characters and count them then I have read them anyway which means I would have set up an iteration etc etc so there are two additional functions which you people don't seem to have read so I'll repeat again if you recall we had said that there is a library called C S T D I O the C S T D I O has all the file functions these are the classical file functions the additional file functions which are part of the C object library which is the standard template library of C++ I had said that we are using some of these file functions in C S T D I O and there are many others which you should read for the sake of usage and at least for the sake of curiosity neither seem to have happened so let me tell you two functions one is called F T L F P this F T L F P tells you where the current pointer is of course your current pointer when you begin the file is at the beginning so that is not very much helpful because it will return a position 0 if the current pointer is on the seventh byte it will return 7 if it is 25th byte it will return 25 but we know how to set the pointer at the end how do we do that F C we use this F C before reading anything remember we saw that if we want to read let's say 24 byte from 100th position we have to set the pointer to 100th position what we are doing we are giving the value 100 relative to beginning as we know we can give a displacement relative to either beginning or the current position or at the end suppose I give a position relative to end and that position displacement I give as 0 will my pointer not be pointing to the last element so if I say F C what is the format for F C file pointer number of bytes displacement relative to whatever point I will say that is 0 and the F C function will set an internal position pointer by this command at the end because I am saying 0 with respect to end it can position is beyond them however it does not return a value or does it it doesn't return a value but F tell F P returns the value of the current position so if now I said so this is the use of this F tell function that I don't I don't read please understand the difference between reading every character and counting versus simply internally positioning the file how does operating system do that because operating system actually knows the size of the file whenever you create a file edit a file insert characters whatever whatever and whenever you save a file at that time the operating system retains the name of the file the pass pointers whether it is directory what type etcetera and the size of file invites this information is known to the operating so when I say that is how it can move things relative to the end otherwise it has to know the end itself operating system knows it and this is my way of doing it I will tell the operating system please seek to the end of the file and then say tell me where you are it will tell me which I capture in F size now you will agree that I can do an F read here is that okay why is not okay F read always works from the current file position the current file position is at the end of the file it will try to read 1 million characters from that point it won't find any so I have to bring it back here it can be achieved by again doing an F seek F P 0 with respect to write at the beginning all alternately there is another command it says rewind F P you are familiar with rewinding a tape musical tape is rewind so you press the rewind button it will come at the beginning of course the rewind operation is much faster because it has to set the point of so notice how neatly we can read the entire file without any iteration without anything without doing any work please remember this F read is much faster than iteratively reading one character why why is this much faster than iteratively reading one character after all the F read will have to put everything into the text whatever we are saying say one byte F size time or F size bytes one time whatever it is why is this faster than using an iteration two reasons every F read command actually makes the operating system go and physically read from the disk please remember the disk is perpetually rotating disk is not waiting for you your file and the various bytes are located at different places based on how the file has been organized by the operating system if you read one character and assume that all bytes have been logically written sequentially on the disk for efficiency say if you read one character the operating system will read one character and it will give it to you in the iteration you read next character the operating system will again go to the disk by this time the disk has moved so it will have to wait for half a rotation that would be very stupid to read one million characters like that so operating system does a smarter thing whenever you see read one character it will actually read one block and you keep it inside its buffer next time when you say read I say I have read that block so it will give you the second character it will give you the third character block is typically 512 bytes by default it could be 1 kilobyte, 2 kilobyte, 4 kilobyte but if you are reading one million characters several blocks will have to be read independent it is possible that the blocks were written sequentially on the disk so if you read all of them in one shot in one single rotation of the disk you could read the entire file whereas individual reading will take much longer in particular whenever you are doing read write operations on disk you might want to be slightly more careful about efficiency then you have been earlier where you could easily handle moving things from this stack to that stack or this array to that array etc ok now how do you handle the editing commands so I have read the text I have initialized the editor now suppose I say move 10 so the editing command can be handled by setting up an iteration an infinite iteration practically so do why what I will do see in character edit command I will define edit command as a car that edit command could be m i d or q I could use a switch statement switch based on that edit command character case q quit case i read the word again by cn and insert case m invoke move cursor case d delete and if this sequence I operate I can implement it so my entire main program will consist of reading from a file initializing my edit object and then writing a loop continuously keep getting editing commands from me and executing don't you agree that the program will become much neater and conceptually cleaner because your program for editing is actually doing editing it is a command editor you can call it a command editor program it uses two classes the main class is editor class which implements move cursor etc etc which in turn uses the class for stack this is the right way of evolving your programs later on we shall see how we could put these files not only these information about class implementation not only in different text files but we could even compile these and kept them separate indeed that is what we mean when we say we use cstdlib or cstdio or cstring or whatever whatever any library where we use the functions the prototype functions are defined are made available to us from those libraries while compiling the actual code which is translated already pre-compiled is linked with your program during the link time that is how the system works