C++ Programming Tutorials - 16 - C++ Structures - Eric Liang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys my name is Eric and in today's tutorial I'm going to be teaching you how you could group related pieces of information in something called a C++ structure now in the past we've always been learning how to use things such as loops arrays and just simple data types to store simple pieces of information now what if we wanted to create a more complicated program that stores multiple pieces of information not only that those pieces of information are related to one another for instance let's say I wanted to create a program that deals with books and we know that books have multiple pieces of information such as string name of the book string author and let's keep it simple so and end it off here so integer publication year now we could declare these types of variables multiple times for each new book that we make however it gets complicated over time because as our program grows bigger and bigger we need some way to cleanly manage and organize all these pieces of information without getting confused now one of the way to do this is by using something called a C++ structure now some of you might be thinking but wait can't we use an array to store these pieces of information well that's a good guess but no the reason has to do with the fact that arrays can only store multiple pieces of information as long as they are the same data type so in this case we want to store strings and an integer and obviously again arrays one data type so it will not work for this case so the thing that will help solve our problem is by using the C++ structure to create a structure first off go outside go outside of the main function and above it but below all your libraries and namespaces type in the word struct followed by the name of the structure so in this case because we're dealing with information of a book that general name we would want to give it it's called a book followed by opening and closing curly brackets and end it off with a semicolon now inside of the curly brackets this is where you would type in your data members or variables so in this case we will put all three of these variables inside of our structure and this basically defines what a book is so let's say we wanted to create a book to store like let's say just the information of a book so let's call book book a kind of like a variable so you would give it the type so in this case it would be the the name of the structure called book followed by the name that will represent like kind of like your variable name now before I go on there are two ways to initialize a structures data members or values for each of the variables inside of a structure so this is the first way so first way to initialize a structure so book book a equals opening and closing curly brackets semicolon and inside the curly brackets you would type in order the information that corresponds to each of the data members inside the structure so the first piece of information is a name and that is the string so quote let's say Harry Potter and the Sorcerer's Stone and then a comma next line it would also be a string which corresponds to the author so that's JK Rowling another comma and then finally the publication year so the sorcerer stones publication year is 1997 according to the Internet okay so that's how you initialize it by using the first method now the second method to initialize a structure is like this so like the first one we type in the structure name and that is a book and then next we give it the name so book let's call it B this time so instead of using equals and in opening closing curly brackets with a semicolon this time you end it off with a semicolon and then on the next line you would type in book B just like you would do with your variables however except this time in order to access the data members or also known as variables inside of the structure you would have to use something called the member access operator and what the member access operator is is basically a period and it allows you to access the data members inside the structure as you can see that's where the name came from member access operator so in this case let's access the name and then to give them you do equals and then because name is a string we do quotes and then Harry Potter and chambers secrets I think that's the name let's see and the Chamber of Secrets my bad it's been too long maybe let's the end the chamber of secrets and then semicolon and then if you want to initialize the author and the publication year you would do the same thing by using the member access operator so book B dot author equals JK rowling semicolon and then book B dot publication year equals and then this time because it's an integer you don't need the koch's and the publication year for Chamber of Secrets is supposedly 1998 8 yep semicolon now if you want to see out the information from book a let's put it down here so see out you would use the member access operator again except this time is just for referencing the stored value inside of it so let's say title and then book a dot name and then n line just to make it look nice and then we will do this on our third just to make it look nice book a dot author and then finally C out publication year ok dot publication year and why ok and if we were to compile this program and run it we should see all three pieces of information printed out and that's what it did as you can see it shells title which is right here and then followed by the title stored in book a which is Harry Potter and the Sorcerer's Stone followed by the author the publication year and then the end of the program ok so let's do the same thing again for the second book look B so let's see out the title book B dot name and line C out of their book B dot author and line and then C out publication year book P dot publication year and line and if we were to rerun this again this time we would see the information from both books because we did see a for both of them so as you can see right here on the first three lines it shows the information for book a and in the last three lines we see the information for books B so as you can see Harry Potter and the Sorcerer's stones JK Rowling 1997 and then Harry Potter and the Chamber of Secrets JK Rowling 1998 okay so let's say let's pretend this is a this is a small piece of a gigantic program that manages books in the library okay so let's say book B went missing so there's no more Chamber of Secrets so we had to replace it with a different Harry Potter book and let's say it's the Prisoner of Azkaban so to update the information we would simply do book B dot name equals and then because we're doing this after book B dot name has run already this will overwrite the stored value of Harry Potter and the Chamber of Secrets with our new value and that is going to be Harry Potter and the Prisoner of Azkaban so Harry Potter and the Prisoner of Azkaban I think that's how you spell it and then semicolon and then book B dot author well it's the same thing except the publication year so book B dot publication year is let's see was it 1999 yep 1999 semicolon so if we were to see out the title for our book B dot name it should show the new value and the publication year just to show you that these value has changed so let's compile and run this program okay so as you can see first three lines remain the same because we did not touch it second three lines remain the same because we did not touch that now the last two lines which shows the two pieces of information that we updated for book B is different because as you can see we ran we reinitialize the values name and publication year for book B by overriding it with these two pieces of information which is Harry Potter and the Prisoner of Azkaban and the publication year of 1999 and this proves that all of this works so in conclusion sequence of structures are great when you want to organize different data types together in one location without having it all over the place and not only that you could use either method to initialize the structure it's up to you me personally I like the second method to initialize the structure and that is all for today's tutorial thank you for watching and please like this video a few films helpful and if you have any questions or concerns please leave it in the comments below and I'll try to answer your question to the best of my ability thank you for watching and I'll see you next time bye bye [Music] [Music] [Music]
Info
Channel: Eric Liang
Views: 775
Rating: 5 out of 5
Keywords: CPP Programming, how to code, how to code in c++, c++, how to program in c++, c++ programming tutorial, C++ Programming, C++ Structures, data structures, C++ Data Structures, c++ tutorial for beginners, how to learn c++, learning c++, c++ tutorial, c++ beginner tutorial, visual studio, how to program with c++, c++ for beginners, c++ programming, c++ struct, c++ structs
Id: K2JdReEG2xg
Channel Id: undefined
Length: 10min 26sec (626 seconds)
Published: Wed Jul 19 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.