Introduction To Classes

5 minutes
Share the link to this page
Copied
  Completed
A class in C++ is a user-defined type or data structure declared with keyword class that has data and functions (also called methods) as its members whose access is governed by the three access specifiers private, protected or public (by default access to members of a class is private).

Transcript

Hello, and welcome to the 46th tutorial in the c++ programming series. And in this part of me looking at classes, just a quick introduction to classes, classes are the reason c++ is a new language or object oriented programming, basically use objects and have this view we'll see throughout the course of the next few tutorials, what objects are, it is also the meat of this language that makes it so powerful on top in many of the popular languages after that, you'll come across us object oriented programming. So have you learned in Java and c++ or whatever, this will serve as a good basis for learning other languages. Classes are an extension of data structure, which we looked at earlier in this series using structs. Classes allow variable and methods to be contained. Then you create objects aka variables of the classes, which have their own unique data.

So let's just open up that project and create a simple class. Emily's gonna create an object of that class and show you how it has its own data. So we'll just create a class in the keyword class, then the name of our class. So we'll put vehicle, spell vehicle, right first and curly braces with a semicolon or dm. And then you can put the scope of your methods and variables, sorry about that notification going off left, right and center. So I'm going to public This basically means the methods and variables that are public or accessible not only within the class, but outside the class as well as if you have an object instantiated.

You can access it via that object, aka the variable. Now here we just put what we want to be public some hint, speed, will cover the scope of variables a method in the next part of this series, so don't worry about that too much of age. Void print function to actually implement the function, which there's two ways to do it, actually, you can do void the class name, and colon, colon and print function. And then you do STD c out or whatever you want your function to do. This is going to be a simple form as a print ID. Another way to implement this is to I'm just going to quickly deleted is to actually just do it in line.

So if you get rid of the semicolon, and you just put curly braces there, you can be on the same line on a separate line and then you can do your code. I preferably Try not to do that maybe if you have like a really small function, which in this instance, it is May, but generally, I just implemented out so that I can create an object of the class is very simple. You can think of it as like string string is an object is a class you hash include the string file, and then you create objects. Steady strings on my integer which aren't. So what we're going to do is use the class name, which is the vehicle, then our variable name unquote BMW, Toyota. And then we can do BMW dot speed equal 900. semicolon.

And then what we can do is tell you to dot speed equal to 89. We can do STD C, up to speed STD in line. Now if we just open up the console, oneness, any printer 900 we will change him to be here to Toyota. And it prints out a ton on the pallet on values. You can turn this on later, which is great obviously if you're if you have a game, there are several columns racing against the records. They're going to be at different speeds most likely Gonna be at different positions, they're going to probably have different damage amounts if you have that for a property if you have those sort of properties within your game.

And we've seen this in the extendable to application then other games. I'm going to show you the print function. We run this, we get the VM, the P print function that is it for introduction to classes are immensely powerful. If you're going to do any sort of serious programming in general, if you're going to have a programming career, you'll be coming across object oriented programming a lot. So go over this. I'll say a few more time and when you feel comfortable, check out our next part of this tutorial, which will cover class access.

Basically, probably what have you if you have any questions, feel free to message us or support us on our system, call it UK email will be in the description. You can comment on this video or historically messages via YouTube, or the required link for source code will also be in the description. And as usual, thanks for watching and I hope you have a nice day.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.