Structs - C++ Tutorial For Beginners #23

NeuralNine · Beginner ·🛠️ AI Tools & Apps ·5y ago

Key Takeaways

This video tutorial covers the basics of structs in C++, including how to define and initialize structs, access their members, and use them to represent data structures. The tutorial also touches on the similarities and differences between structs and classes in C++.

Full Transcript

[Music] what is going on guys welcome back to the c plus plus tutorial series in today's video we're going to talk about struct so let us get right into it all right so as the name already suggests structs are used to represent data structures in c plus the basic idea is that you have a struct and you initialize it by saying struct and then a name i don't know my struct and then you say this is my structure ended up with a semicolon and the idea behind the struct is that you combine multiple things into one structure so for example instead of saying okay i have an integer and a string and a boolean and they somehow relate to each other you say i have a struct and this struct consists of an integer i for example an std string s for example and a boolean b for example this just something here now this structure is one thing it's one data structure and in c plus it is a little bit like a class and an object so for those of you who already know object oriented programming from from another language like python or java for example or c sharp uh structs in c plus plus are almost like classes now there are some differences for example the default um the the the members are public by default instruct it's not like that for a class um and also it's just not considered best practice to use structs for things that you would actually want to use classes and objects for so the the basic um rule of thumb that you want to follow here is if you want to represent a data structure a classic traditional data structure or something like that you use a struct and if you want to actually represent something like an object like a human like a book and so on you go for a class and an object but this is not the topic of today's video so structs and c plus are much more like uh classes whereas in c structs are really just data structures you cannot go ahead and see and say i have a void here which is the method or the function test and this function goes ahead and see out test like that so this is not something that you could do in c at least as far as i know you can point to a function and so on but you cannot just have this function belongs to that struct and so on um so what we can try here is we can just go ahead and say struct my struct i'm not even sure if we need the struct to be honest but i think we need it yes m1 let's call it m1 and then we say m1 dot i equals 20 and m one dot s equals hello and then we have b uh m one dot b equals uh true like that and then we can just go ahead and say std c out m1i std end line and then we copy that and we print the individual values and last but not least we can also go ahead and say m1.test so we call the function from this struct so when we now compile this and run this what do we get here no member named b uh what is the problem here oh it's a boolean obviously this is not java so bool in c plus and then there you go so we have 20 hello one and test one represents true um and now the interesting thing is that those are not this is not the same as saying in i and string s and so on those values belong to this particular structure so this structure m1 consists of an integer a string a boolean and a function and we can create another one we can say struct my struct m2 and we can try to print those values for m2 oh sorry in addition to that i can try to do that and you're going to see that we're going to get uh different values or undefined values in fact because we don't have anything assigned yet um so those are individual structures and we can manipulate them so the values of m1 are the values of m1 and the values of m2 are the values of m2 now the function is the same in this case so if i say m2 test we're going to get the same result here but if the function test for example prints uh information of the struct so if i say okay print i s and b here i'm going to get a different result for each struct now let's go ahead and do something useful let's not do my struct let's do a person so we can say struck person again as i said person is usually something that you would want to model in a class so in object oriented programming but just for an example we can use a struct for this as well uh especially because this is useful if you're doing something in c or even in go and go you also don't have classes you only have structures uh so you can get used to that as well here uh we can create a new human or a new person we can say struct person p1 and this person shall have a name so std string name then we have s not std int h and let's say we have character gender like that and now what we do is we go ahead and say p1 dot name equals max p1.h equals 25 and p1 dot gender equals m for male and we can copy this and have a second person here and this is going to be anna she is 35 and f for female of course we need to change this to two here there you go and now we can also add a function here that just prints the info so we can say something like um void print info and what we do here is we just say stdc out name i mean yes i could use formatting but i'm going to do it like that right now um i hope i don't need something like this here but i think not but we'll find out in a second h h and gender gender let's see if that works we now just need to call this p1 dot print info and then the same thing for p2 so as you can see it works it can uh it can access the individual attributes uh so this works as you can see more like a class and see that's not possible you cannot just do that and see as far as i know maybe i'm wrong but as far as i know that you cannot do that in c in c plus plus that's not a problem and a struct can be treated like a class again it's not it's not best practice uh but one more thing that we can do here is we can look at the size so we can also go ahead and say stdc out size off p1 just to see how much or how many bytes this memory uh this structure allocates in the memory so we can run this here and we're going to see 32 bytes and i think for p2 it's going to be the same obviously i mean hopefully because i think maybe the name i think the name shouldn't make much of a difference here maybe if i add a lot of ace but even then i think that the string has already allocated more than it needs yeah uh but we can see that there is a change if we add uh something else here so let's say i don't know we have a float weight or something even if we don't assign it we still have a float and because of that we have four more bites here so that's it for this video if you enjoyed i hope you learned something if so let me know by hitting the like button leave a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you very much for watching see you next video and bye [Music] you

Original Description

Today we talk about structs in C++. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 💻 Exclusive Content 💻 👥 Patreon: https://www.patreon.com/neuralnine 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video tutorial teaches the basics of structs in C++, including definition, initialization, and member access. It also covers the similarities and differences between structs and classes in C++.

Key Takeaways
  1. Define a struct using the struct keyword
  2. Initialize a struct using the struct name and member variables
  3. Access struct members using the dot operator
  4. Use structs to represent data structures
  5. Define member functions inside a struct
💡 Structs in C++ are similar to classes, but with some key differences, such as default public members and the inability to define member functions inside a struct in C.

Related Reads

📰
7 AI Notion Workflows That Actually Run in 2026 (Honest Comparison, incl. Easlo & Thomas Frank)
Learn 7 effective AI Notion workflows for 2026, including Easlo and Thomas Frank's systems, to boost productivity
Dev.to AI
📰
ChatGPT Plus and Claude Pro reject your card? It is probably the billing country, not the card
Learn why ChatGPT Plus and Claude Pro may reject your card due to billing country issues, not the card itself, and how to resolve it
Dev.to · Tung@fizen
📰
👾 🧚🏼‍♀️Maximizing Fable for Life Admin
Maximize Fable AI for life admin tasks in a limited time frame
Dev.to · Anna Villarreal
📰
Lost in the Cheese Aisle? Here’s How AI Can Identify Any Cheese From a Photo
Learn how AI can identify cheese from a photo, revolutionizing food recognition
Medium · Startup
Up next
how i use a.i. to create viral UGC influencer facebook ads.
Austin Rabin
Watch →