FALL 2018 CSE 241LECT 26 Part 01 – 18 Nov 29

because of this disease evil star the new new chapter chapter number 15 and this is a lot for the morphism and virtual functions for you morphism and I said a lot of time maybe to two lectures ago is one of the fundamentals of the audience’s program what are the fundamentals of the possibility for improving one live one two three and four the values we talked about what destruction then we talked about encapsulation and then we talked about in heathers and now you are talking of polymorphism okay just one polymorphism furthermore vision could be the most widely used feature of this of different programming languages although they are very widely used people don’t know much about them because most of the object-oriented programming languages of courses they don’t they don’t come to this point they cut the earlier circuit maybe encapsulation maybe some inheritance they kind of ignore a polymorphism but polymorphism is very important importance in terms of we use it’s often important also because by understanding how polymorphism works and by understanding how why it is important we would be with the vetted granted programmers advice that I would say that as I as I would always say chapter 15 is the most important chapter of all the chapters in the first 15 chapter of the book after this I think there is no I think this is the peak after that the other chapters they are important but they are not the most important chapters maybe can suffice I mean temples are always valid for other programming languages stupid the for the object-oriented programming the rules the chapter 15 is in kind of the peak point which is all let’s talk about for the morphism let me put this first ok we will actually get the polymorphism I will not I will introduce you only one key Evert the key word virtual ok virtual is a key word and other than that I will not introduce many nice syntax rules for the polymorphism you already know almost all the things but the idea is based the syntax wise you will not have to learn may need software idea why the study should be may be different potential late finding is something that you’re gonna learn without late finding there is no problem orphism actually without inheritance there is no for the morphisms that is not possible fission product or tool to make a polymorphism work we need in help ok and we will see why and this is important cause of these are very important concepts late finding virtual functions and abstract base classes pure virtual function is a very important concept and idea why they will learn them what they are and how they are useful as I said I will not introduce much news many syntax rules ok so this is this is a word meaning of polymorphism it means as in many meanings too long functions when I call a function in C or C++ let’s say function f when the compiler compiles the dysfunction let’s say I have an object all but caps when the compiler compiles this function to compile it knows which function to go okay by compile time by compile time I know that I know that which function I am calling okay this is in the header file it is in the documentation and everything okay this is called earlier bindings early binding okay I bind my function links

early in the compile time or the compile time binding I can say okay in some cases this mechanism is not sufficient for me in some cases I will say that I don’t know which function to call until during the runtime when I call that function okay so during the runtime I will decide which F function to call which have function to call it’s called late binding late binding okay so this is runtime binding compile time binding runtime binding and late binding does not decide which function to call until you run your code that’s why late binding is more flexible because during the runtime the your code makes a decision on which function to call it is more it is easier to write generic code base delays binding because you don’t have to decide which functions to call okay doing real time it is decided but since during the runtime there is a decision to be made the late binding is kind of more expensive in terms of city usage than the early binding option ah we never used like finding and I will show you how to use this like find new stuff a sieve medicine and actually I would tell you what I mean by I would tell you what I mean by how to make a decision during the sometimes potato that’s before it physically go back to polymorphism polymorphism means that associating many meanings to run function this function f when I say all that f okay it may mean many many things and during runtime I will discover what kind of means is what kind of means it will have infinite in one run this F may called one function in another run it may call in another function let’s say this is in a loop let’s say I am in a look something something something at each iteration of this look at each each iteration of this book this function f may cause a different function so this is called polymer cement as a little bit it will be very very useful okay so without talking too much about the theory and how they are useful and etc let me give you a concrete example this will be a very feminine example actually and this similar similar example is from the at the book in our homework okay in our work we have she is right let’s let’s put our shape classes in an hierarchy let’s say I have a base shape class and inside this shape class okay inside this shape I have some functions one of the factionism is for example area right area area it was the lady of the shape right but as the other one is 30 meters shipping services and the other one is maybe a draw right we have implemented all of these rights for your shape but the thing is that for the shape class for the shape class I don’t know how to implement area which is on the double I don’t know how to implement perimeters which is a double again and I don’t know how to how to implement this draw which produces an SVG line and trees for the shape okay because the shape is a not very well formed class it has some missing information like I don’t know what kind of shape is it the the good thing is the good thing is that I can I can drive new classes from this base shape class and I will I will I will make my own shape and I will make my own shapes for example rectangle polygon or ellipse or circle annotation okay so I can I can later redefine them all of these functions and I will have many area implementations many pyramids implement many draw implementation okay so let’s leave this part antenna let’s try to write some code on the shape class okay let’s write some code on the shapeless suppose I have I have a I have

a function name draw oh it will take a vector of shape pointers okay we Thank You Sven disgusting this is the identification yeah I can understand this so my purpose here is that I will take I will take all of these elements of this vector and I will call this draw function one by one in a loop okay I can do this I can do this writing for I tend to arrange for arrival so two matrixes I will do the traditional for statement integer I 0 I less than these are size then plus plus I and I will say that we I grow okay without writing any other things without writing any other without writing any others and without writing in other classes and other things just incremental shape class and implement this a draw all function and call this draw of the with a vector of shape pointers remember if I let me put them back actually in here if I have let’s say a circle a rectangle and a polygon class okay if I have if I have objects of the circle in my hand I can put those objects addresses in this vector because circle point is a shape pointer actually because there is an easy election right remember is a circle pointer is a circle pointer is a shape printer I know that so I can call this function with the elements of circle object the exact objects and polygon edges of the matters okay but every time I call this function as you know this elements of this vectors will point to different types of shapes and when I make this call during the compile time I will not know what kind of draw function this line will crawl because each of these classes each of these classes will define days on will define day on area perimeter and that’s good let me try to do it again caspere’s I hope correcting myself okay so just ahead and put it in here which is fertility okay so each of these will each of these will implement their own areas of two images and draw function each of them we do that and during the during g1 time this line has to make this decision am i calling the draw function of the circle or the rectangle or the photograph okay because they’re in the compiled time I say I may not know that okay there is no way I even know that because during the run time during the compile time sorry during the compile time they may not be any of these classes yet okay to compile this code all I need is discord also the code of the shape class that’s it right I don’t need any cold photos or even if I have the code for these three later after I implemented all of them later let’s say I implemented another class name for example Tammy and rectangle triangle triangle

