C++ Programming Tutorial 83 - Creating a Struct

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on everybody this video is where we start getting hands-on with object-oriented programming we're going to create our own struct in this video now when it comes to file structure I've been kind of tinkering with the files so maybe I lost track of where you guys are but basically I'm starting from scratch in this oo P folder and then I have this main CPP and this is all we have so you should be able to get to this point pretty quickly and then we can work together on building some complex applications now let's get into creating strux but first you need to check out our sponsor Embarcadero rad studio rad studio is the IDE of choice for C++ development quickly build native mobile and desktop applications from a single C++ code base and deploy to Windows Mac iOS and Android with rad studio user interface design has been made easy with hundreds of pre-built components for cross-platform development you can easily integrate with popular source control management systems databases api's and you can make your life easier with numerous third-party extensions let rad studio do the heavy lifting when it comes to C++ development give it a go with a free trial by following the link in the description creating a struct is pretty easy and to start with it's easiest if you just do it all in one file so all our code is going to go in this main dot cpp and then later on you can branch this out into multiple files if that helps for organization structures but for simple applications there's no need and making it extra complicated so we're just going to do it right here to create a struct all you do is say struct and you give it a name now by convention I use a capital letter here all my custom types I start with a capital letter now we can put our data members inside of this struct so what does each user need to have so for example we can create a string variable called first name maybe another one last name and then let's say we have another one status and when you're done with the struct I always forget to do this every single time you have to put a semicolon to end this struct at least this time I remember before compiling it so I'm making progress now this is defining the structure it's like defining the columns of a table and each user is going to give a value for first name last name and status we don't assign the values here inside of the struct although it is possible to give to fall values in general we will want to do this on a case-by-case basis and to do that we need to create an instance of this struct aka an object like so we say user and then we give it a name in this case we use the same word but this one has a lowercase or if that's confusing to you you can use something else such as me that's what I'll go with in this video and then all you have to do is say me dot first-name and you can assign a value and then just do that for all of the data members and there you go that is how you create a struct and then instantiate the struct and assign values to the data members we can access these values the same way we did here for example you can say me dot first-name and output that here so now let's compile and we didn't get any error so we're doing pretty good and then we output this let's see what we get we get first name is Caleb now this first name here can be accessed because it's access modifier is public by default things inside of structs are public we can change this by defining something as private like so we just put the private keyword and then generally will indent everything after the private keyword now when we compile we're getting an issue that's because we're trying to set the value of status but we can't because it's private when something is labeled as private we're not able to access it from the object so if it's private and we can't access it from the object when would we ever use it well oftentimes we'll have methods inside of here that we'll need to access this private data it's basically a way for us to store information without exposing it to the public world through public access through an object and that'll make a little bit more sense once we start talking about objects and methods for now structs are usually just to contain data so that means variables of a particular type they're usually not for methods often times people will switch from a struct to a class once they start wanting to use methods but just for giggles let's put a method in here just to get a feel for how that works now do we want this to be after the status or do we want it to be up here well I actually want it to be up here because we don't want it to be private so you can define it here or you can defy after the variables that's totally up to you all you have to do is say what the return type is the name of the function we'll just call it get status if I can type and then all we're going to do is say return status and here you can see the use of private variables we can access that private variable here and that's totally fine because we are still within the struct definition so status is accessible here inside of these curly braces but it's not accessible down here outside of those curly braces this is not going to work so we're just going to get rid of that line now to use this method all you have to do is on a particular object just put a dot and then put the function name so get status for example instead of saying me first name we could say me dot get status now this ain't gonna do much because status doesn't currently have a value so when we run this you can see it just has a blink and we should probably change first name to status now it just says status and it's blank you can assign a default value to this up here for example we could say the default is gold now when we compile and run we should get gold looks like we got a little error because the in line assignment is actually reserved for C++ 11 so what we want to do is just say standard equals C++ 11 and there we go now I can run and well off status is gold so that is your introduction to creating strux we just covered the bare basic minimum once we get into classes we're going to get in a little bit more detail some of the different things you can do but for now you understand data members and you understand methods and you have a little bit of a feel for access modifier specifically the difference between public things and private things so thank you guys for watching be sure to check out the next video because we're going to start talking about classes and objects structs are similar to classes and that they will also produce objects so there's not a huge difference it should be a pretty easy learning curve so go check that out and have some fun I'll see you guys in the next video please be sure to subscribe [Music]
Info
Channel: Caleb Curry
Views: 40,028
Rating: undefined out of 5
Keywords: c++, programming, tutorial, creating, struct, creating a struct, code, coding, caleb, curry, calebthevideomaker2, intermediate, beginner, c++ structures, c++ tutorial, c++ tutorial for beginners
Id: E_aVsBLlvpQ
Channel Id: undefined
Length: 6min 48sec (408 seconds)
Published: Thu Mar 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.