Typedef - C++ Tutorial For Beginners #24

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

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 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 tutorial introduces C++ beginners to type definitions using the typedef keyword, covering its syntax, usage, and benefits in programming.

Key Takeaways
  1. Learn the basics of C++ programming
  2. Understand the concept of type definitions
  3. Apply the typedef keyword in C++ code
  4. Practice using type definitions in various programming scenarios
💡 The typedef keyword in C++ allows developers to create aliases for existing data types, improving code readability and maintainability.

Related Reads

📰
AI Won’t Replace Localization. But It Will Rebuild It.
AI is transforming the localization industry, but it won't replace human localizers, instead it will rebuild the industry with new tools and workflows
Medium · AI
📰
5 AI tools that generated $50k for creators in 2026 — steal this strategy
Learn how 5 AI tools helped creators generate $50k in 2026 and apply this strategy to your own work
Dev.to · Already Here LLC
📰
How I Slashed My AI API Bill by 95% (And You Can Too)
Learn how to optimize AI API usage and reduce costs by 95% by applying simple yet effective strategies
Dev.to · gentlenode
📰
Why AIF Subscription Documents Are the Hardest PDFs in Finance — And How We Automated Them
Automating AIF subscription documents is challenging due to their complex and variable layouts, but can be achieved with the right approach
Dev.to AI
Up next
McKinsey's Lilli: AI-powered knowledge access that levels the playing field
Box
Watch →