I will implement all of DC and this code again we’ll continue working with the triangle objects turns even though it didn’t know anything about the triangle objects when I implemented this but I am trying to say here is that during the during the compile time when I write this code I don’t need to know what kind of deviations will happen on my class shape okay and I don’t need to know how to call the the functions of those classes I write my generic code this is a very generic code what do I mean by generics the generic college is written very generally it is not specific to any class right I write my generic code Vanda quadrants it becomes a very specific cause to say that I will call the draw function of this circle or the destroying order this rectangle or this polygon okay so this is this is our finger so here what is happening is I like this function to be called using late binding okay they find evil upon is late binding and what is happening it is polymorphism for the more person okay what was the definition of polymorphism again associating many meanings to one function here this draw this draw okay will have many many different meanings depending on the type of this object pointer okay this a shape pointer I know but it could be a polygon pointer or the rectangle pointers or pro pointer triangle pointers or Anglican pointers it depends on how many classes as you drive from this shape class but whatever you do I mean this function has to be there I know how do I know that there is this pointer point a class that has a job function I know that death pointer should be a shape printer if you had a shape pointer then you have to you have to have at least one area function one parameter question and run go Hawks otherwise there would be no inheritance right that’s why I said to implement this polymorphism I needed headers without inertia that is not polymorphism okay so we are so quiet ask me a question it might be a little bit meaningless we all see the meaning Lisa let’s let’s discuss the mechanisms of how the things are done and I mean let’s let’s see why we are doing this the latest okay let’s try to understand what we are doing if you have questions on those okay this is what I usually get that I first introduced for the Morpheus and I don’t think that is the India exam when I ask this I wouldn’t get the exact same this month nothing complicated as I said and by the way if I write this draw function as I have written before it should be early binding okay it is be bound early so I need to make some changes for micro functions very simple change I will say that this function of virtual function remember I said that there is only one keyboard addition in this in this in this justice virtual I will say that my draw function is virtual is calling decision should be done during this during the compile up during the four five numbers during this run time okay so if you implement this if you implement is like this this function will not change but you need to make some changes to this to this part of the area thirty meters and a draw you say that my draw function is a virtual function T which is a virtual function at the edge is a virtual function actually can write the same Club maybe this is more useful for you to understand that we try to copy it and let’s change it to no change in supplication and I will write it again this time I will say that this time I will say that this is total area okay different coveted reddish you will see what is changing its total area and what else these details are with awareness I will

calculate the total areas of these shades right so I should return at a double right and I should have a double the T the initial value is zero all right so and I should make I should make this okay he plotted payment and here this is later and at the end and at the end happy and just eternity eternity my chest and just eternity so this will calculate the total area of the totality of all the shapes that this is has for this holiday for this way sir let’s call one of these functions what do I do to call one of these functions let’s say in my main function let’s say I have a circle objects circle see I guess how to initialize the circles maybe the the center and radius I don’t know and what up rectangle rectangle R let’s say I have many of them one is one is v1 the other one is C to a default default circle then I have r1 and r2 I don’t know the parameters then I have polygon P 1 P 2 P 3 and this I have how many I have seven objection now seven shape object and I have a vector of shape pointers and the same as B so I will call T out okay give me the totality of these total yeah and V but I need to I need to put I need to put all of these stuff inside this message how do I do that how do I do that mustard let me try to do it again okay so you can apply right here B dot push back push back but up orders of c1 do the same thing again please up push that add this of c2 and continue this V dot push that add this off e to D okay so I have a vector of size 7 each of them is a shape pointers see these are all shaped objects and since this initialization is a shape pointers and I call this total area okay I could have called this draw go – okay okay so that’s that’s that’s basically but the thing is that when I when I inverted shapes that I implemented then I define the shape class I should do I should do something like this class shape okay inside this public cloud maybe you will write some kind of there may be a number of constructive I don’t know so the

possible implementation but for that for example for the draw function you will say that you will say that void draw right but at the beginning you will say virtual it’s a virtual function that means that when you are making a function call to this draw you will make your decision during the runtime okay lay down to the runtime during the runtime when I make it home like that okay you will know that whatever object you are being called on is a shape object you know yeah but he’s on is exact type so I get this object find this exact type the pizza polygon object and for the growth of this polygon this is the rectangle object under call atrophic of this rectangle square so that’s that’s what it means I was a virtual void draw and virtual double area detection this is how you define your shape class and then you when you redefine all those functions areas and the areas and the areas and the drawers or the perimeters okay you just redefine them as usual like you are redefining a function and that function automatically is virtual you don’t have to keep it is virtual keywords when you are redefining them and this will happen okay by the way I didn’t I didn’t take a shape vectors or anything as I just hit took I took shape for instance there is a reason for this and we will talk about that here what I am trying to say is that this these functions function draw and areas they will not be always late bonds okay sometimes they will be decided during the compile time and and we will see what time they are called the three days they are decided during the compile time or during the run time later on and that’s why I mean to make it virtual to make a plate bindings highpass is going to the devotees okay any questions for Christ I need at least one question I have been talking about half an hour nobody asked any question maybe people from YouTube yeah let’s let me see maybe if they have some questions are there any questions how do I see my live feed this is stream nice save lives say something okay any questions what watching a just one person okay maybe if i refresh my page i will get something better okay let me see how that means i alright so some people get in five people and database besides us for you what is a nice text to you that you have okay yeah we have this read did that list really redefine that print church but during the compile time I need which print check I am calling because I said that this is an employ this is our employee and if the early employee is object either print check I know that it will be the early employees to interested me didn’t I mean it was not the case that we didn’t know which function to call in the Pampas I’m doing the compiler we always need which

