 Last time we have started discussion on the notion of classes in C plus plus. We have briefly seen some facilities available in the string class, which is so different than the character arrays in which we were handling strings earlier. To distinguish, we have let us say square s t say 100 and we have string s t 1 let us say. So, the difference between these two types, the string class defines us an abstract data type, which can store character strings, but how it is internally stored is not known to us, need not be known to us. The way we handle character strings is for example, this array s t let us say four characters a b c d are stored, then the zeroth element will have a, first element will have b, third element will have c and the fourth element will have a backslash 0. Essentially, we call these null terminated strings and we have seen examples of how to handle such strings. We know that there are functions for example, which can permit us to manipulate strings like this and for that the library which we include is called c string. So, this is the library which we use in order to manipulate strings here. Notice that when we have an array, we can initialize an array, even a character array can be initialized while it is modified. So, I could very well say care s t 100 equal to some string, in which case automatically the characters of that string will be put in element 0, 1, 2, 3, 4 and the last element after the last character will have backslash 0 inserted automatically. That happens when I initialize a string. However, in case of a string s t 1, a string type definition not only I can initialize string s t 1 by saying string s t 1 equal to something, while I define that string type object, I can also say something like s t 1 equal to notice that this string constant is actually a c type string constant. In the sense that the constant itself is implemented inside the c compiler, it maintains it as a null terminated string. But when I make an assignment of this type, it is automatically converted to the string class object type and assign. I cannot make such an assignment to this string. Such an assignment is not possible. So, if I want to assign a value say a b c d, I will have to use a c string type function, which is let us say string copy. You will remember that string copy permits us to copy a string into any pointer to character string array. The pointer to character string array here will be s t itself and the string which I want to copy into it could be written as a b c d. So, this is a valid statement s t r c p y s t comma this, because I am using a built in function which is provided by the c string library to copy this string as a null terminated string into this array s t. Observe that the name of the array is the pointer to the 0th array and therefore, s t r c p y which requires a pointer is the first parameter correctly copy this and I will get this. Whereas, in case of a string class, I can directly handle this. We have also seen how we could use an overloaded operator, a plus operator which normally represents addition of two values typically numerical values can be used because it has been so defined in the string library. The string class library of c plus plus has redefined plus symbol to mean concatenation of string. So, we can concatenate one string with another. We can append one string to another, etcetera, etcetera. And therefore, the hassle of handling array elements for locating individual characters, etcetera, moving them around that hassle is removed significantly and programming is simpler. We will see one more example. The example is given a number. I want to write it in words. So, for example, I want to write suppose I give a number 73 as input, then I would like to write a program which will produce 73 as the result. Suppose I give a value only 20, it should produce only 20, not 20 0. If I given a single digit number, say 3, it should produce 3. Now, the way I would ordinarily handle this in using the null terminated string, null terminated character list in an array would be to store different possible nomenclatures that occur in the conversion of number into words such as 20, 30, 40, 50, 60, 70, 0. 0 will not occur, of course, because I will never write a word 0 anywhere. 1, 2, 3, 4, 5, 6, 7, 8, 9. I can store these in different arrays and then depending upon what is the first digit, what is the second digit, I can decide what I have to print and then concatenate one array, say representing value 70 with another array, value, representing value 3 if the second digit is 3. But that is not a very straightforward process. I can of course use function calls such as string copy as we have just seen. There are additional functions which perform operations of appending and so on. But what I wish to discuss today is how if I use the string class rather than array of characters to represent such strings, then how the programming becomes relatively simplified. So, here is a program which converts any given two digit number into words. You can extend it. This program itself I have based on a program which was which is available on a website called Dany Web. Dany Web incidentally is a very interesting website. It gives variety of such sample code snippets which you can see which implement some interesting problem solutions. So, the way this program is written is I use include string which is the string class. This is a new type of function that we have not seen. Ordinarily we would write void some function name parameters followed by the core for the function. That is what we will write before the end. And the moment I say inline it means something slightly different. What it means is that wherever the function reference appears inside my main program that function reference will be replaced by this complete code. So, effectively the calculation of the function will be replicated physically as many times as I refer to this function. It has a result of increasing the code size because it will translate that many times and that will create that much code. However, the advantage is it will run much faster. Let us see why is the difference of in the execution times. Whenever I write a function here say 1, 2, 3, 4, 5, 6 functions and whenever I invoke a function from my main program remember that functions are essentially compiled separately and they are linked. Now whenever you invoke a function you are leaving whatever you are doing. So, whatever you are doing has some context. It has some open files, it has pointers, it has whatever whatever all these things the operating system preserves somewhere in the memory only in a separate part. It is called the program stack. We shall see the stack in a short while today itself but it preserves all that. So, let us say there are 20 values which need to be preserved before I go to that function because once I go to that function after executing the function I have to come back to this program and continue from where I was. So, where I was should be remembered, what were the values I was dealing should be remembered etcetera etcetera. These 15, 20 values will be remembered that means they will be put into a memory set of memory locations then I will go to that function, execute that function and when I come back I will have to restore all those stored memory locations into the program space proper. Consequently a function call is a costly business. Of course, since everything takes nanoseconds and microseconds these days we do not realize it but if something has to be done which will require an overhead of 100 operations I would rather avoid it. The inline function usage avoids it just as hash defined and other things the preprocessor will ensure that everything is actually physically replaced in the characters that you write in your program before it is compiled. Something similar happens concept. However, if the function itself is very large say 100 line function then writing it inline has no sense because if I am calling it say five different places every place 100 lines of code will get compiled and that will increase the code size very much at a very minimal advantage of the overhead. So, it is to be a judicious choice generally very short functions are written as inline functions. Let us look at what this inline function does. It says append. So, apparently it is appending something to something. Let us see what are the parameters. The first parameter is string and LHS. So, left hand side string. The second parameter is string const and RHS. String const and constant RHS means whatever the RHS string comes here cannot be changed will not be changed. Whereas, there is no such const here that means this will probably change and indeed that will change because the value that will pass back will be through this pointer. So, effectively whatever is the LHS string will be appended by RHS that is the objective. So, I am giving an LHS string, I am giving an RHS string constant. I have I am also giving a string constant and set equal to blank. So, there is one blank here. This blank will be utilized whenever I have to print say 20 followed by 3 10 or 70 followed by 9 etc. just as we saw how to print it in words. So, this is a single function and let us see what it does. So, it says LHS plus equal to notice that LHS is string type. So, when I say LHS plus equal to it means LHS equal to LHS plus and plus means append the string. So, append to left hand side what am I appending? Well, the appending itself is a complex decision. It says check whether LHS dot empty is true. That means if left hand side is empty that means there is nothing there already and RHS is empty. So, there is nothing on LHS nothing on RHS. So, what you appended? Nothing is to be appended to nothing. So, it says colon if this is true question mark operator this condition is tested either it will put the separator or it will put a null character is a null string nothing exists and this will be appended to RHS whatever is the RHS that comes in. You can execute some sample code to see I have shown you some sample examples here. In short this in line function will append any given RHS to an existing LHS suitably examining whether any one of these is bland or none. So, empty string this is another function which is available under string class it is not available under our normal character string a character array handling of strings. And remember that string is a class. So, every object of the class when it you want to operate upon that operate or that object using a function you have to represent the function called by putting a dot there. So, it is called a member function. So, you say LHS dot empty means invoke the empty function do whatever it does and come back here. But for this object LHS object let us look at the code itself in the main program series of constants are defined. Notice that the constants are defined using the array initialization concept that we are already familiar with except that if I wanted an array of words in conventional string I will have to use two dimensional arrays ok. One dimension is a different word another dimension actual characters of each word because each word has to be an array of characters only whereas in a string class every object is a string and therefore an array of strings which is defined here called digits. So, it contains notice what 0th element contains null first element contains 1 second element contains 2 3 4 5 6 7 8 9 notice how beautifully the index of this array is mapped to the digit which will represent the corresponding value. So, if the digit is 0 then nothing is to be printed. So, null characters string is indicated if digit is 1 it should print 1 2 3 4 5 6 7 8 9 before considering the teens consider the cons duplets. Duplets means 20 30 40 50 60 70 again here the 10 is not considered part of this. So, in the constant duplets that means it is a 2 digit number now 1 0 2 0 3 0 4 0 5 0 and 0 0 is also a number. So, consequently if the first digit of a duplet is 0 I award a null string there. The first digit of a duplet is 1 I award a null string here remember this is going to be used in the up end operation. So, does it mean that I do not take care of 10 no 10 is taken care of separately as we shall see later then if the digit is 2 it is 20 30 40 50 60 70 90 etcetera. Now, you come to special cases because all the teens have special name. So, 13 14 15 16 technically 11 and 12 are also teens because they have special names and in the process we are taking care of the value 10 itself. Notice again that the way this string constant teens is defined is that if the second digit is 0 1 2 3 4 5 6 7 8 9 of a number whose first digit is 11 then all of them are classified here as teens. So, 1 0 is 10 1 1 is 11 1 2 is 12 etcetera. So, if during the process of my analysis of given number I detect that the first digit is 1 then I utilize these teens depending upon whatever is the second digit I will append this to the growing string. Of course, if the number is in teens after 11 it is highly unlikely that you have 2 or 3 something like this will be a sort of standalone number. Although this program is being shown here to handle 2 digit numbers the Danny web original program actually converts triplets and if you use those triplets it is easily possible to extend that triplet program to print any large number millions billions whatever whatever. So, it is a interesting thing a practical usage of such a program would be that you have an amount to be written typically an amount in rupees is required to be written as number as well as in words right when you write a check for example you say 500 rupees but you also write inverse rupees 500 only. So, something of that sort this kind of program can be used this is the constant definitions I define num as int that is the number that you will give me I define string num inverse this is going to be my output. So, num is the given number num inverse is the output right now I begin by considering an output to be null that means nothing is there in that words. So, that means I am going to compose words in this string. So, this is not an array is a string type area and this is the program. So, see how simple the program becomes I get a number to be written in words I read in the number of course I do the standard validity check because I want it to be a two digit number. So, if the number is greater than 99 or the number is negative I say invalid number cannot translate sorry end of the pad and I just say return minus 1 whatever otherwise what do I do I have two digit number now I extract the left digit and write it how do I extract the left digit left L digit is equal to num percent 100 this is something which you have done many times, but some people are still confused about it please remember whatever be the number if I take modulo 100 then the remainder will be two digits what will be the value. So, is this correct relate get the left most digit sorry num percent 100 is a remnant of the original program where the number could be larger the number could have been three digit number in our case if we had just said num slash 10 it would have work proper the modulo operator has the same priority as slash star etcetera and therefore it will be executed left to right. So, bracket is not required this statement is correct however in the context of the problem that we are solving this statement is not required ok. So, I could have simply said num divided by 10 and I would still get the left most digit here is again I would like to stress the importance of writing some comments what I am doing would not have been so obvious had this comment not been there this comment says left most digits 10s players. So, I know I am trying to find out the left most digit this is not relevant for me because I have written the program I will remember, but this is relevant for somebody else who reads the program and perhaps has to modify. So, never ever particularly in your projects never ever write your programs without writing appropriate comments even for small things like this where something special that you have thought of is implement the right hand side digit of course is obtained by finding out a modulo 10. So, I have got the left digit and I have got the right digit and now it is a very simple matter to assemble the word look at how it is achieved. So, 10 is treated as a team for simpler logic duplet start at 20, 30, 40 and there is a single statement append num in words comma duplets L digits. So, it will take the left digit and it will go to the duplet please understand that I am going there with the left digit. So, the left digit element of duplet will be appended to num in words. Num in words is currently blank and left digit can be what? It can be 0, it can be 1, it can be 2, it can be 3, 5, 6, 7, 8, 9. Just look at what is there in the duplets. We go to slice back the duplet say null, null, 20, 30, 40, 56. So, if L digit is 0, nothing will be appended. If L digit is 1, nothing will be appended. But if L digit is 2, 20 will be appended. So, 20 will become the first word in the final string, or 30 or 40 depending upon the digit that you have got there. So, I go to this function append with L digit element of duplets to be appended to num in words. Num in words was null, I append now. So, what I will have? Either I will have blank, null string, or I will have 20, 30, 40, one of the things depending upon the num. Now, after that I have to add the next number. So, suppose the number was 24, then this would print, this would actually assemble 20 word here, and the four will have to be added sometime later. So, I am taking care of the right-hand side num. If the left digit is greater than 1, that means 2, 3, 4, 5, 6, 7, 8, 9, then the value is either 20 something, 30 something, 40 something, 50 something. That something could be 1, 2, 3, 4, 5, 6, 7, 8, 9, or that something could be 0. Observe that this digits place, I go with the digits R digit. The R digit 0th element was null. So, if the 20 followed by 0, it will not print 20, 0. It will just print 20. But if it is 1, then the first element will be picked up. So, it will print 1, 2, 3, 4, and it will append it to num inverse. So, basically num inverse is a string which I am treating as a growing string. Initially, there is nothing. Then it will have the equivalent of the first digit value inverse, and then it will have the second digit value. The problem with the second digit is this is all right when I have the first digit as greater than 1. But if the first digit is 1, then I have a problem. Because first digit is 1, there is no standard way of expanding. First digit is 1. In the first instance, I would not have put anything because first digit of duplex also is null. So, that means I have to pick up values from teens. And therefore, else, if l digit is greater than 1, append the right digitth element of digits to this. Otherwise, append to the num inverse depending upon left digit. Question mark, what is it? If l digit is not 0, then it is already not greater than 1. The l digit is not greater than 1. So, what that means? l digit is either 0 or 1. If it is 0, then that means a single digit number, I should append only a digit. That is what is appended. Digits are digit. But if that is 1, then I have to write 10, 11, 12, 13. I have to pick up from teens. So, see how simple this question mark operator says. Either pick up from the teens array or pick up from the digits array depending upon whatever is relevant. Of course, if this code appears slightly complicated, you can simplify that. You can write if statements as many times as you want, as long as you get the correct append. It is not necessary to write the append function as an inline function there. You can actually keep writing code as you append. It is not necessary that you start with a null word and expand it by appending. You could have your own logic of implementing. But I am presenting this as a nice logic. Additionally, this illustrates that when I use string class objects, then I have my life much simplified because I don't have to worry about the backslash 0 at the end of a character string array and so on. All that is taken care of by the string object library of C Plasmas. So, is this clear? How you can print is a sample program. Those of you are interested in finding out how exactly the same task can be achieved by using conventional strings which are represented as null terminated character array. Then there are enough such sample programs available both on the web and in several not C plus plus books but C programming language books. The C programming language actually has the character string representation only as an array of characters terminated by null. And therefore, those functions and the methodology used there can of course be used to write a different kind of code. Only thing is this program becomes shorter, simpler and neat algorithm. Here is the output of convert. If I give a number as 95, it will print 95. If I give a number as 18, it will print just one word 18 because it is picking up from teens. If I give a single digit number 7, it will just print 7. And of course, if I give a large number, it will say number not valid etc. I will be putting this up as a sample program in the next week's handout in the moodle. So, those of you are interested can see that. We now move over to some more serious discussion on object-oriented programming. We have had an introduction to the notion of class and we know that objects of similar type belong to the same class and objects generally permit us to define the data values associated with the objects or their attributes as well as the functions which will operate upon those values. Here is a little more clarity on the characterization of object classes. There are three important features which actually characterize object-oriented programming. There are three names here. One is called encapsulation. Encapsulation is you know medical capsules, right? So, you have an external sort of small container inside with some powder etc. stuff that is called capsule. Encapsulation means binding things together and putting them in a container kind of. So, object-oriented programming permits encapsulation by letting us define a clear binding between data and code for an object together. So far, what we had seen were variables or structures of different types such as array etc. They described only how the data was to be reprinted, how values were to be reprinted, whether structure or array or whatever. However, an object has these kind of values and additionally an object has a behavior. It can operate upon these values to modify these values etc. And therefore, that behavior which is reprinted by the code for functions that we write, the functions and data are bound together in an object. Consequently, whenever you define a class, it is like defining an abstract type we have already seen. Lateral in our program when we define an object belonging to that class, that definition is called instantiating an object. I am creating an object. Now, this time when the object is created, it will not come alive only with the data element that I have described which characterize that object. It will come alive with all the functions which are designed as part of that class definition. Now, the data, all the functions which are defined for a class and therefore which automatically get defined for every object can either be private to the object or public. This is something which does not exist in conventional program. There is no notion of a data which is completely private to a particular object. There is no notion of a code being private to a particular object. Of course, we can simulate that situation in conventional programming by writing functions in a specific manner, declaring variables in a specific manner. There are scope rules which we have not yet discussed extensively, but we can limit the scope at which a certain variable or array is visible, etc. But that is artificial way of doing it. Object class definition does that automatically. So, it permits you in short to define data which is private to the object or even methods or functions which are private to the object. Additionally, it permits you to define public data and public methods or public functions. It is these public functions which incidentally act as an interface between you and the object. So, object can manipulate its own data. Private or public data is visible to that object itself, but all of it is not visible to you. What is visible to you is only the public part. The private part is accessible only to the object. Public part usually provides the interface to the rest of the code. This is encapsulation. I will very briefly describe two more characteristics, but the examples of those characteristics we shall see in later classes just for the sake of completeness. The second feature which characterizes object-oriented programming is called polymorphism. Polymorphism means something which morphs into different entities. It is like Aotar. So, Vishnu has a Ram Aotar, a Kurma Aotar, this Aotar, that. There are ten different kinds of things. Each one has a different characteristic. Similarly, you can define one interface which has multiple methods. So, for example, I define a function. Let's say absolute function. Usually you will define absolute function which may take an integer number, positive or negative, and return the absolute value of that. Now, how will you define such function? You will say integer, absolute, in bracket parameter, which is also integer, and then write a code, this greater than 0, not, et cetera, et cetera, return that. Now, suppose you want to find out an absolute value of a floating point number. What will you do because this function you have written which takes an integer parameter, returns an integer value. A floating point number, you will have to write another function. So, let us say you call it F, A, B, S, or a floating point absolute. Then there is a double, so D, A, B, S. Then there could be a long, so L, A, B, S. Now, this is nonsense. Would you not like a single function called A, B, S? But you would like to tell the C++ compiler that look, depending upon the type of parameter I send you, please invoke this definition, this definition, this definition or this definition. The correct definition should be invoked and the correct return value should be given back to. Now, this is an example of polymorphism. I will briefly describe a program which does precisely this, which permits you to define a A, B, S function in four different ways and yet call the same A, B, S function with different parameters. It is C++ compiler which will resolve which of these four definitions to be called. These four definitions of the same function is a clear-cut example of polymorphism. So, multiple authors. In short, one function can be used to access a general set of actions. It reduces complexity and simplifies program. The third equally important characteristic of object-oriented programming is called inheritance. Inheritance effectively means that I can classify my object classes. Remember, as I told you, we had considered a class called student, but a student could be further classified as student class, could be further classified as UG student, PG student, research scholar. Now, observe that many of the characteristics of object of any one of these three subclasses will be common. Everybody will have a roll number, everybody will have a hostel number, everybody will have a room number, but certain characteristics will be different. So, consequently, if I am able to say that, look, I have a student class, these are the basic properties. Now, I have a subclass called UG student, a subclass called PG student, a subclass called DD student. I do not have to now redefine for every subclass that there is a roll number, there is a name, there is a hostel number. These are the properties of the main class which get inherited by the subclasses. In conclusion, three important characterization of object-oriented programming, encapsulation, which actually not only binds data and code together, it permits certain private components to be hidden completely from rest of your program. So, it permits what is known as data hiding. Unwanted data is hidden. You do not have to know what is inside that object, much like that cell which we had drawn. We do not know what fluid is inside, what DNA is inside. We interact with that cell through messages and those messages are implemented through public functions. The second one, polymorphism, where the same name for the function could cause different implementations to be invoked depending upon different parameters. Makes life simpler in terms of reducing complex. And the third one, inheritance, where I can define one class and I can define subclasses such that properties of the main class are automatically inherited. So, when I define subclass with a fewer, let's say characteristics, some additional variable names, etc., and later on I declare an object of a subclass. That object will have not only those methods and that data, but it will inherit all the methods from the main class and that is an important attribute of object-oriented program. As I said, we shall be seeing a couple of examples in this class of class definition and usage, but polymorphism and inheritance are something that we will bring out in subsequent lectures through examples. Here is the example of a... Here is the format of the class definition. In C++, you say class followed by a class name, opening class, private data and function, then an excess-specifier colon data and functions, excess-specifier colon data and functions, like that bracket... Oh, curly bracket close, list of objects. In principle, this looks very similar to the format of defining a struct. How do you say struct? Some struct type name, followed by element, element, element, element, close, and if you want, you can define variables of that struct type name. Similarly, if you want, you can define objects of this class. However, this list is optional. Once you have defined this class class name, later on in your program, you can instantiate any object, define any object of this class by saying class name followed by an object. Just like you say int x, float y. Similarly, you can say class name z. Then z becomes an object of that class. So this list is optional. What are excess-specifiers? What are private data and functions? So this is obviously public data and functions. Let us look at an example. I am defining an employee class. I am trying to represent the minimal required information and its processing for an employee, namely the name and salary. You can extend such a definition easy. So see how it is defined. Using namespace std, I define a class for employee information, class employee, care name 60, float salary. Observe that after this, I am using the word public. What it means is that these are not public. So these are private. In fact, if you do not specify that something is public, then whatever you define in a class definition by default is private. What it means is in your entire program later, you will never ever be able to use the word name with 60 characters, character array, or the word salary, which is a floating one. You will never be able to use it. The property is intrinsic to an object. So suppose I am an object's employee. Say db fatak is my name and my salary is so and so. Now this salary and this name db fatak is contained in me in variables called name and salary. These variables cannot be accessed by you. But of course, the institute would like to get my name. The institute would like to assign a name to me as an object. The institute would like to change my salary occasionally at least, hopefully. Then that salary has to be put in. Somebody may inquire what is my salary, so I will have to tell that person. As an object, I will have to interact with someone else. Somebody says tell me your salary, I will have to tell the salary. So that outside or may not have access to my data structure. But there must be an interface through which I can take an action of reading my own data and giving it to that person. Now these actions are implemented through functions and private data is written like this. Let us look at the actions here. There are exactly four actions. Put name, get name, put salary, get salary. Put name, meaning some name is given by outsider. It has to be put inside this array. Get name means inside I have a name. I have to put it into the parameter, hopefully a character star parameter that I have got. And I have to put this name back so that somebody else can see what the value of my name. Same thing about salary, put salary, somebody wants to change my salary. So fartag is an object, fartag not put salary, 50,000 rupees. So then this put salary function, which will be written somewhere else, must take the given parameter salary and put it inside this salary. So you see I am in this one set, one shot I am defining all the private data and the prototypes of all the functions. Indeed in C++ in classes it is mandatory to define prototypes. In our programming so far what we have seen, I could define a function directly without defining a prototype. But since class definition will come not necessarily containing the function definition, the prototype definition is required and this ends the class definition. So in short then employee has two private elements name and salary and four public functions put name, get name, put salary, get salary. Here is the implementation of these. Notice that when I say public I have to say colon. Similarly if I say private, again I have to say private colon, care name, float salary etc. I have not said private because that is the default. Incidentally it is not mandatory that I must neatly collect private and public things separately. If for example later on I want to add some private elements here I can say private colon and then define additional private elements. I could define private functions, I could define private elements, whatever I am. Similarly I could define data also which is publicly known. If I so require in some case, in this case I don't need it. Let us see the implementation of these functions. The class definition or description will contain function prototypes but generally the implementation of the function has to be coded immediately following the function. So if that is the class definition I have given immediately later I will say void employee colon colon put name care star name. What is happening here? I am actually writing program for implementing the function put name. Put name will take one name from outside, the outside name I will give and this name has to be inserted into NAME array which is the private name. For that I am writing a code here but observe the way the function header is written. By just writing put name it is not adequate. Put name could have been a member function of a n number of other classes also. Of what class is this member function has to be explicitly stated here and it does not matter whether this immediately follows the basic definition of the class employee. Still you have to say that. So here colon colon means on the left hand side is the name of the class. On the right hand side is the name of the member function which is being defined. This is how you define this function and this function is very simple. All that it does is given the pointer NAM it will copy the contents of string pointed to by NAM into the contents of into the array pointed to by name which is what it is taking a name given from outside putting it in the inside. You can do the exact reverse when you are saying get name. When you are saying put salary you will say given the salary assign it to the salary word. How do you get salary? This does not even have a parameter. So float employee colon colon get salary. What it does? Because it is supposed to return a value. What is the value? It is returning float. So this is not this was why this is float. That means this is a function which will not take a parameter but read my private data value of salary and return it as the value. So it just says return salary. That's a simple implement. Look at the usage. This is my main program. I define an object called E which is of the type employee. Once E is an object then the moment I say employee E an object is created by the C++ compiler during the run time and that object will have the two private data values name and salary and it will have ability to execute those four interface functions. The way those functions are to be invoked by outside word however it requires you to qualify each function name by the object name followed by a dot. So this is how you call a function. E dot put name. Not just put name. E dot put name. So let's say E dot put name delete. E dot put salary 7538.50. Just these two function calls will initialize that object private elements exactly to name will be initialized to delete salary will be initialized to this. Now that object can remain there. It can participate in any other operation at any point in time. If I want to find out what is this object's name and what is this object's salary I can do that simply by saying E dot get name E name and E salary equal to E dot get salary. Observe that E name is defined as an array of characters here and E salary is defined as a float. So I am extracting that information and putting it in the two variables in arrays which I have defined here. The extraction is done by those member functions. Don't you see how neat my coding becomes in particular with respect to understanding of what I am trying to do. I am defining an object of type employee. I am initializing that object by some giving values and I am saying get me the name get me the salary which I can print out. I will now discuss another important program which uses a new kind of class which we have not seen. It is a data structure can be implemented in the conventional sense also, but we shall be using a particular data structure which is more easily implemented as a class. That data structure is called a stack. Stacks and queues are data structures which are conventionally regarded as complex data structures used for simplifying solution of some problems. We shall see the motivation to use a stack and then we shall see how that stack could be implemented using a class. The problem that we consider is the problem of editing text. Suppose you are given a text. This itself is a text. You have seen the G edit editor. How does G edit editor work? You have a cursor. So you move the cursor, say somewhere here and at that cursor you type something, the words come here. At that cursor you delete something, something gets deleted. So the editor in terms of a visual editing becomes an extremely simple job. You modify your program very quickly, save it, recompile it. But have you ever wondered how does the editor work internally? Remember editor is also a program. How do you write an editor program? So we consider writing an editor program without using any graphics. Suppose a text is given to us. Some text, thousand characters, thousand characters text is given. Now that text, I have a typed copy also so I can read where the text is. I know for example that at this position I have to insert this word. I know at this position I have to delete something. So I know which position what I want to do. Suppose my editor program, you tell me that for that give me an editor program which will permit me to move an equivalent of a logical cursor. So I imagine that a command could say go to 6, that means go to 6th position. So the characters in my text are regarded as a large string and inside that string I might want to go to any position. I may assume initially I am at the 0th position at the beginning of that text. Suppose I say G6 that means go to 6th position. Once I say G20, I will go to 20th position etc. Now at any position I may want to do one of the two things. Either delete some characters or insert some character. So I want a command which says insert followed by a word. A word mind you, simplifying not a string with blanks etc. So suppose I want to insert star, star, star at the position 6. I should be able to do that. Alternately if I want to delete, say I want to delete 32 characters from this position onwards. So if I follow a simple logic that look there is a text. The text is going to be regarded by someone as a long array of characters. Someone will say go to this position. I should logically position my cursor at that position and I should be able to say insert this word if someone says I should be able to insert those characters inside or delete those characters. So add, delete or modify text accordingly. Rewrite the text in an output file. Here is an example. Assume that the text is read as a single large character array. So there is a 5 which contains that text. I read all characters in a large array. Now that array has position 0, 1, 2, 3, 4, 5 up to say 5000, whatever. There will be a length of that text. I will know what that length is. Suppose this is the string a, b, b, b, b, c, c, c, c, d, d, d, d. Now I define a cursor which is originally at this position. So you see this line. This shows the cursor logically. There is no physical cursor that exists but we can notice the position. Its position for example is 0. We should be able to logically move the cursor. Say if we say go to position 6, the cursor will be positioned after a, a, a, b, b. Then I say insert two stars at that position. Then not only these two stars should be inserted here but the cursor should move here. How will you handle this kind of array? This is my array. I can have a cursor variable here and I can say cursor is equal to 0. Originally it is here. Later on somebody says move the cursor to 6th position. I can move it to a new, new cursor position. That's new cursor is equal to 6. All right. I will go to this position. 0, 1, 2, 3, 4, 5, 6 would be here. Anyway if I, if I, let's say I put it here. This is my position all. Now how to insert two stars? How will I insert two stars? What I will have to do is whenever I want to insert these two stars, I will have to push all the characters down. So I will have to move the last character to the last plus one position, then last minus one character to the last position, etc., etc., up to this point. And how many? Two, because I have to insert two characters. If I have to insert three, I have to push three characters down. What happens when I want to delete? If I want to delete these characters, I will have to pull everybody up. So that means every time in this array, I will have to keep on doing some nasty computations of pushing these arrays down, pushing these arrays elements up or down. Notice that as long as I treat this as an array of characters, I will have to do pushing and moving up and moving down. But doing it in a single array directly operating on that text is not the most convenient way of organizing your logic. For this reason, there is a new data structure which is called a stack. So a stack is essentially a container in which values can be pushed in or popped out. It works on the principle of first in and last out. So you might have seen that, I don't know whether you are hostile messes. You have a stack of plates which you pick up. So you will always pick up the plate from the top. You can't say give me the fifth plate. So whatever is the top plate, you take it out. That means that is a stack. So the operation is called pop. So you say pop, it will give you one plate. Somebody else says pop, it will get the next plate. Now after some time, somebody will start putting clean plates back into that stack. That operation is called push. So stack conceptually is an extremely simple thing. There is no pointer, shunt or mechanism, nothing, nothing. Where you are in the array, you don't have to worry about. You either push an element on the stack or pop an element outside this. If that is the methodology, then can I use it to simplify my editing logic? Editing text logic. There is a well-known model for editing called two-stack model. Let us look at this by saying this was my original text. A, A, A, A, B, B, B, B. Let's say C, C, C, C. Let's just keep this. What I will do is I will maintain two stacks. I will call this L stack, the left stack. And I will call this R stack, the right stack. And the left and right is defined based on the position of my cursor. Originally, my cursor is at the 0th position. When the cursor is at 0th position here, the stack will have all these characters here. C, C, C, C, B, B, B, A, A, A, A, like that. All the characters are here. There is nothing here. This stack is empty. Suppose somebody says move the cursor to 6th position. Then what I will do is I will pop from this stack and push into the other stack. Consequently, when the cursor position is at 6, so A, A, A, A, B, B. Let's say it is here. Then my two stacks will look now different. How will they look? Any idea? I will pull from here and push it here. So please note that the elements which are going into the L stack will go in a reverse sort of fashion. The first A will come here, then next A, then next A, then next A, then B, then B. And on the other side, what will remain now? B, B, C, C, C, C. But notice that moving the cursor now is an extremely simple operation. For those many times, pop and push. Pop from right, push into left. Pop from right, push into left. That's it. I can move the cursor any way. Now observe deletion or insertion. If I want to insert, say, five characters, say stars, stars, stars. Well, let's say one, two, three because this sequence is important. If one, two, three is to be inserted, I can push one here, then push two here, then three, push three here. Any time I combine these two stacks, I have got the full text. If I want to delete, I can delete by popping and forgetting them. This is an extremely elegant and simple model of implementing an editor. And that's why it's called a two-stack model of an editor. What we are going to see is we will define a class called stack. And we will implement the simple trial things. Okay, these are all comments. Forget all that. What is relevant here is to remember, this program is called stack underscore class dot cpp. That is the name by which it will appear on the model. I am defining a macro size, 10,000. Now I am defining class stack. Notice that in class stack, I have char s size. So I am implementing the stack using an array only. I am implementing the stack using an array only. However, this array is not a conventional string. These are only set of characters. So I don't have to worry about backslash, zero, whatever. It will just assemble all the characters. Any stack will have this array as a private element. Any stack will also have a thing called int tops. What is tops? Top of stack. Please remember, if I am implementing in an array, then internally I must keep account where my current position is. Because ultimately in the grass root level implementation, the stack is still an array. Stack has 0th element, 1st element, 2nd element. So I am now considering this as an inverted array. 0th element is the bottom. 1, 2, 3, 4, 5, 6, 7, 8 goes up. And whatever is the top element, that is pointed to my tops. But outside world does not know where I am in this stack today. Only I know. Only the stack knows how many plates are remaining there. You don't. You say pop, it will get a plate. If there is no plate available, you say pop. What it will say? Sorry, no more plate. Similarly, you keep pushing plates. It will keep accepting. But when the stack becomes full, it will say sorry, stack full. Exactly the same logic is implemented here. So there are public interfaces which include a static int stack flag. I have not used it in this sample program. We shall see later how that can be used. There are exactly three functions. Void init, initialization means top of the stack is set to 0th position. There is nothing there. Void push, a character will be given that has to be pushed on to stack. And care pop, nothing is given. I say pop, you will give me a character. These are the three functions. Let us see the definition of these three functions. Void stack, colon, colon, push, care c. What does it say? If tops equal to 10,000, okay, I am sorry. I have directly used 10,000. I could use size here if I wanted to. Output stack is full and ended. Otherwise, if stack is not full, assign the given character to the tops position and increment tops by one. Simple. So push operation is very simple. Whatever is the current position, put the new character, increase the position by one. Exactly opposite will be done for pop. What does pop say? Stack, colon, colon, pop. If tops is 0, stack is empty, end. Otherwise, tops minus minus, return s tops. So whatever was the element s tops, you return. Why this minus minus first? Because you have incremented the tops when you inserted something. You are ready to look at the next available position. There is a init thing also. So init will just initialize, it will set tops to 0. That's all. Now look at the main program. I am defining two stacks as per our model. Left stack and right stack. I am defining a text array of 5,000 elements. Out text array has 5,000 elements. I am defining old cursor, new cursor, text length, out text length, etcetera, etcetera. Defining some i. Sample text for editing. So I have just arbitrarily constructed a string containing all the letters a to z. This is my text. The text length is strln text. So I know what is the text length. Ordinarily, instead of using string copy, I would be having an iteration where I will read the file. All the characters into this are. The text to be edited is this. I initialize both stacks. That means I set the pointers to 0. Tops to 0. Now cursor is at the beginning of the text initially. What it means, as per our logic, put the entire text on the right-hand side stacks. How do I put everything on the right-hand side stack? I know that I have text length number of characters. So I start with i equal to text length minus 1. Up to i equal to letter a equal to 0, i minus minus. In this iteration, all that I do is I take one character of text i and push it onto r stack. That's all. r stack dot push takes time. Now I set cursor to 0. I am all set up to edit. I will set an edit flag to 0. I will define an edit command as a character. i for insert, d for delete, etc. And if a word has to be added, I will take it in the add word. In order to keep the explanation simple, I have not implemented the entire editor here. But I am just showing you how a word could be inserted at a given position. So the sample program that I have written here, it's a trial code, takes cursor position to 10, inserts opening brass 1, 2, 3, 4 before that position. So what I am doing? Add word is arbitrarily defined as opening brass 1, 2, 3, 4. So this is copied into add word. This is the word to be added. New cursor is 10. The actual program, they are fixed values. Of course in an actual program, you will interact with the user, get an edit command and implement exactly something like that. Now the new cursor could be either larger than the previous cursor position or smaller. So accordingly I will have to move the cursor to either left or to the right. I decide what is to be done by looking at the two cursor positions. Currently the cursor is 0. New cursor is 10. It means that I have to move the cursor to the right. What does moving cursor to the right means? Take so many characters from right stack and push it on the left stack. So that is what is being done. For I equal to cursor to new cursor, rstack.pop will give you a character. I am invoking the function pop for right stack. And whatever character I get, I use that as a parameter to push it into L stack. A simple operation, lstack.push in bracket rstack.pop. I do the exact reversal. It will take from this to that. A simple operation, iteratively executed, will push the cursor to wherever I want. And after, otherwise, I will shift characters to right stack from left stack. So exact Ulta. lstack.pop and push it into rstack. In either case I come out here and now I have to push characters from the add word to the left stack. This is my sample word to be added. What do I do? I know the length of this. So from 0 to that length, I have to push each character to the left stack. That is what I am doing. lstack.push add word up. So I have these two stacks. I have got the cursor correctly pointed. The new word is getting pushed onto the left stack. And after pushing that word, I calculate the new out text length, which is the text length which was original plus string length of add word. What is the cursor value now? The cursor value now, first of all it is a new cursor. So that is instead of 0, it is 10. Additionally, the cursor will have so many positions added because of the word that I have inserted in the left stack. So the cursor will be 10 plus this length. That will be the new cursor. Remember that the number of characters in the two stacks together has increased. So I am resetting the cursor. And at this stage I am ready to accept any new comma. So I can actually iterate around this. Collecting commands from you, editing it, inserting, deleting, whatever, whatever. At the end, when editing gets over, I assemble the entire edited text in the R right tag. So basically I bring it back to the position where cursor is at 0. What do I do? From cursor to I greater than 0, these are the elements which are in the left stack. I push from the left as a pop from the left stack and push it on the R stack. That means the out text is now assembled on the right stack correct. All that I do is I get text in the output string and print and that's it. I get rstack.pop one by one into out text and I print out the out text. What I have done is I have written a small version v1 where I have inserted intermediate cout messages such that you know exactly what is happening. Here is the last slide which shows you the print outs from such a program. So this is the sample execution output. This is class stack v1. Text is this. Text length is 26. Shifting all characters to right. Word to be added is 1, 2, 3, 4 with length 6. Shifting 10 characters to left stack. So it shows a, b, c, d, e, f, g, h, i, j. These get shifted to the left stack. Adding characters from add word. So what characters I am adding? Opening curly bracket 1, 2, 3, 4, closing curly bracket. The output length is now 32 because 6 characters have been added. Shifting all characters from left stack to right stack. Now observe the order in which the characters are shifted. First this character will come. This was the last character. Then 4 will come, 3 will come, 2 will come, 1 will come. Then this opening brass will come. And this will be followed by j, i, h, g, f, e, d, c, b, a because it was in this order that I had pushed things on the stack. So it will come out in the reverse order. And finally the output text is this. So we conclude by saying that object oriented programming does give us an enormous advantage in hiding unwanted details internally. Stack is a good example where I interact with stack only as pop and push. I don't care whether it is implemented as an array. It is implemented two-dimensional array. It is implemented as a whatever, whatever. I don't care. I know that whenever I say push and give a character, it will be pushed. Whenever I say pop, I will get a character provided. It's a character stack. We can enlarge it by using the polymorphism to have stacks which deal not only with characters but deal with integers, deal with floating points, etc. We will discuss that later. Thank you very much.