Taught By

Ira Pohl

Transcript

[MUSIC] Today, we're going to get deeper into the object oriented programming style. So we're going to look at more C++ classes and how that builds on in an object oriented manner. We're going to look again, at constructors and destructors which are these critical methods that both build and deallocate the new extended data types. And then we're going to discuss our next significant homework assignment and that's a homework assignment based on graph theory. So again, all of you came in having a background in computer science roughly at a sophomore level at least and you may well have already seen standard graph theory algorithm. So this may be familiar to you. In any case, I will review all that material. So even if you have forgotten it or haven't in the past had an adequate background, we're going to really understand Dijkstra's shortest path algorithm and that's going to be the basis for our second homework assignment. Objects. Objects could be anything. So here in this little example, we're thinking of shapes. Shapes could be boxes, polygons, text shapes. There can be operations like delete on them operations that involve counting the shapes. And so if you wanted to build a windowing environment, what would be our set of widgets? What would be the operations? What would the user expect, the user of this package of routines? How would we properly design things and keep the internal details hidden away. All of that is what you have to think about when you're an object oriented C++ programmer. Again, a best guideline to doing this thought process is to know what happens in the language for native types. So with native types, we have to fully understand what happens when we declare something, even a simple object like an int. What does it mean to say int i, j = 3? Well, we typically do that inside block scope. Block maybe inside the main function or it maybe in the auxiliary file scope functions. So we're declaring here, we're allocating. We're initializing. Now for a simple native type, all this happens automatically on the stack. We have to replicate that for our user defined types for our objects. And again, as we started with last time, we saw we had a special method for doing this called a constructor. So just for review purposes in the C language or in basic C++, what is supposed to happen here where we have nested blocks? So here's a a main. We have two declarations, i is initialized the 9. J has initialized the 3. We're going to print what goes to screen. So that's what we're expecting you to do. This is going to be very similar to the kinds of exams that I give. This is a question that I could readily put on are in our final exam. And then secondarily, we have an internal block is not, here's the internal block then the nested internal block. Now, I actually would have preferred an indented style. This is not, you should reformat that if you're going to try the code. So that the internal block is indented. So that you can visually. That's again, a convention. But it makes it more readable, you can visually see the inner block. And so in the inner block, there's a redeclaration int i and we're asking what that prints versus at the outer block. And the answer is the inner block, the outer block has 9 and 3. But the inner block has what you might call i inner, which was recalculated to be 5 and j was left. J just got passed in, because there was no inner block j. So that's really j outer. And then over here, we have outer again. And when we go to the outer block and we print it, we see that value 9 was retained. So that should all be in your knowledge base already. From an allocation point of view, i and j are placed on the stack. Inner block. I inner is on the stack. I outer is hidden. Then we have block exit. I inner Is destroyed. And then we return to the outer block and we have again, i9 and j3 which are the outer block variables that are still on the stack.

Explore our Catalog

Join for free and get personalized recommendations, updates and offers.