function is going to be called okay doing this during the run time we didn’t have to make any decisions it is kind of similar but I’m in this to catch you the very first question occurs as a certain things that’s why I’m saying that you will get you get right and you get confusion why are we talking about this is I mean we have been talking for the last half an hour and wilderness in essence that’s a very good question is asking that we already needed no you didn’t notice okay it is something important the previously when we then we call a function we never raise up we never made an object of shape classes or we never made an object of employee class right we never said employee does princess we always say that only employees appreciate all the commissioners without Chris Rock okay we did not call anything using the base class name admitted that on purpose because with the base closings III know that I was run into some trouble because this is the mean but what would the meaning of that call should I call the base classes bro function or the green check or sugar for the drug presence in his life I never I never created that kind of a situation career I was really careful about that yes so I was hiding it and you thought that like Angeles or Easter so so this something is this didn’t have to talk about let’s have some fun people have those kind of questions or maybe some of your outside too and maybe I taught them we will talk about this next chapter example what you didn’t realize so far I can watch the videos okay let me let me go back to books like and try to try to explain the same thing in terms of books example and and it will be more Keynesian Kalidas and you go back and read the book and you will understand the things better okay so that’s the meaning of polymorphism associated many meanings to one function I don’t know which ad I am going to fall during the compile time the the call will happen during the run time okay and during the run time I will make this decision which one of these four or which one of it is not correct to say which one of these four okay it could be which one of which which function I am going to call because there could be many other shape objects shape class relators derived from the space class and when I write this quote genetically I didn’t know how many classes are going to be right from that shape flat I just wrote this code and I have brought to Scott and that’s it so that’s that’s why I say steady quote I write very generous very reusable code the at the beginning that design this class and I don’t care how many class the vivid rise for my class my court will work on all of them because each of them has to have an area or draw or the perimeter function otherwise it wouldn’t be any nervous that’s why we say that polymorphism without in essence not possible okay that’s my recipe remember remember I told you something about Windows application programming I said that that is a well this is not exactly true but there is a Windows application base class okay when you write a Windows application okay you need to provide you need to provide your clothes function and your redraw function and maybe your resize function okay so let’s say I wrote a new very good g2 calculator class an application inside my GCE cockpit it’s like a calculator application like this one there are very nice features with reference creatures yeah so it’s like a calculation like this one see the disk operator applications understand resizing right it’s not how to resize it knows how to

grow result because when I do this when I noticed so it draws itself in a different way or when I do that it throws itself in a different way or when I when I do this so it is hidden behind my current window when I suppose it again he throws itself so it’s knows how to handle stop so I have to I have to say I will implement all of these by the way it knows how to close itself to three draw and three five okay so operating system knows that if there is a Windows application that application should have to provide this application as to as to provide the implementations for these for these functions so maybe this Windows operating system then maybe this Windows operating system has a function named Royce close or if I call this function if you close all the applications that are running so when do I go when right for the school also function as an operating system then I am shutting down my system right so as an operating system I have the pointers are all of my application so I get this vector of the Windows application objects right it is Z and what do I do in a range for loop what I did is I would say then apt for inter P B what writing is what by resonance P not that but arrow yeah cool so when they first came up this windows idea in 1989 or 1990 they have written this code and this Court did not change at all right for the last 30 40 years we have been running the same code of course I am just making everything simple this is in reality things are not this simple but the idea is the same so operating system it was designed an implementation since – when I shut down my computer my operating system I call the scroll function closed off function and the oldest close all function that is that called the clothes-conscious all all all these applications now the matter if it’s a issue calculator or the windows calculator or the Microsoft Word or your or your favorite game application doesn’t matter they all have to have this closed function so this is again very generic code okay this is generic code the same thing for the redraw the operating system says the application say that ok your window is not showing you have to draw your self ok all this other stuff called zero action operating system for the redraw function as an application programmer I don’t need to do that I don’t need to deal with these these buttons or that kind of stuff let’s say this is explorers and and there are bottles in here right there and other other stuff there are many buttons and there are many things as I can do they say for example there should be there should be another convenience here right here something like that ok so operating system tells me that somebody’s trying to close you so I am calling your cross function or somebody is trying to resize you I am calling your resize function all I need to do is just provide these functions to the operating system and I don’t need to worry about the rest so this is one of the most common uses for this photo morphism and enhances so you drive your glasses from the base operating system for us you make your own applications and you fill in the missing parts might have a missing part your your reader your resize your many operations etc and the rest also stopped in 300 by drop AC system and for the operating system this has to be Z or as is you’re calling the appropriate functions of each needed right press but the operating system that was designed when you first compile it did not know

the details of all of those applications across applications are written the operating system is compiled and swore to you okay I hope this makes more sense right now okay and the same thing with the graphical user interface programming and we’ve amassed an example for this with the plus button we will see if it’s a Java there is a complete section about a quick programming and notice okay let me let me move on actually it is giving me the exactly the same example figure the example is say that classes of several kinds of videos we have surface rectangle and in the rectangle I have height width center point for the surfer data center point and radius so each class has a different type of information in it but each class has a draw function okay and if I Drive all of these classes from the same base class and that base class defined the draw function as a virtual function then I I can make this I can make this polymorphism work when I take X angle iron and other draw this is not late binding this is early binding during the compile time to compile they know that it will call it will call the draw function of the oh crap this one is not okay it will call the draw function of the circle plus these are not even even if even if I define that the draw function that as a virtual function okay virtual cultures doesn’t matter these will be these will be decided to in the compile time okay so there is much is nothing new here okay the thing happens the thing happens when I have a base class and using the base class pointers or just yes if I have a pass class with a figure okay and let’s say let’s say the draw function then I on a on a figure four I called the draw function which draw should I call a draw function of the base class or draw function of the drive button which was a lot better already if it’s a lake binding that is above the product through a function of the price class if it is only binding line so called the base classes draw function so late finding or sometimes called dynamic finding virtual functions it does the compilers don’t know how function is implemented I will wait until it is using the program okay I will get the implementation from the object itself when I say object pointers and function name has I don’t know which function to call this because this F is a virtual function this is the polymorphism going on yes okay I will look at this object what type of jacket says I will figure out and that object will tell me what kind of aspersion I’m going to fall okay so during the run pipe I am making this decision this is called late finding or Chinese writing yes and virtual functions in from update’ find this again I have to be we have to be careful here that if a function f is a virtual function that doesn’t mean that it is always it is always decided during runtime you have to use that function with a base class pointer or base class reference otherwise like in this case is for D it’s a bit doesn’t matter it’s over the air it will be a early binding example okay the book has another example same class I think I will show you this one on the actual on the actual code from the book what are these I don’t I don’t

know what these are talking about times an integer okay I believe up like this I think these are all from the others let me close them all let me Oakland yes this is chapter number 115 chapter number 15 let me open everything must be an example in this chapter okay so this is my tale class very simple class I am selling something okay I have a store I am selling something whatever I am Telling it has a price okay and this is my cross track fish I guess the price and I set the price get priced at prize okay but then I said something I need to write something to the bill for track now I need arise happening to the bill and bill is the same as the price because on top of the bill I have to add the tax ID card area or something like that and if I have a discount in the rest if I have some discounts I have to do that discount – okay so this bill amount and the price is different and depending on the sales hype if it’s a discounted sale sale my bill visited it is smaller if it’s a regular sale then my girl will be a little bit larger that’s why they define the spilt virtual because I’ve a hair I will drive I will drive some new classes from this sacred and I was a me I will make my decisions like that for example this one different fail okay for the discount sale I have a price and I have to discount the amount 10% 15% for every case and I have my certificate that gets discussed of courses – data members and I am redefining my build a member function okay remember this tell bill is a virtual here I can write a virtual or not doesn’t matter doesn’t matter if I write with you or not my bill is automatically virtual in the base class this is virtual so I know all the functions only bills can behave as s virtual function only polymorphism can happen let’s just build a function which is let’s see the implementation of sale class nothing interesting much constructors prices zero constructors price should be straight or equal to 0 for J and the bill so the bill just it’ll price for the base class so in this case my bill is the same as the price regular sales get price and set price and savings will be kept this one the function savings and I call savings what time is my training how much may I save by making this a comparator how much high state I am completing myself bill with the other bills so I don’t know what kind of function other bill has the other is calling because bill is a virtual function okay and I’m using the base classes reference to call this bill right base cross the Japan’s to call this bill and I don’t know what kind of function I am calling calling during this the compile time this will be decided to in this run time this is okay I know I am calling the bill function of test the calling object I know that but at this one I I don’t know other the fail okay cause of death may be decided to in there during the run time let’s see an example actually see an example or the front sales diplomatist is no problem see how do i implement this one discount sale bill it takes fatally frowns divided by 100 and it multiplies the price with this fraction and if you try

to solve it is called the price by the discuss among eight soldiers and this is my new function so this is a sample tale object and this is a discount sale object and what what is the bill amount for this one this is hand our guys the Vil amount for this one is $10 what is the bill amount for this one nine dollars and ninety cents right because it will it will discount 10% of eleven dollars it’s really $9.90 so it didn’t give me anything here but let’s say if I write this one C out see how simple that savings and this one but what this muscles muscles this line point of about this line print on the p10 – 998 is – minus zero point ten dollars right so if I but if I do this maybe this is more meaningful this counts simple so this is a that you have a savings of a sentence okay but without making this without making this call during the compile time okay doing the compiler I didn’t know I didn’t know which function I am going to call I am going to call this is savings function okay I I did notice during the run time after I called this function I figured that that other is a sale objective or the other is there it has simple base say for us this one is already air this one is already a discount for us so I made my decision during this from time and I printed out the box so check this for there and let me see if you have any questions yes we are running in the example that I gave you here I always use the pointer okay then if you if you like to make a late binding or the polymorphic call you need to make n you need to make this true there are no Google late binding okay global functions are always decided during the compile time okay only the member functions are decided virtual member function I decided to index run time but not not all virtual functions are decided to enjoy run time you need to contain in some specific ways you use the base classes reference or you use the base classes a pointer so make the scope otherwise you will not get any you will not get any polymorphism out of your call okay it has to be either the base called pointers base class for in search for base class reference okay as I said before without the base class there is no polymorphism or it is not a finding our funding without the base classes with the base classes and the appropriate usage of the keyword virtual you may have you may have your late finding okay so that’s a sin tax related rule that’s important but more important is the idea why we are doing this what we are they how we are going to use it in our daily program is life okay another question okay if there are no questions let’s take a little break after the break we will continue if there are any pressure no no pressures for people watching

weather very have fun being good in taking the Guru’s VI

okay let’s continue from as you said before this late finding or dynamic fighting happens only when you make your functions virtual in the base class and when you call them you call them using the base class and pointers on it base class

