 So, we move over to our use of, we have said that the classes consist of objects and the objects have contrast this with the variables arrays and structures are something that we have been routinely using in most of the earlier discussion in this course. These by the way are considered the native facilities available with any programming language consider for example, a declaration of the type int m comma n what you get are two locations assigned one for m one for n if somewhere you make a statement m is equal to 28 n is equal to 5 star m plus 37 what you are doing is you are writing expressions to make assignments. These expressions are evaluated as per the standard rules provided in any programming language and most programming languages have exactly similar rules because the simple arithmetic of integer floating point or the care manipulation etcetera then almost identical why are they called native to a programming language because explicit operations explicit expression rules explicit assignment rules etcetera are all defined specifically for all the elements which are operated upon in this fashion. So, for example, the operator plus minus star have specific meaning equal to has a specific meaning the moment we use a variable name it has a specific meaning that that variable name corresponds to a location in memory and a value internally represented in a certain form has to be used that is how we know that integer is represented separately floating point is represented separately care is represented separately. Later on we discussed using the conventional means only whether we can create super structures where a super structure has several components these components in turn could be simple variables for example, array is a super structure a super structure of 200 elements 1000 elements of the same type a struck is a super structure a struck could in turn contain a data element which is integer another element which is floating point another element which is an array and so on and we discussed that these effectively permit us to define what we call additional abstract data types the basic data types are in float etcetera, but these additional data types permit us to handle more complex structures as a single entity it is in this context that we discussed that if a struck is defined then how a structure variable say s can be used to define not only its components because whenever you declare a structure variable s of the structure type let us say employee then that s automatically gets allocated memory for all the elements of that struck. So, you have s dot m pi d s dot mp name s dot something referring to member element needless to add these individual member elements have still to be operated upon by using the normal rules of addition subtraction assignment concaten whatever we take this notion itself to one level higher when we talk about an object the difference is that if we write some functions in conventional sense the function declaration and function uses has a specific meaning for example, I could say a f 1 obviously is not an array it is a function it has two parameters m and n we do not know what f 1 does obviously there will have a prototype definition and the full implementation, but let us say it calculates square root of m into m or m square plus n square square or just m plus m or just whatever we do not know what it does, but it will return a value now when I make such a call it is a standalone call it does not make any sense the return value what you are going to do with it. So, that is why in normal functions you have a return type and you would usually have this as something like p equal to f 1 comma f 1 m comma n where you might declare p as n. So, our notion is that whenever you invoke a function f 1 with some parameters these parameters go to that function and some return value comes back there are occasions when a single return value does not come back, but multiple values are modified and are sent back and that is what you call parameter passing by reference we have discussed pointers in this context and how to pass parameters by reference and so on. In case of objects there is a fundamental difference while there could be some public functions which could be called and which may involve multiple objects in general an object itself invokes functions in order to modify itself or in order to give you out some information about itself and that is a fundamental difference between the functions which are written for object classes and the functions that we ordinarily write. For example, if I have a class irrational we shall see in a moment you remember we had discussed this we had discussed this class earlier we had data called so denominator and numerator we said that is the component of the rational class. Now, whenever there is a object that is declared which is of class rational it will automatically be assigned two memory locations one to contain denominator one to contain numerator ordinarily in my main program I will be never assigning anything to D or N nor I will be using D or N directly the idea of defining an object is I use an object in the context of wherever I wish to do something this is where we had used can you read this so you will remember this class rational where I define private data members as N and D and then I define member functions you will remember we had constructors please note that these are all definitions of the interface the functions themselves are not defined here usually you break up the definition of the interface and the definition of implementation in two separate parts as I said the implementation part could be compiled independently and made available as a class library but the interface part must be available in the source form because without that interface part being included your normal program cannot be compiled which uses the member functions to perform certain things on these of. So that is why you usually have them as say something dot h as a header file in this particular case I have a single header file which contains both the definition of interface as well as the definition of member function though it is not necessary we had not seen the destructor the destructor is being defined here it has a standard name tilde name of the class so please remember constructors and destructors have the same name as the class name this is an example of a constructor without parameters an example of a constructor with two parameters there are other functions so you remember I had mentioned a function called r components where given an object I want to find out what are the denominator and numerator separately so this r component is a member function which will get me two variables which are integer and I will return into those two variables the numerator and denominator component there was an observation made on this wide r component int and int and what is this kind of reference we are generally used to having a reference in which we define a pointer as a formal parameter and we say and m or and n in the parameter which actually cause this function so that the pointer is passed that is the standard way of passing by reference c plus plus introduces a implicit parameter passing by reference and this is an example of that I will explain the difference in a short while but this is the standard way of doing things here is a function called reduce there is another function which I have introduced called r convert this r convert converts a single integer into a rational so for example I just give 5 what I mean is 5 by 1 but 5 is an integer number is not a complex is not a rational number so I cannot use it as a rational log essentially I am constructing dynamically a rational object given only one parameter please note that I have already a constructor function defy but this construction constructor function will be used ordinarily when I declare objects in my program during that declaration any pre-assigned values which I want to give I can give and the constructor will be invoked to use those but if I want to dynamically assign values to numerator and denominator of a class object how do I do that there are two ways of doing it one is to invoke the operating system to give you dynamically some memory and ask c plus plus to dynamically create an object during which time the constructor can be invoked actually we have not seen that yet we will see that briefly in the next week's lecture there is a command called new when you say new something that new something is created that object is created however if ordinarily I want to assign numerator denominator values to an object which I have declared but not assign anything except the default value then I have written this thing called R convert it will take an integer number and create a object using that number as the denominator here are some examples of overloaded operators you remember last time we discussed that the overloaded operator I had given you a example saying I want to define plus as an additional operator which can operate upon two objects of the class ration basically I want to define arithmetic on rational numbers now we can have a function which does addition of two rationals and returns a rational but a much easier and simpler way is to redefine the operator itself last time I had tried to show you a sample definition of this overloaded operator thinking that since it is a function call and since the operation of addition will happen upon two parameters I will pass two parameters then I had said please think about it whether that is the correct version it so transpires that when I want to overload an operator the operator overloading cannot be done in this sense because operator overloading is detailed as an equivalent of a function call by the calling function so let me just go back to let us say I have declared a rational C1 R1 R2 R3 etc and somewhere I want to say I want to make such a state remember last time we have seen an example where I had written an add rational function so I could get R1 and R2 passed as two object parameters and get the final addition but here I want the normal plus operator itself to work exactly as it works in normal cells now this plus operator is actually treated as a function call and the second operand is treated as a parameter so it is as if I am saying please note that I can make this kind of call if I have written a function somewhere which takes one rational as a parameter so this is being invoked by R1 itself so obviously any changes that happen to R1 will be reflected in in exactly the same fashion instead of this function plus symbol is treated as the name of the function and R2 is treated as a parameter and R1 is treated as the invoking object that is the reason why when you overload operators plus minus star slash these are all binary operators in that they require two operands the first operand is considered to be an invoking object the first operand invokes the operation of plus minus star slash and the second operand becomes a parameter so whichever operand invokes that operation see R1 plus R2 R1 minus R2 R1 is the object which is invoking that operation and therefore R2 will be added to R1 R2 may be subtracted from R1 R2 will be multiplied with R1 or R1 will be divided by R2 the second operator therefore second object has this significance there is a difference between the meaning of the first and second so it is yeah very good point. His question is what is if I have R1 plus R2 plus R3 I am glad you made this point because this is precisely the reason why we would like to overload the operators without killing the normal semantics associated with that operator the normal semantics associated with arithmetic operators is that they could come in any combination in an expression if there are multiple such operations defined in an expression then there are rules of precedence which have been defined and at any one time only one operation takes place so let me expand this further by saying let us take this operation imagine for a moment that R1 R2 R3 are not rational objects but simple integer of floating point number now what would happen if there are simple integer of floating point numbers the standard precedence rule will state that amongst all these operations which are defined multiplication and division are at higher level and therefore this operation should be done first any operation which is done will result in a value of the same type. So if it is integer it will result in an integer in our case since it is rational we expect the same rules to be applied. So if star plus minus etcetera all been overloaded properly and have been defined in this context C plus plus will automatically determine the same precedence which applies to plus minus R and it will examine this actually calculate using the overloaded multiplication operation and it will give me a star say temp T1 R T1 as the result is a temporary object of that kind this replaces the occurrence of R3 star R4. Now the resulting expression is R1 plus R2 plus R T1 minus R5 all these operations are at the same precedence level and the C plus plus defines that these operations should be carried from left to right therefore now it will do R1 plus R3 which is what would have happened if you had simply R1 plus R2 plus R3 R1 plus R2 has precedence this will result in another temporary rational number R T2 let us say. Now the expression is R T2 plus R T1 minus R5 R T2 plus R T1 will now be added using the overloaded operator. So this is the reason why when we overload operators we would like to retain the natural flavor of the meaning of those operators as long as we retain the natural flavor C plus plus intelligently applies all the rules which are associated naturally with those operators. So all the precedence rules will be applied etc. So I hope that is clear this will work. Here are some default constructions initialize a rational number this is initializing a rational number with two parameters with one parameter having a default value mention couple of things first of all the destructor function note that the destructor function does not require a body it merely requires its definition for the sake of completion all the objects which are created through definition of the kind that we are doing will get automatically destroyed at the end of the program. But whenever objects are dynamically created using a new statement you will have the responsibility of invoking the destructor implicitly by deleting those objects whenever their job is done at that time this destructor function will be invoked and you will delete that. But it does not require any body it merely returns the memory that is used to the operating system notice that this is the way I forgot to mention this int star m star n sorry and m and n let us go back temporarily to discuss this you would be familiar with this kind of notation that we have used whenever we want to pass by reference we say and q whenever we call that function. So we are saying we are passing a pointer and in a formal parameter we write int star p that has been the usage so far this is the standard usage because otherwise as you know c plus plus will only send values by copying those values when you explicitly send it by reference value is copied but what is copied is a pointer a pointer is copied and that pointer ensures that the actual operations will take place on the value that you have. So in case I change star p for example here apart from returning the result if I change star p itself by saying star p is equal to 28 then the value of p would also be changed unless it is a constant there is another way of referencing sorry which is peculiar to c plus plus this facility does not exist in an earlier subset of c plus plus called language c this has been introduced when c plus plus was designed because the need for passing by reference is so often that c plus plus thought that a special way should be created to indicate that certain functions will receive their parameters only by reference that means they will receive only pointer this method is to say so you use and p as a parameter definition when you define the function itself later on when you invoke this function f 1 you simply say f 1 q now this is a different way of invoke ordinarily when you say f 1 q you assume that the value of q is being passed but because the formal parameter is defined as and something then what is extracted out of the parameter that is passed is its pointer and that pointer is made available it is therefore wrong to say and p and q for f 1 because then it will become pointer to pointer kind of it is illegal in fact so this is the standard way of using a parameter passing mechanism in c plus plus particularly in the context of objects that you define and that is why most of the object libraries that you notice the function formal parameters which are defined in the function definition will always say and something and rational and or in and or float and they will actually get receive a pointer and therefore it is illegal to say and again there notice that the left hand side shows the way in which you will pass parameters for example to functions which where you want the parameter to be modified for example scan f in scan f you have to say and m and p and whatever in order to get those values if you have declared your own function like this that is how you have to use it however if you have declared a function like this then you do not have to use that you just say this and what goes is a point is that clear having defined those operations here is an example that we had seen for example I am defining an additional function called display I could have made this display function part of the rational dot h itself I have chosen merely for demonstration that functions involving objects can also be written separately as part of your main problem why would this be essential this is essential when a class library is constructed by someone else and I am trying to use it take for example strings library or the stack library while the class library does provide the basic functions as thought fit by the creators of that library I might always require some additional function and I might not have the facility to modify that class definition therefore I might have to construct my own additional functions which in turn may use public functions defined in that class so that is why I am using this rational display function it is invoked by any object all that it does is it defines c dot r components n comma d x is equal to float n float d so basically I am creating a object c by defining the r components using the values of n and d I will outputs n and I will output d and I will also output the floating point value that is the objective here look at the main program I have created c 1 c 2 c 3 c 4 c 5 c 6 c 7 arbitrary objects these objects get created automatically I am displaying these c 1 c 2 c 3 etcetera and then I am adding c 1 and c 2 I am adding multiplying c 2 and c 3 and I am displaying these results I am dividing c 4 by c 5 I am displaying that result I am subtracting c 5 c 6 from c 5 and I am displaying that result this is a simple sample thing I have done merely to demonstrate what kind of object arithmetic that you can do the importance of defining a rational class is not simply to get an easy way of writing operations on the rational there is a larger importance and that is associated with our ability to do arithmetic which is more precise all of you remember that when I use floating point representation I lose precision because I have a limited set of procedure we know that whenever we add or subtract large number of values particularly values which are very close to each other I might lose precision or I add values which are close to each other I might lose precision or I add a very small value to a very large value I might lose precision to illustrate this I have written a simple series summation program let us look at this I am sorry I am not adding ok consider summation of series arithmetic series geometric progression you are familiar with those so a plus a square plus a q plus a raise to 4 is a geometric series a plus 2 a plus 3 a plus 4 a is a arithmetic series how will you call a series a plus a plus a plus a plus a what I am doing is I am showing that if I want to add given a value of a in the form of numerator and denominator and if I give some actual value what will happen to it so I am saying that I want to add n terms of this series I input the value of n however the value of the number a which I want to add is given in terms of denominator and numerator sorry this x is a mistake this should be this should be a here anyway so look at the look at the way the formula is written or the iteration is written I say a is equal to numerator by float of denominator please note if I say numerator by denominator I will lose everything there because it will be an integer division so I convert to floating point and get it and then the first term is 0 let us say now I add from 1 to n sum is equal to sum plus a so I keep adding a a a a now 1 by 3 added 3 times should give me 1 but 1 by 3 converted into floating point will result in 0.33333 and when I add it 3 times I will actually get 0.999999 of course the C plus plus will display the result as well but internally it will not be storing it as well if I add a large number of numbers say 30,000 then I will suddenly see that after adding 30,000 I should have got what result 10,000 I will not get 10,000 but I will get 9999 point something something that is where the loss of precision happen if however I did the same arithmetic using rational arithmetic I would not be subjected to this so to demonstrate this I have I have got another one here so this is a simple series with rational sum I have given numerator and denominator as 1 by 33 and I am adding 33,000 terms the number is 1 by 33 whose value is 0.030303 please note this is not the exact value after that there is another 030303 by the way 1 by 3 in decimal is not an ending decimal it is a recurring decimal so it will never end curiously there are some decimal fractions which may result in non-recurring decimal but when converted to binary fraction they may become recurring because base 2 and base 10 are not compatible in the sense of representing floating point number in any case you can expect that when you operate upon floating point numbers you will invariably get a loss of precision somewhere or the other this is one example so when I add the 33rd 33,000 term which is 1 by 33 the value is still this but the sum is 1000 by 1 because I am doing a rational arithmetic I am continuing to have my numerator denominator at every stage of the addition finally I am getting 1000 by here is a simple series some example I do 1 by 33 how many terms to be added the sum turns out to be 999.539 this is not what you will see if you add only 3 terms or 9 terms you will get an integer value because the floating point number which is converted for display by C plus plus in your C out statement automatically does moving upward the fractional value but when you have a value like 539 there is not much that C plus plus can do here is another example this time I have given the value of a as 1 upon 210 if you consider the factors of 210 they are what 2357 now obviously when you carry on the rational arithmetic and keep adding these numbers so this plus this plus this plus this somewhere whenever the numerator becomes either 2 or 3 or 5 or 7 it will cancel off and that is why when I add 21,000 numbers the 21,000 term is still 0.0047619 please remember this is not exact and the sum that is turned out is 99.9854 whereas if I just say multiply n by a n by a is a different multiplication a is still a fractional there is a loss of precision in a single a but when you multiply by n the value comes very close to 100 C plus plus shows it as 100 so n by a is not same thing as a added n times that is a fundamental fact about floating point arithmetic I would like you to remember always because many of you would be doing engineering computations with numbers spanning millions of operations addition subtractions multiplications and divisions and a major portion of your mind activity will be spent on how to limit the errors in the floating point arithmetic that will come on the other hand if I do the same example with a rational summation then I still get this number as 0047619 but the 21,000 term is again same but the sum is 100 by 1 so because the rational arithmetic I will finally get 100 by 1 value is 100 there are couple of questions at the end of that print out which I would like you to remember to look at can you read out that I think one of them is under what circumstances this will not work the rational arithmetic it looks to be very precise no loss of precision it should always give exact results and I will always get the approximate floating point value of the final result without losing precision in the intermediate operation but there will be some cases where things will not work first of all I would like you to come back to this do you appreciate the fundamental fact that 99.9854 is not same as 100 so let us first comment on this is this difference significant or insignificant yes the first question I am doing some arithmetic I should have got result as 100 instead I got 98.9854 99.9854 99.9854 99.9854 is this difference significant or insignificant insignificant everybody agrees it is insignificant no some people do not agree why it is not insignificant this is the problem with you you are not willing to go outside the domain of very well defined question and very well defined answer the question of whether a value is significant or insignificant entirely depends on the context now whenever you are saying it is insignificant or whenever somebody saying it is insignificant one is creating a context in one's mind and in that context one is answering this question let me give you two possible context one context is there is some additional remuneration that is to be given to an individual based on number of hours work multiplied by hourly rate or something and that is a fractional number and it turns out that the person should be paid 99 rupees and 98.54 paisa now clearly anybody who is trying to pay let us say integer sum of rupees because that is the tradition this sum will be updated to 100 the difference is insignificant suppose somebody says so I will get 99 rupees and 98.54 paisa which is actually at least equal to 99 paisa I lose one paisa if I am generous I will say insignificant if I am not I will say I have sweated down give me my one paisa extra because I will show the rational calculation in general you would agree that in case it is a question of rupees then 100 rupees and 99.98 rupees the difference is insignificant imagine now that this is the velocity setting calculation for a space craft which is being fired from seary quota and which is supposed to go to moon and the setting is to be done by the calculation of certain parameters which take into account variety of things it should have been 100 but it is 99.9854 will that be insignificant this space craft will never find the moon in fact you will be very lucky if it escapes the gravitational pull of the earth itself you never know so please remember this in the general context of problem solving whether an error is insignificant or significant depends entirely on the context some of you would be doing civil engineering and designing bridges some of you would be doing aerospace engineering and designing structures for the aircrafts when you design these structures you have to define certain basic minimal characteristics of these structures that they will bear so much load in case of bridges or they will bear so much of stresses due to the winds and external factor in case of aeroplane all those parameters are to be calculated as exactly as possible suppose you certify an aircraft fit to carry so many tons of weight and so many people and let us say you have an error of this kind of percentage and that aircraft is loaded with someone as thin as a 40 pound child but because that child is added to the aircraft the aircraft comes down the back not acceptable so please always remember this without before answering significant or insignificant you should always ask this question to yourself what is the context and construct in your mind not one scenario that is the mistake that most of you do you construct one scenario and come to a conclusion this is insignificant or this is significant or this matters or this does not matter my suggestion is very simple the moment you construct one scenario and come to conclusion try to construct an exact opposite scenario so be imaginative imagine some other context at least why even though that context is hypothetical or does not apply to the consideration that you have in your mind you will know that yes such possibilities exist that is the way to approach problems anyway I will be putting up the programs these programs as well as the other things on the model those of you who are interested in looking up more should read these and should once again read the definitions given in the kuhun which is the standard textbook but the C plus plus tutorial on the web is fairly exhaustive it gives all the definitions of how these functions are defined there are a few things which I have not covered here the additional aspects of object oriented programming I will not be discussing in the class and therefore those of you are interested in reading more should study these things these are friends functions and these are related to advantages of object oriented programming in inheritance and information hiding which is natural for all objects polymorphism and inheritance are two terms which we have not discussed in this course polymorphism and inheritance lead to a far greater power that becomes available to the class libraries. So, those of you are interested should read more about these two features of object oriented programming and the explanations given kuhun book itself explains some of these concepts although it uses extensively the same e z windows library to explain these concepts. So, while e z windows is not part of the final exam as I just declared at the beginning and therefore a majority of you would have discarded that from your mind those of you who wish to study polymorphism or inheritance from kuhun's book should remember that the examples given actually expand on the e z windows. So, you stop at this.