Classes / Class Types
Subtitles of the Movie
Ok we are talking about classes and now we are going to talk about members in a class, these are the variables, these are the data that live inside an object. Just like we used to have variables and data that lived inside our C programs now they are within the various classes, it just kind of keeps them separate from one another and it makes it easier for us to think about and it makes it easier for the compiler to understand what we are trying to do and when and where and how to maintain some separation of these items. The members of a class have what's called an access specifier, that's a new item and you can see that there underneath the class name, this is what a definition looks like, here we are going to make a new class, this is the name of the class, kind of like the tag to a structure that we used to have our open brace and this is a new thing that we haven't seen before. This is an access specifier and this is the first one, it has a colon at the end of it, not a semi colon, but a colon and then this will be the first member that is corresponding to that access specifier. Then we can have another access specifier, another colon and then more members as we go down through here. This is our class definition. If we are going to instantiate or create any of these classes and create an object it'll be right here and then of course this creates a new type , just like an integer, character float something like that. So we will be able to make new objects later on and you'll see of course how we do that but we can have the object immediately created right here or have a new one. This actually takes up memory, this of course does not, this is just the definition of what a class looks like should we ever actually make one, down here. So classes can have data or variables called members, members can be private, public or protected and that's the access specifier, you'll see the word private or public or protected in that access specifier with a colon after it. Now the members that are there, they can be any type, they can even be other classes, they can be integers, floats, characters, all of those good things but we have all kinds of opportunity to create new types of variables, that's really what object, one of the great things about object orientated programming is we can create these new types and when we create them we can create the rules that say how the types can work. What you can put into them, what you can take out of them, how you can convert them and do casts, you know type casts from one thing to another, remember how we used to cast an integer into a character and you could possibly lose some data because an integer was normally twice the size of a character. Well when we create these classes we are creating new types and we can specify how all of that works and you're going to see a lot of that as we go through our fundamentals. Not all of it but I guarantee you that I'll introduce you to some of the other type creation things that we can do and that's what this is right here, when we make a new class we're making a new type. Now here let's make a C rectangle this is a new class definition and its called C rectangle, that's kind of a convention in the OOP style, it used to have a capital C in front of that name so that you see that this is in fact a class definition, now down here this will be the real object that takes up memory, a rectangle, but here this is what the class definition, the class name is up here. Now only C rectangles, others like rect here can see the X and Y, so when rect is created it knows that it has an X and Y but nobody else is allowed to know that because when you start a class declaration like this the first default set of access specifiers is private. So int X and Y here are private, nobody can get to them, we have to specify that we want some things public and when we say public and a colon, now this function set values which takes in two integers is part of the C rectangle, it is a function within the C rectangle class, and its really called a method, this is a definition of a method, we haven't we haven't, declaration I should say of a method. We will define the method in a little bit and actually say what you know what a set values do for us but right here we are declaring set values as part of C rectangle. And here we make an integer called area, are will return an integer if I could read my own code and it takes nothing in so there's a void parameter to area. Area is another method which can be used to return information about a rectangle and as you can probably read ahead we will teach area how to calculate the area that this rectangle covers. Now again anybody that can get to rect can see this set values and area method, they can use it out of this rectangle, and rect is an instantiation of a class called C rectangle. See its not too difficult huh? Well let's keep right on going and look at some more of this and our C Plus Plus Fundamentals video series.
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 