references okay so this is how the polymorphism works as I said before this is almost like these features of the object resin programming languages when we when you when you develop anything using a graphical user interfaces or the modern operating system applications you are always using you always drive your new application from a base class and you redefine you redefine your some of your virtual functions so that the basis genic function of the base wrap can work actually these these are not the first examples of our genetic code I mean this one very generic code is it closed all the functions on if I like to let’s say see when I do this then I do this by Japanese so it knows how to resize them into these smaller rectangles and show them to you right so each function has to each each application as you know how to resize them to this smaller versions right so if the if the if the application doesn’t know how to do that then operating system cannot perform this application they cannot perform this this operation and which is which is bad for the operating system that surface inches and say that if you are not providing me a resize with function then your your your your your code cannot because it is a Windows application so it’s not comprised okay the component is a rejecter contain that you are missing this disciple if you look not if you do not provide any replies function this generic could be wrote other types opportunity code before like like remember this I may talk about it quicksort I think it’s the English quiz quiz for type Q sorts and linear search remember this a linear which do you remember these functions they were very very generic they can work on any type of arrays right and they can use any type of comparison operators and these are these these are written like 30 years ago which are good use and everything still I can use these functions with my new process such as rectangle class or the rational class even though these are thirty-year-old functions they have been there they have in – without any changes they sit there and I just call them and and the same thing as this one the scores off okay but we have to be very careful about this quick quiz you remember the prototype of the resource to search to search and see okay here it is remember to say that I need void pointers okay and the product comparison I need such a function is very very generous this is religion it is very easy to make mistakes on this one this is function writing this function requires lots of thinking lots of type conversions and etc right and I need to pass all of these parameters very carefully even though this is a very generic function there’s a very generic function it is difficult to use this function it is not the case for this one all you need to provide is a vector of windows application class printers and all the rest is handled by this function and it is not very easy to make any mistakes while calling this function because you know that without any shape classes you cannot call this function right you cannot call this function with other types of objects and there is always there is no way to make any object hardly section here and although object has to provide this close function why because they derive from the space class so I made that type of generic function programming delay code writing process easier more robust by using this polymorphism and inheritance mechanism object-oriented programming okay everything is right but only the sulfide is when I make this decision during the run time is crossed with some CPU time because I need to make this

decision when I see this P arrow close what is the type of fear I know I know it’s a we know that but it could be a GTO corporation application or it could be a it could be as multiplication Excel application mind severe application whatever it is so I need to figure out what kind of application assess and I need to call the specific course functional test applications and I need to do this to interrupt that’s the only downside of the disadvantage of the polymorphism okay so since C and C++ are very healthy in terms of CPU stated as being very very efficient and they kind of they kind of they carefully about this usage of this late binding in Java everything is boundless with an FSA for severity is virtual to make them not virtual you need to you need to follow some other some other steps okay I mean optional you have lived your life your C++ life life without knowing virtual functions okay virtual is something needs because it causes some CPU time for the job people they live daily life with all virtual part of all the times until somebody’s has been with you by the way these are virtual design this cost you more if we send a lazy deficient yes in German 30% performance is not that’s bad okay there’s a rule of thumb if she takes 10 seconds to run a program if you write the same thing in C it was policies like 15 seconds for the Java it’s like 20 or 30 seconds but it is as well most of time not all the time sometimes Java has very powerful libraries and some real powerful structures sometimes your psssst sometimes for some kids faster than T for some applications but if you write the correct code you see it is as as it is almost as efficient as they ascend implementation right and you cannot be more efficient and s and implementation if you spend enough time they say that okay the writing everything is assembly good but it takes lots of time if I am going to spend like three years of development to write something in assembly and if I’m going to write on the advance divided program in a corporation in an enterprise why am i spending that much time because I mean that’s why I find a unique time for the development is too much very expensive it says a software is not that critical that’s ideally language like Java for Java developers time is important because you are going to develop lots of lots of custom software like the enterprise software and not many people are going to use it maybe hundreds of people that use it and every day you did Neal initially modification then programming language like C or assemblies are not good development environments photos okay so let me go back to my twice again figures example another example this might take class okay say that the book say that I am doing this automotive parts and I need to complete this cross daily sale okay the first video I will calculate everything for today okay oh come from individual bills it is not the price of my good it is that this is what I write on my deal okay that the important thing is the bill because bill is how much I make money on that on that on that on this item today I’m selling okay so this is virtual there okay virtual there and I have couple savings as I said it’s taking careful take the refers to the base grass and refers to the base grass and on this Jeffers I may I call this sale that’s why I don’t know which fit is called is the base glass of beer or dry grass with this I will not know that until the vampire hunter the runtime okay but if I have if I have this sale

as on my grid sale as hand a Scorpio this is definitely not late inviting but this is early whining this is early binding because I am not using the base class reference why I must believe in the base class of pointer this is affinity early binding but if I have but if I have something like this sale P address of s then P p.m. and I don’t know I don’t know I mean because this P might point to a sale object or to speak my point is an object that starts from the stapler I don’t know and the same thing is a sale reference are as r dot dl i don’t know these these two both wash make binding this p could point to a base object or the right object this art could be a reference to the base object bonus for a driver I don’t know okay so this is my say this is this is this is my base class and these are two interesting functions one of them is s the otherwise the global function actually say this global function takes two long just say object references first and second it was the first bill and second bill I don’t know what kind of bills I am going to call here because the base classes reference it could be dried to a spill or the sales process and this will be decided on during their during this time as I said before again this one may be devices decided on this during death during the runtime also in the stage of late finding examples they will take more time so when I say when I say this one P arrow bills when the compiler sees this is delayed virtual function yes P is the base class or J pointer yes so the component is the internal census this is what happens this is an object is processed in of the crisis of pointers go to that object find its type and depending on is that call its bill function that means that there is time information inside my objects that indicates the price okay so I need to keep that information in my object in my object each object in C++ for most cases as they know these types so the runtime type information it says are 30s from type Chi types are TTI this information is embedded inside the objects of C++ classes unless you disable it there is a switch in the compilers you may say the compiler that don’t keep any RTC I data in my objects some types of information objects so that I can save some space you may you may you may reject this property so in that case you won’t be able to you won’t be able to achieve this for the morphism of a price what happened okay so then the other is another downside of late finding is a small CPU time I take the more object space doing this on the on the memory or change that we test this one again is a very nice code thousand matters I write this code very generically as there could be a discount sale there could be a commission sale that could be a tax sale there could be a pity sale okay have another this discord over this one yes if you raise these two characters in here then you take two sale objects okay these are sale of this so this and these are the references other pointers first and second are sale object objects and the big function are the bins for these two mill functions are the bin functions of the base classes so it should be

