Classes / Nested Types
Subtitles of the Movie
Hello again back to C Plus Plus Fundamentals, we want to talk for just a minute about something called nested classes since this is C Plus Plus Fundamentals, I'm not going to take you deep into the details of this because it can get a little bit hairy but, let's just try to understand. I want to help you understand that there can be classes nested within other classes, they can be defined and declared down inside another class, where it makes sense. Their also known as member classes because these nested classes, or these sub classes are defined in the same places that you would find the other data members of a class. Let's take a look at an example for just a minute. We'll get this out of the way that's just a some things from Microsoft our AFX, so we'll get rid of those declarations, here we have this class surround and you see that the compiler helps us out by reminding us the name of the name that we can already see. So there we have class surround and here's our public area, these are going to be the public definitions, declarations of this class so that everyone can see them, and now we are going to create another class inside here called first within. It will have its own private de-variable, integer, that if we hover over that we see it's an int and it is within first within which is within, surround. So we surround double colons, first within, double colons D variable but it is private to first within. Now here is the constructor for first within and we are going to see constructors in more detail in a couple of chapters but basically this is the function that is always run first when we create a new one of these classes. So this will construct any local variables and things like that we need and the way we know it's a constructor is it has the same name as the class. So we have class first within and then we have a function being declared in here called first within. Same name means that it's a constructor for the class. Then we have another little function here called var just to kind of stick something in here, it's a constant function and it will return this D variable that's up here in the private area. So real simple, but, so this is a public class defined as a member to this surrounding class out here. Now here this is the private area for this class called surround. Alright we had the public area here, now we've gone private with an access specifier to show that we are changing how we get the things. Now down here we have another member class called second within, this class will have its own private D variable which has nothing to do with the one that's up here. Different classes, they'll have different names. Here you can see surround, second within D variable verses surround first within D variable, different places in memory. And here's our public section for the second within class and it has a second within constructor function as well defined or declared here but not yet defined. And here's another var function and its another constant function and it'll also return this other D variable that is right here. This is the one coming back. But having these classes within each other we can kind of sub divide and compartmentalize the program so that in our case a first within fully makes sense in the context of being inside the class surround. A second within, only makes sense being inside the class surround but we don't want anyone else to be able to work with it so it is private and that changes the access between these two members. Public members we can get to from the outside world, but no one will be able to use this second within class unless they are within the class surround. So people from the outside world, other functions and methods and that can't even see this. They can't deal with it anymore then they can deal with anything else that is under the private access specifier. But things within the surrounding class like first within in can work with second within for the public sections. Again, second within gets to decide who can see things outside of it. So this D variable being declared private within second within says that only second within can work with it and things that are defined within the second within function. That's not truly confusing, all we're trying to say is that second within and first within are separate from one another and second within gets to say who can see its inner variable. And in this case since this is set private only anything within second within can work with it, first within cannot touch this variable, it has to rely on another method to deal with it. A public method like this var function down here. Alright, hopefully you are confused to a higher level at this point, but the main point, the fundamental that I want you to understand when your reading other code and other tutorials and things like that is that you can have classes defined within other classes if that helps you subdivide your program and make it logically make more sense to you. Alright, hopefully we have done that for you. Onward to our next chapter.
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
United States 