Classes and Objects
A free video tutorial from Serge Lansiquot
AAA Game Developer, Indie Game Developer, Programming Tutor
4.3 instructor rating • 2 courses • 2,376 students
Learn more from the full courseLearn C++ Programming By Making Games Volume 2
In depth and practical look at Object Oriented Programming
28:09:26 of on-demand video • Updated December 2019
- Students will learn good OOP fundamentals
- Students will learn some fundamentals of 2D computer graphics using SDL
- Students will have created a shell app that will contain 4 games
- Students will create a game like Tetris
- Students will create a game like Break-out!
- Students will create a game like Asteroids
- Students will create a game like Pac-man
- Students will learn some vector math in 2D
English [Auto] You'll remember that strikes in EMS were ways of creating our own data types. Well classes are another way to do this. Classes are the fundamental building blocks of PE. We use classes to encapsulate data and functions that modify that data in order to create our own data types. So let's see an example of it and its syntax. So let's say we had the class call 2D and we can have functions on the class that would say sets certain things. In this case point two D's of course is just x and y coordinate. Don't worry about the what's actual the actual content of this yet but we'll focus on the structure of it in the second okay. Something like this. So again don't worry about the details of the class itself. Let's just focus on the structure. So we have the data members M X and Y which are of type int. We also have function members that will operate on the data in set and display. So this is this whole thing is the declaration of the class. Basically what the class has and can do. This doesn't actually implement anything in the class yet. So let's look at that next. Uh uh let's implement the set method first. So uh. So SATs and again I'll explain what I'm doing. Second so I should say an X equals D X Y equals Y symbol. The display something like that. So again don't worry about too much about the implementation details of the class. Let's just focus on the structure. So it almost looks like a normal function definition but it has the name of the class with the two colons in front of it right here. So point 2 D colon colon set in point two D colon colon display. The two colons are an operator called D scope resolution operator. We use the scope resolution operator to differentiate between say a local function call display that may that we may have and point to D member function definition call display. Remember you can't have the same identifier defined twice in a C++ program. So this operator helps us differentiate between something else to note is how we use the data members in our member functions. Note In set here we can use our data members and modify them using the parameters passed into our function and in display we're not modifying them. We're simply accessing them in order to print them out to the console. So let's actually use this class and run it. So again I'll show you I'll explain exactly what all this is in the second sets. Anything done display and do it different one the two it's it's play this one as well. It's Bill s run OK so you'll see what we did was we have our point object and we'll set it to some value and then we'll display them out so we do that with two different points. So you have two different outputs right here. OK so first you'll see a new variable of type point to d Call point. This is an object of our class how you should think of it. You should think of this is the class is the blueprint of the type. What it has or what it can do and the object is the instantiation of the class. A single instance of the of that class. So point in this case each instance will have its own version of set display M X and Y meaning if we had a point two D point two point two would have its own version of the members. To call a member function or access data of our point class we use the dot operator right here in this case we use the dot operator to call both set and display on point that will call point set method to modify points. This point specifically its members M X and Y and display will actually print them out. These only affect this point object. No other object of that class so we also have these public and private things up here. Right. So what does that mean. So public and private. Define the outside accessibility of our classes members. So in this case our main function is accessing both member functions. OK any public members can be accessed by functions or other classes like in the case of our main function accessing set and display but any private members can only be accessed by our class. So we couldn't have say something like point dot in X equals 5 like that. Okay so if we build that we'll actually complain saying M X is a private member of point 2 D. Okay so that's not allowed. So anything that's private can't be access outside of our class so you might be wondering how we got away with that in the set function using M X and Y. Why didn't the compiler complain about that. Well that's a do again with the scope resolution operator point two D set here literally means that it's a part of point two D class so it's a part of the definition of the class so we can access those data members so don't worry too much about it or don't worry too much if you didn't fully understand all this quite yet. We'll be doing many examples of classes in this course.