decided during the compile time it’s not a final yes you can still call you can still call your operator lesson using any type of sale or just including signal click or discount objects okay doesn’t matter you can call them but the function that will be called is the base class function bill okay that’s a good question I kind of answered that question previous what exercise enhances if you don’t make any late binding then if you don’t make any late findings and revising will make the decision providing the advantage in by depending on the type of this object but in the type of this object here it is base class save objects okay base class save objects or how do I make my decisions which function to contest I am making this decision doing this from Python when I am compiling this course then I will just call be off the baseline okay but if it is a reference or it is a base class for inter done and it is hard to rush virtual functions then I will have to delay my decision up to the runtime until a long time when I called this line right easy to this line and I will know which one to quote and every time I execute this line I will make this decision that’s right this way Express okay so there are the classes the represent sales of single item decided to count all charges and brochures remember this one this is our near is reserved Roberto if your fortune is virtual then that means that the desired object rod is intending that function to be using a polymorphism mechanism late finding so we should be careful a prospect okay okay so this is my discount sales and I am or I am here bill is redefined by the way if I redefine a virtual function it’s not what the definition is called overriding you are overriding a base class function if you if you are if you are redefining a base class base class virtual conscience we call as overriding with on its visual quality we don’t call it a redefinition some people mix it up okay the definitional already is the same thing they say that what about the book is a shelf a Baptist is a book say that overriding is different from the definition so we have overloading we have overriding and we have the definition say we have three things although things we learned at the beginning of the semester giving the same name to different cultures and the essential should be different redefinition we learned that the 15,000 you are redefining a base class function without changing signatures overriding a special form of Education when you redefine a base class virtual function before it’s overriding my testicles over X okay so we are all writing this base class b function of course our constructors and the this is injected for this one and our need build is this and remember the base class still itself it turns the price this one makes this discount okay qualified virtual does not call into actual function definition without right is because it isn’t that what happens if you put it into position so cable TV is used on this in place okay so virtual function base class automatically virtually drive process the fact that I didn’t write that virtually it doesn’t mean that bill is not virtual is virtual because it is referred to in the base class if i drive a new class from this discount sale it’s been busy virtual again doesn’t matter if I put virtual or not in here because at the beginning in the base product is virtual okay so this is what to say okay this cult members function bill implemented differently than sales and the member functions

savings and a global operator overload when they use they use this polymorphism fixed rate by these features of the speed function what you saw this is this is how the things are working by the way this is kind of say of D 1 D 2 D 1 dot C D so this is not it is a virtual function followed it’s the late binding or order binding example later early this is really right because I am not using base classes as reference source for intervention things or dissident and I know that definitely saving is that oh why why did you say it is early it is only because the cardiac surgeons such a treasure hunter it is early because inside the savings inside the saving I am NOT passing no forget about what I said let’s go to our savings example because saving these other virtual functions it’s a savings that it’s a it’s a it’s a regular function non-virtual function but inside savings inside savings that is my savings ERISA I call the bill function but there is a virtual function and since I get the base class reference then there will be there will be some polymorphism going on inside this saving functions of saving is the 50s the early binding yes but inside the saving function that will be have late function late functions late binding Parsifal okay because as base class which and base cross-references defined endure so how the hypothesis has been happening out how to how how did they implants this as I said before each object knows what type of or class it belongs to okay when you ask an object may ask an object and that object can tell you that what they can tell you what kind of cloud is long so there is this C++ class class okay class is tight let me try to pile it from the recommendation don’t wanna go there okay there is a there is a function operators that is the type of the class I am trying to find the path I’m just trying to find that correct documentation get class in Java and it is built inside a case to define a type ID a cat named type ID a is here an object I would get the link 1 plus PI pi okay sir operator and you get the type

ID here’s an example say base cross right cross second date it is a virtual function here ceases they are doing exactly the same thing this is base press and there is address class and this is base tube with a virtual function foo then this is Drive cloud base – okay so I’m going to use a type ID my integers use this okay my string yes last name this will tell you what kind of object it is okay what kind of objective so this is possible this is C++ mode for most of its objects what kind of type it as it is it is it isn’t inside the object since the obvious or the type name they hide they know what function they are using to remember our circle class chocolate when I ask an object what type are you it’s a that I am it’s a circle object so we need your draw function let’s say this is my draw function okay so let’s say I have a rational object inside myrational object what do I have I have an integer nominators and other integers denominators and inside my object I have another pointers that points to my type related stuff like my other’s work to a function this is this table is shared by all types of rational rational valuation tools they all point to the same table inside this table I have type related racial religious information like the name of Federation of the streets are okay or it’s virtual functions let’s say in solid state is not station let’s say it’s the rectangle rectangle okay for the rectangle I have a pointer here for the draw I have a pointer for the area I have a pointer for the parameter also I have my name etc so this table this table is common to all rectangle objects and by keeping this table and pointers to disabled I can achieve this for the morphogen initiative formal sometimes this table is called virtual function table B table the table or virtual function table okay so by keeping this extra information for the morphism is possible that means there’s very slight it’s good to have this kind of capability but keeping all that information using them updating them getting them it costs you some CPU time and the memory too so the the polymorphism is up the other country it’s very useful very important of the apprentice program in first of all right so dating until the run time but it is not that a cheat and we’ll talk about that yes Leo talking laughter actually either lost the same question for you and I think you can answer glitch I will prepare that the question I have asked and I think you should label selfie okay this is how the virtuals are done overriding and redefining are two different things virtual fractions if you change your base class the virtual function is called overridden overriding if it is non-religious or the definition reduce energy defined over resonance redefined are two terms they should be used carefully okay we talked about this already in C++ 11 and the same thing is in Java 2 this is need keywords overwrite all

