Typedef - C++ Tutorial For Beginners #24
Key Takeaways
This video tutorial covers type definitions in C++ for beginners, providing an introduction to the typedef keyword and its usage in C++ programming.
Full Transcript
[Music] what is going on guys welcome back to c plus plus tutorial series in today's video we're going to talk about type definitions so let's get right into it so type safety in c plus is actually quite simple thing it's not too complicated the basic idea is you use the typedef keyword so you say type def and then you specify a data type that already exists so for example something like unsigned integer unsight int and now you want to use a different name for that data type you want to create a new data type but not you know not a structure not a class nothing too complex just another name for example you could have something like h h is a type you now want to have a separate type for h uh you don't want to have just integer and then assign an h to it you want to have h if it is an h and the the basic convention that you use here is always underline t now that's just a convention you don't have to do it in order to get it working you can also say my age type and that's also a type um as you can see it's highlighted like that this means that it is type in visual studio code and now instead of saying unsigned int value equals 10 or something or h equals 10 we can just go ahead and say my h type h equals 10 or something like that or let's do 20. now this is now an unsigned integer but it's of the type my h type uh and this is important for type safety it's going to be in signatures if you have a function that accepts a my h type you can still pass an integer but it's going to tell you hey this is actually my h-type so that you know what you're passing so it's a lot it has a lot to do with readability if you have um types for age types for size for example one side one one uh type that we already have and c plus plus is size t this is actually a type definition as you can see it's just an unsigned integer as well i think um so you could also just use unsigned int but it's a type that you defined for size t and this is the type used for for defining the sizes of arrays for example but i can show you that this is actually a number so we can print it like that uh we have a problem unused oh i'm not printing the h i'm printing the type now it works as you can see we've got 20 as a result here um and i can also do calculations with it because it's an unsigned integer i can go ahead and add 60 to it and it works the same way as with an integer now as i said this is not uh how you would do it you would not just go ahead and tell uh and name it my h-type you would do something like h on the line t and the reason for that is just because it's a convention because you know okay this is a custom type that i defined it's not a class it's not a structure it's just a type definition and you can see actually the visual studio code recognizes uh recognizes that convention because even if you write some nonsense here and then you follow it up with a underlined t it's going to highlight it as a type so if i do below t it's going to be marked as a type it's going to be highlighted if i just do something like that it's not noticed as a type but if i do something like that on the line t we get the syntax highlighting of a type at least for a second uh of course this does not work you don't have a new type just because you do underline t but that's the convention and visual studio code assumes that this is automatically a type so this is how you define a basic type in c bus now let's look at a concrete example here you want to have a data type called byte so buy it like that and it shall only allocate one byte of memory and we want to do calculations with it now of course we already have data types that only allocate one byte or eight bits but uh those are characters and boolean and uh we also have another one but essentially those are not what we're looking for because we don't want to deal with bytes in terms of calculations and call them characters because you want to know okay is this now a character shall i treat it like a character or is this a byte and do i want to do calculations with it for example uh and for this we can say for example type def unsigned character byte and now we have a byte uh type definition here and i can go ahead and say byte b equals 70 for example and i can go ahead and say c out b hand line like that and the problem that you get here is that it's treated like a character because it is a character um and you can counteract it by just saying unsigned and even if that's maybe not the most clean solution maybe not the best solution it works and you can use it like that so you can say byte b and it's still better to know that you have to do this because if you just call it character b equals 70 we don't know okay should i do this unsigned here or should i actually treat it like a character so using byte as a type is helpful here helpful here just because of the name byte because the programmer knows okay this is actually a byte and i probably want to treat it as a number here um now the interesting thing is that if i have a second byte here let's say i have b2 equals 20 and now i don't use this unsigned here but i just say b plus b2 i will get a numerical result because i can calculate the sum of those and then it's treated as a number whereas if i have this directly stored into a third byte so let's say byte b3 equals b plus b2 and then i go ahead and print b3 oh not b2 sorry b three we're going to get a character again because it sees this thing as a character it takes uh it takes up eight bits therefore printed as a character now we can see that if we go ahead and say c out size off be any b actually b3 for example we're going to see that we only allocate one byte or actually one character unit obviously since this is by definition a character um but we also have a different uh way to do this for example we can use another type definition here we can say something like typedef int or actually let me look what it's called u int u and eight on the line t eight means eight bit so one byte and i can call this my byte here um i'm not sure if that even works because this in and of itself is a type definition as you can see this in and off itself is an unsigned character so you know i could do that but it's in fact the same thing that we did up here this is one way to do it and if you want a fancy way to actually have a type for a bit set for an eight bit bit set uh you could go ahead and say type def uh and for this i think we need to include a bunch of libraries here we need to include and this is this is just fancy you don't need to do that uh this is just to show you that it works um we have bits and std c plus plus h and then include std and h like that um and then we say std bit set so we can define a bit set of size eight and this is the byte type for example and now i can go ahead and just say uh byte type uh mb for my byte equals 100 and we can then go ahead and say c out mb and line like that and if it compiles we get this in a binary representation so if you actually want to use this as a real byte represent it as uh as a binary number we can do it like that uh we can also try to see what happens i don't think that it's going to make any difference here maybe it's going to crash everything if i do unsigned but yeah it's not going to work but that's how you can also define a bit set if you want as an extra type byte type or you could say one byte bit set but i think in fact that this is not even true because if i do size off and b we're probably going to see that it allocates eight bytes in fact so maybe this is not even a candidate for that uh yeah no it at least allocates four bytes so uh this is not what we're looking for character is probably the best solution here so if you want to define your own byte data type you're going to use an unsigned character and if you want to print it as a number you're going to have to deal with unsigned as a function so to say so that's it for this video hope you enjoyed it i hope you learned something if so let me know by hitting the like button and leaving 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 type definitions 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
Related Reads
📰
📰
📰
📰
AI Won’t Replace Localization. But It Will Rebuild It.
Medium · AI
5 AI tools that generated $50k for creators in 2026 — steal this strategy
Dev.to · Already Here LLC
How I Slashed My AI API Bill by 95% (And You Can Too)
Dev.to · gentlenode
Why AIF Subscription Documents Are the Hardest PDFs in Finance — And How We Automated Them
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI