 For this material by Dieter on C++ for programmers, the book is actually meant as an advanced study for those who have already done a first course in programming. In fact, usually in IIT also, we would usually have an object-oriented programming course or a lab even for computer science students in their second year. Consequently, please take this discussion as an elementary introduction to the notion of objects. Most of the programming nowadays actually happens using object-oriented programming concepts, primarily because of the rich features that it provides for building very large applications. Information hiding is something that we briefly saw. There is a notion of inheritance where you could have a class, a subclass, a sub-subclass, where a subclass inherits properties of the major class. Let me just spend two minutes in explaining that notion. So if you take a person as a class, for example, some of the attributes of the person which can be defined universally for any object which is a person can be done at this level itself. For example, the name of the person, the date of birth, address, context of other projects, for example, fingerprints, these could easily be the member data elements of every object which belongs to the class person. Now usually you might define additional classes which are populated by objects which are persons but they have some special attributes. A clear case is that of a student. Attributes for a student independently. You will find the student also has a name, student also has a date of birth, student also has an address, student also has a fingerprint. But additionally, the student has room number, the student has hostel number, the student has room number, the student has CPI. Now some of these things will become attributes of an object. Only if that object is a student in IIT kind of setting, where you have CPI. If the student is a university student, he or she might have marks. The point being made is that once I have defined a class called person, then is it necessary for me to redefine all those attributes for a student which are already there as part of the definition of my class for persons. If I could somehow say that the student class is not really a completely independent class but is a subclass of person. Now context of programming, this kind of modeling facilitates a huge amount of reuse of whatever I have already defined for the parent class. For example, all the data elements which form attributes of the superclass are inherited by every subclass member. So consequently I define these attributes, maybe certain functions, member functions of this class which will let's say set data, set address, capture fingerprint, all of these methods or functions as we call the member functions in C++ can be defined for the person class roughly in the way that we described yesterday. But once they are defined here, they automatically are inherited by a subclass. Consequently if I just say without defining anything that student is a subclass of the class of persons, then every member element, data element which is an attribute of person automatically becomes an attribute of student. Every member function in the class person becomes a member function of student. This property is called inheritance. Just like traditionally children inherit properties of their parents genetically. Similarly subclasses inherit properties of the classes. Now every subclass would have a few attributes and a few functions which are peculiar to that subclass. Obviously otherwise we wouldn't need a subclass if everything was same. It is those additional attributes and functions that we need to define for this new subclass. So for example, if we define student as a subclass of person, then we need to define for the student those attribute values such as rule number, hostel, room, CPI which are different from the attributes of the main class. Similarly any additional function which for example could calculate CPI which could allocate a hostel number, room number, etcetera or change room number, hostel number need to become member functions of this subclass. Calculation of CPI would be a function which belongs strictly to this subclass. It will not be available to objects of the class person obviously because they are all defined in the pattern. So a subclass inherits attributes and member functions of the parent class but not the vice-versa. Additionally there is a provision. There might be a feature for example that you want to redefine differently in a subclass. Consider this, as far as IIT campuses concern, hostel and room constitutes the address of the student. Now even if the address of the person is defined differently, this address is defined differently. I have two choices. Either I can treat this address as the address of the originally the student and call this as current address. Ultimately I may redefine the address itself here. If any attribute is redefined in the subclass then that redefinition holds. The originally definition is not inherited. Exactly the same thing happens for functions. If you were to rewrite a function with the same name that you have originally written for the members of this class here then the rewritten function holds for the subclass. So class and subclass are extremely powerful programming paradigms. On one hand they permit you to inherit properties of the parent class automatically. You can see how much reduction you will have in writing your programs. Everything that you have defined for person is automatically assumed to be defined for student. On the other hand if you wish to change something, you of course will define additional attributes and additional functions which are required for the student subclass. But if you were to reuse any function with the same meaning that was defined for person, you can do that. If you want to reuse it with a different meaning then you can reimplement that function or redefine it with a different meaning in which case the new definitions will hold as far as objects of this class are concerned. Objects do not fall from the sky. Consider a variable for example. A variable is brought into life in your program when you decay on that variable. So for example when you say int count that is the time when compiler allocates memory to count that is the time when compiler decides that this count will hold an integer value in an internal format binary or whatever. Subsequently wherever you use count this will be typically an initialization statement. The scope of count is defined to be the entire program. When does count disappears from the scene? The count disappears from the scene automatically when you say return 0 close this thing. So when the program exits it is the operating system actually which cleans up all the memory that has been allocated to this particular program. In the process the count is wiped out. So just when a variable comes into an existence only when the program is executed because the compiler has said allocate memory to this. So when you load the program at that time the count comes into existence and when you finish execution of the program the count disappears. The scope of count is the entire definition of count. We have already seen for simple variables in our programming language we could for example define say int j int j also is brought into existence by the compiler but this j will have a scope only up to the curly brass which closes this curly brass. Because the scope of this definition is just the first statement. Consequently as well in the program if you have said j is equal to 25 and subsequent to this for loop if you say c out j independent of what was the value of j inside here the value that will be printed will be 25. Because outside this for loop there exists another variable of the same name j it has been brought into existence. Consequently there are two j's for some time during the execution of your program. There is a j here which is kept separately a new j is created here allocated memory. It is handled inside but when you quit this j disappears. This came the scoping is important to understand and that is the reason why when you write large programs where same named variables can be used by different people for different purposes it is important to limit the scope of local variables in this fashion. This is an extreme example where you limit the scope of your local variable to a body of a loop but in general the scope should be contained within the program segment or function segment that you write unless of course you require certain variables to be known globally in which case you will define them as global or static variables. Why I repeat this notion is because in objects exactly something similar happens an object has private data members which are not known to outside the object. Those values cannot be touched. So any variable names that you use to define private variables of an object cannot be modified externally. Only the methods which work on that object which are the member functions of that object can modify them. Similarly the object itself is brought into existence only when it is defined. An object class definition is different from an object definition. An object class definition usually happens in some header file just like your voltage files in the conventional program. Yesterday we saw an example we shall see one more example of extension of the same thing here. Similarly the member functions which are defined in the class will operate upon any object of that class. As a matter of fact if you see c out if you see c in these are nothing but the member functions of special class called stream class. And these are called operators. This is called an insertion operator for the member function. And member functions are involved in one of the two methods by either putting a point this is what we have been using. There is an additional condition called an arrow which we use in case the object is referred to by a pointer rather than the object itself. So whenever you are the object name you use dot convention to invoke any function belonging to that object. Let us look at some of these things here. We will begin with looking at a pre-processor directory which is quite important. We have seen some pre-processor directories earlier and we will see one more in the context of the class. There is a notion of creator and destructor of the class and there is a notion of private and public properties of the class which I will briefly see them again. The basic theme is you should separate interface and implementation. So you may implement a particular member function in peculiar way that you want but that should be hidden from the users of that function who should be familiar only with the interface. Very roughly this corresponds to prototype definition of functions that we have seen earlier in our conventional program. So a prototype definition of function hides all details about implementation. It just tells you how that function is to be invoke and everybody can invoke that function as long as that function is included during compilation and linking process. Just like functions could be separately put in a separate file could be compiled into a compiled version. Similarly object classes could also be defined in separate files could be compiled and kept ready separately which means not only the class names but class attributes and class member functions are automatically available for your program. That is how object libraries are created much like the function libraries in the conventional program. As a matter of fact without knowing explicitly so we have been using all standard object libraries all C++ standard libraries when you say using namespace std for example or when you say include fstream or include string what you are doing actually is you are invoking you are telling the compiler go back to the previously compiled object classes member functions, member attributes, everything everything and please make them available to me. The reason why your programs are not very large is because huge amount of coding has already been done as part of these member functions of standard classes. No modern software system can be developed without strong backing from such existing libraries. Many of the domain specific libraries are proprietary. For example if you see the fingerprinting kind of environment there are many people who manufacture devices but they hide their member functions of the classes which they define. They don't make those libraries available. However some of the libraries are mandatory made available for example in our case the program that you execute in the lab to capture the fingerprint could not have been written unless the interfaces were made available by the manufacturer of those devices. Of course the way they had documented it was so short it was not like the standard C++ library that is why it took some time to construct a program which can capture fingerprint give you a file and that file you can use wherever you want. But such is the case of developing large programs that you would necessarily need large libraries. For C++ specifically there is a huge attempt to develop a very large open source library comprising of various domain specific components as well. It is expected that that library over the next decade will go further and will become the hallmark of any modern program. Let us look at this example. We have seen that object classes need to be defined but what may happen in a group project is somebody may wrongly redefine an object class. Same definition. Two groups have agreed on the definition of object class but it is not clear whose program code should contain that definition. So one group defines a dot h file and I also define a dot h file. In such case how do we ensure that only one of them is used by the compiler because like your variables or arrays or functions object classes can only be defined once. For this purpose we use the compiler directive called if-ndef. If-ndef means if not defined and the directive called defined. Originally everybody will have hash defined some name. This name is an arbitrary name by the way this is my choice but it is a C++ convention that if you have a dot h file then you convert the name of that dot h file into all capitals convert that dot into underscore and write a capital H instead of small h. So the file mytime.h which will contain all of this would actually have an artificial name it's like a variable name mytime underscore h. What different groups working on the same program must agree to is the definition of these names. So now it does not matter whether in my program I have used this mytime underscore h and I have tried to define it and if somebody has this program in the same group somebody else also does the same thing. So as I said both dot h files get included because of the input statements and your program gets compiled. The first one to occur sequentially is let us say this where this one is encounter the compiler pre-processor this is it's not compiled it's pre-processing it will come across this if endf and you find out from its memory data structure whether everything called mytime underscore h has been defined. It will notice of course that it is not defined because it is the first file it has picked up. Since it is not defined it will execute everything that you state up to endf. You will recall if you end a statement that we have used for debugging purposes once this is very similar. But if endf is a specific pre-processing directive. So as I said since mytime underscore h has not been defined by anybody when this particular file is pre-processed all of this will be included for compilation. Now imagine there is another input file which contains exactly the same thing which comes later in the sequence of all the files that you are pushing to the compiler. The pre-processor of that time will look at this and when it cross checks it will find out mytime underscore h has already been defined. So since it has been defined it will ignore that complete dot h file. Consequently multiple definitions are prohibited automatically by this pre-compiler directive. Just to ensure that you always take care to do this it is not uncommon amongst professional programmers to include if endf and endf in every dot h file that they write. Whether somebody else is going to write and read like that or not it doesn't matter because they want to ensure that exactly one version is processed. But the main is this prevents multiple definitions which is a must. We will again go back to using mytime which is a class that we had defined. So when I say include mytime dot h that entire definition of that class would get defined. We have defined that class yesterday. If you recall that class had three parameters or other three attributes other minutes and seconds and then we have defined certain functions to work on that class. Here I am extending the definition to and using that definition to include a main program which shows how the objects are used. So look at this for example. It will remain as usual. Now look at this place. Mytime t. Mytime is actually an object class. When I put the name mytime it is like a variable type, integer, floating point. So I am trying to define an object of time type mytime. So an object belonging to a class this particular declaration is called instantiation. So I am instantiating objects. Just like I instantiate variables or arrays. Similarly I have instantiated objects. Objects do not fall from the sky as I mentioned. So in every program which tends to use any object that object has to be defined and has to be informed to the compiler that this object belongs to this class. Once I say mytime t, t automatically becomes an object of type mytime. It inherits all the attributes that mytime class has and it inherits all the member functions that mytime has. So automatically all the member functions and attributes become the property of this object t which has been created here. Here is another example of instantiation. Wake up in bracket 6 comma 30 comma 0. This is equivalent to instantiating a variable with initial values. So just as you can say int count equal to 0 here I am saying int wake up in bracket 6 30 0. Since the user knows that there are three attributes other minutes and seconds which belong to a particular object of any class then it is defined in wake up time for example as 6 hours 30 minutes 0 seconds. Notice that as far as the user of this object class is concerned the user need to know only the interface. The interface to object says there are three attributes and there are so many functions. As long as that is known how exactly those functions are implemented need not be known at all. Here is another instantiation of 10 objects in the fall of an array. So it is an array of 10 elements called t-stamp or time-stamp. So I could use this array for example to capture the time-stamps of various activities. Let us say a student enrolls in the institute I capture the time-stamp. The student goes to the hostel in Melbourne another time-stamp. Time-stamp is by the way a more generic term used in the information technology field to represent not just time in hours, minutes and seconds as we have defined it here but most of the time it is defined as date and time. So time-stamp actually is a unique combination of a 4 byte integer number which represents date and a similar byte integer number which represents time. It is always told in an internal format. So time-stamp is usually an intrinsic element of any object library or any large function library that you write for processing data. I am just mentioning this to tell you that time-stamp is a generic concept in programming. However, for our purposes this t-stamp declares 10 objects in an array of the class my time. So there are three types of definitions that you have seen. Definition equivalent or simple variable which is an object of this type. Definition of a simple variable with initial values which is also an object of this type. 10 element array of objects which are of the type my time. Here is another definition, constant my time node. This is again similar to the definition of constant variable. A constant is something whose value cannot be changed. So node for example is a standard time. None is 12 o'clock. None does not change. I can assign a value 7 o'clock to node because node I want it to remain 12 o'clock. So I define it as constant. So when I define constant my time node 12, 0, 0 this defines a constant object. Because that has to be brought into existence by some definition line. Finally a variation or definition of an object is defining a pointer. My time start time pointer. Please note that when I define something like this what is created is a pointer to that object. Pointer to some object of that class. There exists no object at this stage. So just as you can define in conventional programming pointers to integer, pointers to character, pointer to characteristic etc. But there is no variable, there is no memory allocated at that point in time. There are two special functions available in C++. One is called a new and the other is called delete. The new one actually brings an object in existence and allocates its pointer to such a pointer value. And delete of course deletes the existence of that object. So pointer is a placeholder kind of thing. The pointer variable which is declared here time ptr will be able to hold a pointer to an object as in hand an object is either created or as in hand the pointer to an existing object is assigned to time ptr. It is possible for example for you to say time ptr equal to star t. Because t is an existing object from that point onward time ptr will point to object called t. That pointer can be used to point to any one of the objects. I could even assign it to say fifth element of t star but I could always create a new object if I have a pointer placeholder for it. Here is how I will use the member functions which are being defined for the class myTime. t that prints standard. Now this is a method or this is a function which is defined for the object class myTime. All that it does is it present, it prints the values of time in the standard time format. Those of you who recall yesterday's discussion will remember that the standard time was printed as 12 or more nearly not 0 0. Observe that there was a class which said that if our is either 0 or 12 print it as 12. Otherwise print it as between 0 to 12. That is my definition of defined printing the time. Observe also that there are no parameters here. So what will it print really? Why there are no parameters? The reason there are no parameters is once I say object t. When the object was created at the time of creation the three attributes of that object were initialized to certain values. So when you instantiate an object the object automatically gets constructed and we shall see some example of construction. At the time of construction initial values are assigned in case you have an initialization routine available to you. Otherwise it is expected that you would have set values appropriately as we shall see later how do we set them. The point here is when I say t.printStandard the printStandard function will be invoked and it will print the values of the three attributes which are not being passed to it but because t itself is being invoked the values belonging to the attributes of the t is what will get printed here. On the other hand when I use a member function like setTime I am required to do parameter value. t.setTime 13 comma 27 comma 6. What will these do? This will assign around minutes and seconds as 13, 27 and 6 to the object t. Consequently if I invoke printStandard now it will print 1, 27, 6 p.m. Remember that in the printStandard function we are printing hours in 1 to 12 notations and we are adding a.m. or p.m. at the end of it. So that is how it will get printed. On the other hand if I try to setTime to 99, 99, 99 which is obviously minimally time you remember what will happen. The setTime function is powerful enough to say that if any hour, minute or second specification exceeds the specified limit of 12, 60, 60 then it will be set to 0. Again that was an arbitrary decision that was my implementation decision. In a programming situation you may want a different default value to be given. You may want an error message to be printed. You may want an error parameter to be passed to some other function. That is your choice of implement. The point we made is we will use a function. I don't know to worry about how it has been implemented. As long as the results of the implementation are well known to the people who are using that function. So in this case as long as I document that setTime is a function which will set appropriate time to the three attributes of time or in early when you supply those correct values but if you supply incorrect values I will reset that to 0, 0, 0. As long as that is understood that the other function is used. Here is another call. We cut that set hour. Notice that yesterday we did not discuss set hour as a function. We have set time only as a function. So that is why I said what is this? In the next slide I will elaborate how it is useful to further break down set time into set hour, set minute, set second. For example it is quite likely that in a digital watch which has a default wake up time of 6.30 somebody wants to change only the hour. They say I am sleeping rather late till night so instead of 6.30 I want to wake up at 7.30. Now 30 is already there in my clock. I don't want to change it. So I just want to change 6 to 7. So what am I trying to do? I am trying to set only the hour. Originally I would have prescribed 7.30 is 0. Why should it be necessary? Why can't I set any hour? I could if I have a special function available which can set our individual. So we shall see how this is done but it is possible to have a function which sets only the hour and not the time. In this case wake up which is an object I could not only use wake up not set time to set the full time if I wanted to but I could also use wake up not set hour if I wanted to do that. Here is a file called mytime.cpp which tries to define that class once again. I have included the complete definition I have included only portions of the definitions to illustrate the point that I made. This column-column notation is important to understand. This column-column notation defines actually the class name here my time and as a part of this class name it defines the constructor. So the constructor is the major function my time int h int m int s our minute seconds are three integers. Notice that the three attributes of the class are integer, integer and integer. And only thing that is defined in this constructor is set type hms. Notice that this constructor is not something that we have to execute explicitly. Whenever we define an object in our main program automatically the constructor function is called and whatever we have stated in the construction function will be executed. The construction function sets. Set time hms is the constructor. Why? My time. Set time integer h integer n integer s. Since I have used set time I am defining the set time here. But this time I am defining set time in a different way. We have stated what we saw. We had actual code which was setting up hms. Instead now I invoke three other member functions set hour, set minute, set seconds and finish that function. So now I am defining a member function set time in terms of three additional member functions. These are set functions. This perfectly valid. After this of course I must define each one of them. For example I now define for the class my time a function set hour. It is only one parameter integer hour. This time it is defined properly with the code. If hour is less than zero or if hour is greater than 24 set h to zero. Else set h to hour. So suppose somebody passes an hour which is 15 it will be valid. 2 will be valid. 0, 24 will be valid. But minus 18 or 212 will be invalid. So consequently this set hour takes care of setting the hour attribute of the object properly. In exactly the same time I can have set minute, set second, etc. The set and get functions are typically the names used for what we call excess functions of the objects. Anytime you define an object class you are expected to define these set and get functions. Set functions which will permit you to define various attributes of the object and get function which will permit you to access that attribute and maybe print it out or show it or assign it to something else or simply return the value of that attribute. So this is a function called get hour. Get hour return h. Please note that this get hour is defined as a const function. Because it is not supposed to get any parameter no changes are going to be reflected inside the object because of execution of this statement. This statement is nearly an excess statement. You are asked me to find out what is the current value of the particular attribute of an object and returning that. It is not necessary to have only get hour and get minute, get second. I could always say get time and write a function will return hour, minute and second. Or it will return something else and it will print hour, minute and second. But in the other way it is advantageous to have get and set functions which operate upon individual elements or individual components or some small subset of the components. What is the purpose? Notice the difference between set time and set hour. Set time itself is defined as a combination of set hour, set minute, set second. What is the advantage? Suppose tomorrow I change the implementation of set hour. For example I want to set hour in a different fashion. I don't have to change set time at all. I don't have to change set minute. I don't have to change set second. This appears to be an artificial example because hour, minute, seconds and setting time are equally trivial activities. But consider on the other hand you have a very major function as the initialization function or the constructor function. And within that there are four or five different sub-functions. It is always better to write those four or five sub-functions separately and invoke them in the main function. So that if along with the implementation changes for any one of those functions the main function, the main body of the object definition need not be changed at all. This becomes vital in large projects as you would have experienced. Recall that in all the group projects that you have been doing for last two weeks a large amount of time must have been spent in communication, in agreeing to certain things. And very little time will have been spent on programming. You will therefore like that whatever little time that is spent on programming is quality type. That means you do not have to reinvest in that time again ever. And that is the reason why this kind of partitioning is extremely useful when you build large software. This is all I wanted to discuss. I just remind that yesterday there is a notification from Someshaykar which is slightly erroneous. So I am just correcting it. Please remember that as a group activity you have to submit a total of seven questions in two distinct submissions. One submission which is test questions simple median complex. Each question attempted by three students plus a correct answer for that question. That is the first submission. Each of these questions will be evaluated for two marks each accounting for six marks. The quiz questions which originally I had said five questions to be said by five teams instead I have now allocated topics to each batch and you have to set quiz questions on those topics only. I was told by some students that they have already set some quiz questions which are not on this topic. Sorry that is sad. You have good experience that you have gained by this exercise but you have to redo it and set questions in the allocated topic. So each lab batch has to set two questions in the elementary topic that has been allocated and two quiz questions in the advanced topic that has been allowed. Again this work should be distributed equally. I am not going to insist on any diary report or something but it is for the team leaders and the lab batch coordinators to ensure that every member of the lab batch actually participates in some of the other activity concerning this question. At the least I would expect that all students of a batch are familiar with questions and answers for the quiz and test questions which have been set at least by that batch. Originally I would have expected people to become familiar with all the questions and answers. But they do that on the submission date for this by the way is 16th. 16th is the last lecture which is Monday. So before the lecture the submission must occur. The lab batches therefore have to submit these as a deadline. The lecture on Monday is 11.30. So should we say midnight of Sunday then or would like morning of Monday? Okay morning of Monday. Everybody always wants more time. So let us say 8 o'clock morning Monday. That is the deadline for this. I will miss your notification but remember that. What I propose to do is the submission of these questions and answers of all the batches will be put together in a single document and will be made available on 16th night itself for all of you to look at just for fun. We will of course evaluate them separately. So that is all I wanted to say. Good luck for the remainder week to complete your projects and this exercise. We will meet on 16th primarily to discuss the composition of ensame paper and to remove any squeakers that we have about insubmissive marks and the totem marks. Just one more announcement. Some students have met me and told me this and one student has written a long mail in the mixing and the quiz papers you have the opportunity to get your quiz papers really looked at by the T.S. But because this is final exam people are worried whether they will ever get to see the final exam. Some people have said that they are confidential. Nothing of that sort. In fact in the older days when I used to collect papers myself, I used to collect patient papers publicly. So a lot of people would come and see each other saying my job was very easier because students will find others and somebody else's problem. We don't have that opportunity. The exam time table is said I think it is 23rd right? So 23rd morning is the exam. Papers will get corrected on 27th. And the paper correction will continue up to midnight of 27th because that is when my T.S. are available. Now I am required to submit the grades within 96 hours. So even if I make the papers available basically not here on that weekend. What I am going to do is I will keep the final exam papers with me. I will take special permission. Anybody who is interested can still look at the question paper. Suppose you don't have time when your exam is ending and you are going back home. Then mother, go back home. If you have a genuine worry that your paper has not got marks that it deserves please apply for recheck. I will give the answer book here and instead of reporting with you in front of me I will personally go through any problems that you have with this. Are you comfortable with this process then? Fine. Thank you so much.