right when you say class stay in the base class my bill is a virtual function okay and the drive cost you say that I am overriding ass I am overriding at base class virtual functions this makes it explicit if you are making a mistake if you are making a mistake when you say all right but there is no virtual in here the compiler will give you some message saying that you cannot alright a non-virtual function okay to write a virtual part it’s alright a function it has to be virtual in the base class so this is called alright and you are saying that I am NOT redefining it I am NOT overloading it I am all writing ok it makes sex SSSs functional right bills in the save a file okay so this a nice keyword so if what if you don’t what happens if you don’t use it nothing we serve will happen if you if it is everything right that there is no problem but if you made some mistakes for example if you are taking that you are redefining it or you are all loading it then the compiler is not happy but if you think you are overloading it only are already defining it when you put that all right keyword is incompatible give you a message saying that what you are doing is not right it is of course this is what you’re telling me okay so this is one new addition with C++ level at the beginning of the choices at the beginning of the chapter I told you that that we’ve only one new keyword virtual but it has the or C++ palette this is the second keyword and there is another one final Kira say if you don’t if you don’t want your if you don’t want your base function to be overridden or redefine you define that function to be final okay if you say if you say this function is final and you nobody should redefine our override it you put a final keyword at the end of the at the end of the function definition the base class and in the drive classes you may not override it this will produce a compiler edge I think somebody else with this lovely for the previous if I don’t remember somebody found that if I don’t want my function to be redefined I don’t know who said that what should I do and I told Alice wait until next week so again this is T plus what level teachers this was both of them this one and this one was in Java and they have been using it and leave the editors to C plus what 11 have people people are using to prevent any redefinition already defined very functional yeah Julius you can do the same thing to you just called you just call that I am calling the base class and being function in that case there is no problem or vision because you are saying that this base classes coffee okay is good fun okay related to listen to this final and alright keyboard the book doesn’t talk about it but let me before he would let me know let me tell you something that sequence was introduced yes if both here you may you may know this is not is not what I was on anonymity fault difference not default constructors defaulted contractors defaulted

contractors okay I don’t want to go to stack all over in C++ 11 you may use the default keyword in here it say that I have a construct it is virtual and we’ll talk about why this virtual I say that is the same as the default constructors for this ability so you may you may you may say that I have an assignment operators I am beginning it in here but I’m not going to implement it because this is the same as the default assignment operator so you can you can write this default this is sign equals default with your quarters your copy constructors okay your assignment operators and your destructor because these four things they have this default implementation so this is another dessert is something related that it was multilevel introduced yes why do why do they write default in here because if you don’t try to is already default you are making Texas’s you are saying that default constructor default is time to talk a business that’s what you are saying I do this is on purpose but in this case they did this because they want to make this base discharge the default let’s let’s stop the process why why should I make my stage the structure default let’s talk about this and you get the answer and let’s see why let’s say let’s see I have this class a and I am driving a new class C okay and this class a has Class A has many things and one of them is the Constructors the other one is the instructor right and it may be that some characters 4 inches in it the size of this circle is important for this class be for this class be okay maybe has some integer pointers so that’s why it says it’s on destructors call it a call to construct – constructed a deal first maybe it has it a copy constructor net I’m professor – let’s see this one can I write this can I write a need a object new a no problem right I can write this no problem and I can delete it now I cannot write this routine after this yes ok so I can say the lead a when I say the least a what constructors writes must’ve stuck this way – cause of course this one but remember I am using a pointer I am calling a operational apprentice like kind of interesting as is visible to polymer how about this one can I write this one can I write this one the pointers me give be objects of course I can write it and I can delete it delete the pointers respond to and it is this this TD circus maybe photo this one but how about this one a pointers apt our new be delete apcr Vista stock this going to cost which disappears because the writing is to cover this structure to be right because he has some housekeeping to do with his integer pointers and it has its own how get into the bus the writing is to cover the structure of this be because this is a big object even though I am pointing it to buy an April interest right the city object so when I call these delete a PTR this one should be called but what do you think which which one is called unfortunately

