Username:
Password:
C++ Fundamentals Tutorials

Classes / Member Functions

Subtitles of the Movie

Welcome back I told you we would pick this right back up where we left off, we have our C rectangle class to find. We have one of the methods, set sides, define outside the class or right after the class. When we put the set sides name here we had to use C rectangle, colon, colon, as part of the name when we're defining the function. That's so that the compiler understands that we are defining this function right here which is within this C rectangle class that way we get the correct scope and put it all together into C rectangle, two colons, set sides, so we are now actually defining this particular function. Ok, now main down here, we are going to first of all make a new rectangle right there, rect and that is our rectangle and it is one of these, C rectangle, right there. And that's our class, up here, so you can see that we have created a new class, I'm sorry, we created in addition to a new class a new type where we could have said int and something, a, b, c or you know some other name down here. We can now say C rectangle and use that just like any other base type that is part of the language. So here we have C rectangle, rect, that creates a new rectangle and so this thing has now taken up memory, its taken up enough memory to contain H and W and set sides, this is just going to be a, a pointer to this function which is defined over here and then just this function, this little return and multiplier, multiplication will be inside the class as well. So here we have our new rectangle and now we are going to use one of its methods, and we use just like we would in a structure, we use the variable rect, or the object with a dot nomenclture right there and then there's the method, set sides and we're just gonna hard code some values in. The deck I want is 12 feet by 24 feet so I use 12 and 24 are our two sides for our rectangle and that sets the 12 and 24 into H and W of rect. So we couldn't get to H and W directly because their private but we can get to them byusing this method called set sides and method will only take in two integers andits very careful about what it does, this way we can get to these data values inside this rectangle of the real object here. But we have to go through this method, this is modular programming, this is what's different about modular programming then procedural where we would just say rectangle dot H is this and W is that and so on. We don't have the kind of control and protection that we have that we pick up here in object orientated programming. So this is what it looks like, there we call this method set sides and notice when I hover over it, it shows me the interface for set sides that it takes in two ints and if I had more then one way of coming in maybe I can take in doubles for being able to have floating points values there would be a small list here of all the different possibilities of what I could put in there. Now let's get something back out of the rect object and here we have C out, that's our console output and that's part of our output stream, its standard C out and you can see the definitions pop up there. Its in the standard namespace that's the left most and then there's two colons and then the class is the O stream, std and then the C out is the actual member that we are using in there. So we have our C out, this is the send to, we're sending this data out, the C out, you know giving C out. So here's our text, real simple area and then we'll call another method, the rectangle dot area method and that's this method up here which is our little inline and the information will be calculated in height times width which are already here in our private integers and then that number will be converted by C out, because C out understands what to do with an integer, how to display it properly and then we'll stick a new line at the tail end of it, just to get out, get down on a new line. And then we'll get out of our program and return 0, so very simple, but all of the things that we've already talked about in classes are there and this little classes 023 CCP will be available to you as part of the video series. It'll be on your disk or available on the site so you can grab this if you don't want to type it all in. I would recommend you type it in because a few typos here and there are a good learning experience but it'll be available to you. So now let's run this and see what it looks like. I'll do a control F5 and there we go, area is 288 and since I did control F5 without debug I have to press any key to continue and we saw that back in chapter and that is our first class and declaring class, declaring a method that is within a class and then defining it and then actually doing something with our new class, creating it and then using it and then when we hit the return garbage collection will be done and this rectangle will be cleaned up. OK so that's the end of our first class, good job, we'll move on to the next video.

Tutorial Information

Course: C++ Fundamentals
Author: Tim Heagarty
SKU: 33797
ISBN: 1-934743-09-7
Release Date: 2007-09-14
Duration: 4 hrs / 55 lessons
Captions: For Online University members only
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 81,350 Video Tutorials (20,800 free)
  • Video Available as Flash or QuickTime
  • Over 782 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available