Classes / Data Members
Subtitles of the Movie
Hi there welcome back to our video series on C Plus Plus Fundamentals. We are going to take a look at a actual little program that's going to do something very simple but a lot of the pieces that we've already talked about are in there and we are specifically wanting to talk about the members that are inside of a class but I want to show that in an actual little program that's going to do something. So we're over here in our visual C Plus Plus 2005 Express Edition which we got as a nice freebie Microsoft and got it all installed and updated and you did that way back in chapter one back at the beginning of our video series. So let's just go line by line, down through our program and take a look at it. The first line is something that Microsoft likes, that's our standard AFX dot H and that'll need to be there if you don't have standard AFX dot H as one of your early lines the compiler will be glad to complain about it and will prompt you, did you forget to put this line in, so just put it in there. Then we include our IO because we are going to be writing to the screen in a little bit and that's there and then we're going to set up to use a STD standard library namespace, we'll talk about namespaces later on in the video series but basically this says that we want to gather a group of functions and methods that are out there and different kind of definitions and use them without having to call up STD colon, colon all the time. So it says that we are using the standard namespace. Now here we are going to make a rectangle and I use the class name C rectangle and you can see that right there with the capital C in front of it and that just says that's the name of my class, that doesn't create anything, remember this is just a declaration, we're not actually creating the class yet and you know by instantiating the class is when we are actually create one. So here we just want to say what the class actual is and what's in it. We are going to have two integers which are private and that's H and W. That's going to be height and width of our rectangle and I am thinking about building a deck on my house so that's why I've put this little thing together just to do a quick area and know how many square feet we are talking about. Here we have a public access specifier and that says that these two items, anything after the word public colon will be available to anybody that wants to use them in the rectangle, the H and W are only available to the C rectangle class, other members. So H and W could only be used by set sides or area to be able to do any work with them or touch them at all. Set sides, this is a declaration of the little function called set sides, the method, cause it's a method that we can use with this class and it'll take in two integers at the end, that's the interface to set sides. It'll have to take two integers or the compiler will complain about it. Here we have an int area, this is another definition of a function and here is our actual definition of the function right there, that's the code that makes it up, because there's the braces that we need to contain the contents of the function itself. This is called an inline function or method, an inline function. The rule of thumb for an inline is it should fit on one line and be a very simple return, like that. This can save you some function called overhead by putting this in here because it will be included right in the class as an inline function definition. So that's our little area function and it will return an integer, we see the int there, notice its set sides as a void return, it doesn't give anything back, area will return an int and what it will return is the H times the W, height times the width and that's the same H and W that we're defined up here. Alright, straightforward. Now we are going to come down here and we're going to actually define the set sides method and that method which is referred to up here as part of the C rectangle class now has to, it was declared up here, now it has to be defined, down here. We have set sides, it'll take in integers A and B, this looks pretty normal to C programming right? We have our open brace and then we're going to say that H is equal to A, this incoming integer and W is equal to B, this incoming integer, so these H and W's are two private H and W's up here, their part of C rectangle and we can see them because we also are part of C rectangle, set sides is a method within C rectangle. Alright? And here's the closing brace on the C rectangle, now that's enough for right now, I'm going to start another video so that we don't make these two, long and boring and then we'll talk about main and how we actually use this class ok? So just skip over to that one and I'll meet you right back here in a second.
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
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 