unfortunately is this activity called why because they say that a three piece here is an type of a SS right here so I’m going to call this a covetousness early binding if I want my destructors to be a to be to be to behave using the late binding mechanism that I should say my structure is a virtual destructor I should say virtual a okay in this case when I say just a PCR which it is a pointer to a base press which frankly I’m calling the destructors rap this act is a virtual so I should make my decision during the runtime so I will I will delay my decision during runtime I was determined the type of this object at this point the apt is pointing to be homosexual I will call I will folder I will call the destructor of the class P that’s why he may are distracted virtual if I thought if you don’t make our destructors virtual in the base class then I will run into this kind of problem this kind of problem if I am deleting something with the base classes pointer and I do that a lot remember am I you remember my clothes all functions remember my cool role function if I shall find it where is my porno function for this beginning there it’s obvious close all functions when this one is making this closes and lately for deliverables application then you leave them with the base class for insertion the corrective structure should be fortunate so so it should be all base class it should be in the base class my destructors should be displayed as as as virtual so the rule of thumb always if you are doing in a hierarchy always declare your district as virtual okay basically your destructor watch what is my district is empty what do I do if my district is empty that’s why data this way my district Rhianna point right that is default to structure but I like to say that in virtual that’s why I asked okay this one is the questions say that this one and then this one is behaving the same but it is that I can get the same effect request button using em is there any way in which these to the fridge to behave differently what is your answer this one this one no they are the same thing because you are default instructors doesn’t do much okay I just called the B flat instructors and both of them the other thing things up go to the god Stack Overflow and try to ask your questions there and try to see what kind of answers you get from the front other people and try to contribute yourself – okay and you will understand lots of stops because these problems are not really the infrequent problems is there D happen a lot and this the same problem is happen to you too and sometimes you will realize that you didn’t understand something and sometimes you realize that but you just met the beginning of the college that I see sometimes people are delaying database quest and virtual base class distracted virtual right so why I mean to be able solve this kind of crisis you need to be kept cold right without we have to put you you you not resolved first as I said before there is no way I can tell you everything about C or C++ in the inspection the one thing is that I don’t know anything the second thing is that we don’t have enough time okay we talked about all right we talked about final and even though the book doesn’t talk about it he talked about different keywords I need you to open a default keyboard that the book says if the virtual functions are so is it 11 so I am sorry tamalitos okay now let me let me let me talk about this and let’s finish our virtual function might not make everything worse because they uses more storage in memory or case remember that cable rated my

table there was a table here virtual table okay that table uses lots of memory okay and during the late fighting on the fly my program runs through the every time I might make a late fighting and making any decisions saying that versus the type of these objects it is rational so that is the virtual function table for a regular original its ears so I am going to find this one okay so it is up it’s not cheap this is not cheap in terms of C C++ money okay it is cheap in terms of your money because in Java there teach in terms of cities I don’t need it why do I want money when you don’t distance I mean by default you don’t know your virtual keyword retrogress and it don’t usually so I mean C++ tries to hide this virtual functions you have to be the other nice virtual at the beginning of the function so you are doing it yourself you are saying that I know at the ending is virtual in Java the jatropha this Java you say that I know what I’m doing this literature so when you don’t need it you don’t use it because using it although it is more flexible they use a lot CPU time and more storage okay from right at all the answer is s in Java they are supported by why not why not use non-virtual function okay I think I’m out of time let me tell you this one and that’s what I said in the previous lesson you know I know that you don’t have in other classes they have to run across cooperating with me you may say that I have a virtual function bro I am NOT going to implement it okay how do you implement the virtual function drone for the base cross shape how do you implement the area function for the base cloud shapes you cannot you cannot implement it right that’s why I said that you should not call this function aegis-1 right so there is a way you say that I don’t know how to implement this function I am NOT going to implement it and I am saying this equals zero okay so that means that that means that if somebody calls your function draw on your object there is nothing to go so what does the what does your program do it can’t do anything that’s why if you have a function like this in your class you cannot have an object of that class you may not have let’s say in my shape class I have a draw function if you say shape as the compiler will tell you that you cannot make an object of cloud shape it is unprofitable okay so why do I have a class like Jason if I can’t make objects of the shape remember we are using shape as a base class I am NOT it is not my intention to use shape as a regular class but I am going to use the zest base class okay these kind of classes are called abstract classes why they are abstract they are practical you cannot have you cannot have objects of those classes either for abstract web to make an abstract class in C++ you need to have at least one pure virtual function is spiritual it is virtual because there is a virtual keyboard in dispute because that is this equal 0 syntax error okay pure virtual function top come out functional in Turkish salon function soft have some malfunction kind of difficulties a a few virtual function so usually we make our base classes absolute classes remember our employee class we said that print check is not incompatible so why why not make a print check a pure virtual function make it through virtual in our base class shape why not make that draw premature an area pure virtual because I don’t know of the intimate them okay you attend the compiler that this is not a complete class the a bunch of abstract and concrete class so you so much in Turkish okay in the Photoshop classes I cannot make any objects for the complete process of course since they are hoping

I can have objects of this complete process after now all we did was concrete classes but by this notation you may have your abstract base classes and they will use the supported evil uses a lot of course without derivation without in essence abstract base classes of make any sense because you cannot make an object process crust which is abstract and without further morphism virtual doesn’t make any sense so if this is something like this in a basis that means that we are going to drive something from the base class and able to say values polymorphism and you have to you have to you have to override this one otherwise in your new clothes in your dress class if you don’t overwrite this one what happens if you don’t overwrite this one your new class becomes an abstract classes right it’s not all religious so remember that same thing again this one with notification so usually these are virtual and pure pure virtual so without or without all writing these three functions or five functions and functions you cannot have a Windows application you have to provide otherwise you may not have an objective-c to accomplish class okay these are defined usually these are defined if you virtually okay so that’s it for today on Monday we will continue from this point on as I said this is a very important chapter needed then the book discussion is kind of okay– is not that good so it is from other sources to try to see how the people are using actually read second flow the questions about polymorphism and virtual keywords and etc okay and come back on Monday with lots of questions because if you don’t have any questions right now you didn’t understand anything okay and by by by the way you look to my right and to my chambers and to myself this is not that much idea okay as I expected it’s a different world and the idea is simple it’s kind of trying if you get the idea you will you you’ll know that okay you need some time to understand but ideas okay that’s it for today see you on Monday yogi notify your establish the default life – animal number of successful members example folk show that as I can change rush point another are already similarity function for VAR a bad day I see our functional pointer watch every citizen member function not an Evo finger on my counselor he did your prediction jaan-e-mann behind a functional made for Jonah we’re home from the initial assault and easy to neighbors and that for just all bewitching music is here is better than the creative