Classes in C++
Today we’re going to be talking all about classes in several spots so finally getting something called object oriented programming which is a very popular way of programming object on the programming is really just a style that you can adults as to how you write your card other languages such as they shop in Java a primarily object oriented languages in fact economy really dry any other type of card I mean you can if you really try but ultimately those languages are meant to be objects on the languages however say first loss is something a little bit different because it doesn’t really enforce a certain style upon you the language say for example doesn’t actually support under John’s programming because in order to have objections programming you need to be able to have concepts such as classes and objects and that’s not something is available in say however it’s about $12 cattle that functionality if you wish to use it and it is almost always a good idea to use that to some extent some of them talking about what classes in this article to put it simply classes it’s just a way to group data and all functionality.
Together for example thinking again and again we might want to have some kind of representation of a playoff so what kind of things do we need to actually represent a class we definitely want some kind of aloof for example the position of the planet you know game wild certain attributes the plan might possess such as the speed at which the planners we might also want to have some kind of 3 D. model that represents the play out on the screen all of this data needs to be stored somewhere we could go ahead and create variables for all this in fact let’s go ahead and take a look at what that would look like let’s say that we want to make a play here now card we might want to have a physician’s office or say X. and Y. and then potentially a spate buses that this vehicle to terror so these are all into just now you can probably start to see that this is getting a little bit Massey and in fact because these names also generic you might want to do something like player acts play why for the X. and Y. quarterly plan maybe play speed and this really does start to get a little bit messy especially if we decide actually we want to play in that game well then suddenly you’re going to have to duplicate this as not doing something like player X. 0 plan acts one instead of this you could of course use an array but the point is still the same it’s just kind of a bunch of.
Variables that on group together just on organized I just sitting out card and it’s just not a good idea another great example of why this is annoying is because if I want to write a function that moves the player or something like that I suddenly need to be specifying all 3 parameters as the insurgents are for example the X. coordinate the Y. coordinate the state all of this just become stars much card and so difficult to maintain and followed that it’s just it’s really messy so what we can do instead of simplify this by using a class we can credit class for the plan you can call it play out which contains all of that Donna that we won all those variables kind of in one time sensitive this let’s create a class called plan. We do so by just using the what class and then giving it a name that has to be unique name because classes are types we basically creating a new variable time and then we open and close caliber acts as if this was a function however notes that we do actually need a semi colon at the end of the closing price inside here we can specify all of those variables that we did the large for example X. and Y. for the position.
And then a favorable as well all right great so there we go we’ve created a brand new class called plaza which is essentially its own type so if we were to start using this player class we can credit is if it was any other variable we write the type so plan and then we give it any name I’m going to call this place and that’s it with credit you variable Coldplay which has the time to plan and of course the definition of that time is over here variables that are made from class types of cold objects and a new object variable is called an instance so what we’ve done here is we’ve instantiated a playoff Jack because we’ve created a new instance of that class time now we want to set those variables we can simply write play adults and then the variable names such as acts and then assigning equal to something like 5 if we try compounds card right now we’re actually going to get an era that tells us that plant cannot access private member declared in class plan this is because of something called visibility 20 for any class you have the option to specify how visible the stuff in that class actually is by default a class makes everything private which means it barely functions inside that class can actually access those variables however we want to be able to access these variables from the main functions of what we actually need to do is come up here and make it public means that we’re allowed to access these variables outside of this class anywhere in a cart really.
we’ll talk a little more about visibility into future article I really want to get into it right now so you can save a combined occurred here than African bosses athlete okay pretty sweet so we’ve achieved a fast all we managed to drastically clean on the card and have all about variables in one place because really this collection of variables represents a plant so we’ve managed to curb it nicely now we’re going to start a surprise that we actually want to plan to do something for example move somewhere around a function which changes the play as X. and Y. variables how can we do that well we could ride it as just a standalone function it’s alright void I’ll call and move and then I would need to take in the plan that I would like to move and I will have to pass by reference because will actually be modifying the player okay and then also take an X. I. and Y. I. which will be the amount that we move the plant by in both. X. and Y. that all I need to do is just say plan to axe plastic walls accented and plant why plus equals Y.
we can also use that speed bearable to we have that by multiplying our amounts by the state all right pretty cool if you want to call that we would just rise murders plateau and then about how much we want to move the blast maybe one minus one and then we got we’ve written a function that can move the plant however we can do a little bit better miss Holly I said the classes can actually contain functionality so what that basically means is that we can move that move function about us into the class and functions inside classes a cold methods so I can literally garbage here into my card and just moved just a little bit off so that it’s inside the plate glass and there we are now obviously we’re inside the black box now so we actually have access to these variables we don’t need to peas a playoff checked in because we’re already inside apply object so if I get rid of this and then all of these the X. and Y. and speed that we’re referring to will be the current objects variables if I come down over here I can change my card now to just say play adult move.
I never thought was simple find a card quite a bit every player object is going to have its own move function and when we call move for that specific player object that is the player object that will again this isn’t really any different to having the main function outside of the play across all it does is it kind of cleans up a card it makes everything look a little bit nicer and that’s a huge bonus when you’re dealing with a lot of card because the mall could you have the more complicated it can get in the hotter it can actually become to maintain that card to having things like this to keep the car cleaner is actually a very welcome thing so that’s it that is essentially the basics of what classes are classes allow us to group variables together into a type and also add functionality to those variables because if you take a look at this card one more time what we’ve really done is we’ve just defined 3 variables the kind of existing one type and also a function which manipulates those variables that’s really all we’ve done of course that function can do literally anything but.
The gist of it is we got Dada and functions to manipulate that data and that’s really all the classes now there are so many uses for classes and will kind of start getting into them in the future this is also you literally just the basics of what a classes there are so many more things we can do with classes which is why the next few articles are going to be taking a look at those things in more detail also I want you to keep in mind that while classes are extremely useful and can keep your card a lot cleaner. They can do anything that you can contour without classes don’t give you any kind of new functionality that you could not have done otherwise anything you can do with classes you can actually do without classes which is why languages such as C. exist on a perfectly usable languages don’t have classes and yet we can still write code classes it is bad to make our lives easier ask program is essentially just syntactic sugar that we can use to organize our cars and make it easier to maintain that’s all they are anyway I hope you guys enjoy this article.