C++ Crash Course For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone my name is saudina and i make i.t and programming related videos on code beauty channel and today we will go together through c plus crash course but first i want to thank brad for giving me this opportunity and for giving me his platform to share the knowledge and i would especially like to reach maybe some more girls and women and encourage them to enter this industry but all the male viewers are welcomed as well so thank you very much brad uh some of you who clicked on this video are already familiar with c plus plus to some extent some of you are not at all which is fine because i'm going to introduce you to c plus plus c plus plus was created back in 1980s and it was created with two other programming languages in mind one of those two was simula and at that time similar was a language that was very understandable by humans but at the same time it was very slow so they needed to combine it with another programming language a fast one and at that time the best choice for that was c programming language so they took the best from both worlds and that's how c plus was born now with that being said c plus plus is general purpose language that means that it is not specialized for only one domain but it can be used to solve a wide variety of problems what are some examples well for example you can build browsers with c plus plus your browser was probably built with c plus plus and then games as well the ones that are very fast those have their engines probably written in c plus and then other programming languages operating systems most of them have have their core written in c plus or c programming languages your car your mobile phone your computer a lot of drivers and embedded systems of those are written in c plus and then there are always examples of compilers interpreters media editing data mining and machine learning as well those are examples of things that are very heavy on the cpu and you are definitely going to notice a difference between a solution for those that is written in c plus plus and then the one that is written for example in java or c sharp or python or nodejs you know because c plus is so much closer to the hardware and that allows it to manage cpu and memory and resources in general very efficiently and very fast now with that being said c plus plus is not going to be exactly easy to learn because c plus is very big language and there is a lot of things to learn however if you are someone who is interested in the things that i mentioned or you are interested in competitive programming for example then you definitely are going to benefit from learning c plus now there are a couple more things that are important to mention when it comes to c plus and those are that c plus plus is compiled language and that means that the code that you write first has to be converted in a code that your machine can understand and that is what compiler is used for simplesplus as well is case sensitive language and that means that it is going to matter matter if you are using uppercase or lowercase and c plus plus is as well statically typed language that means that once you declare a variable you cannot change the type of that variable so once you declare it you assign it a type and then later you cannot assign it a variable or a value of a different type um in order to start writing c plus you will need just a text editor and a compiler however what i would recommend is using an ide meaning integrated development environment and that means that it is going to have everything that you need in order to write your code compile it and debug it on one place now there are many ides to choose from you can use code blocks you can use clips but the one that we will be using is called visual studio and here i'm going to insert a clip on how you can download and install visual studio because depending on your connection and your computer that can take a couple of minutes in order to download your visual studio you will have to navigate yourself to this page here so visual studio microsoft vs look for that link in the description of this video and then you click here download visual studio and you are going to select community 2019 you can download that for free so once you have this installer file you are going to click on that and then here you should click continue and now this should this shouldn't last long okay now we have this window here and what you should select here and what should be enough for this course is going to be desktop development with c plus plus so i'm going to select that and as you can see it is 7.04 gigabytes so it is going to take a couple of minutes so i'm going to pause the video here so just click install and that should be pretty much it let's now create our first visual studio project for uh c plus plus console application so i'm going to open my visual studio and then here i will select create a new project now here i want to select c plus console application so i'm going to type that c plus and then here it is console application and then click next and here you will have to name your project and your solution and i'm going to say cpp crash course like this so cpp crash course and then click create and now your project your solution is being created okay now here we have something called solution explorer and here you have the project that we just created and then here in this source files folder you have cppcrashcourse.cpp file which is this file here the one where we will be writing our code now as you can see we have gotten some default text when we created our project now these are examples of single line comments this is a single line comment and then this is a single line comment as well and for creating a single line comment you use this slash slash and then your comment comment is not going to be compiled nor your computer is going to try to understand your comment it's just for you so i'm going to delete this because we don't need it and then this part here as well great and then now as i already said c plus plus you have to compile so how do you compile your code how do you build your code you click here build and then you can say build solution and here you will get a message it says once succeeded meaning that you don't have any compile time errors now you can do compiling and running your code all at once so you can click here this local windows debugger or you can click debug and then start debugging whichever you prefer and now your program has started and as you can see we have gotten this hello world message that is written here and we have as well this junk text which we want to remove so i'm going to use a command for that and that is system and then here you say pause greater than zero like this so now if i run my code again you can see that it has only this hello world message excellent now let's add another message so i'm going to copy this line of code and let me copy my message as well so here i want to say for example welcome to c plus plus crash course uh and an end line so a new line at the end and as you can see in order to write out a message to our console we use this see out command and then this std is a namespace so it is a standard namespace that you have to use in order you have to include it in order to use this c out command and many more so now when i run this you can see that welcome to dc plus plus crash course has as well been written out in our console window okay now the first thing that i want to do is going to be really to include this std namespace at the beginning so i'm going to say using namespace std now here i have included this standard namespace so i don't have to write it here nor here i can remove that however i want to say that this here is not going to be the recommended way of doing things because this here is going to include many more things than just ones that we will need for this application however i'm going to use this in order to save some time but the recommended way would be to use only the things from this namespace that you will be really using in your application so only include those okay now i'm going to delete this because the next thing that we will be talking about are going to be variables and data types a variable in c plus is just a container that takes memory and that container can hold a data of a certain type now that type in c plus plus can be an integer number it can be a floating point number it can be a character a boolean value a string an array and here i want to show you the most basic data types and then later in this course i'm going to show you how you can create your own data types in c plus now let's create a couple of variables first one that i want to create is going to be integer variable so you say int and let's give it a name let's say age is equal to 25 so i have assigned a value to my variable you don't have to assign a value to your variable uh when you create it and you can change that value later as well so you can say age is equal to 26 for example however if you say that your variable is for example const oh const then you will not be able to change the value that you have assigned to your variable as you can see here it says expression must be a modifiable value meaning that this here is not a value that you can modify because it has been declared as constant okay so i'm going to remove that for now or you can just not assign a different value to it whichever you prefer now another type that i want to show you is going to be float meaning floating point number and let's say for example weight and here i want to assign 65.5 for example kilograms so that would be an example of a variable that stores a decimal point number now another way to store a decimal point number another type is going to be double like this and difference between float and double is that double can store double the data it is double size of float which means that it can show a larger number it can hold larger number or you can represent a number to a more uh more with more decimal places to a higher precision so i want to say for example balance meaning bank account balance if you had a lot of money and you want to store that in a variable you are going to use double for that so i'm going to say for example one two three four five point six seven or you can store much much bigger number we are going to see that later how big of a numbers you can store in each of these now another type that i want to show you in c plus is going to be a character so for that you use char variable and for example let's say gender like this and then in order to assign a value to your gender to your character variable you will use these single quotations and let's say that you use m for male and then fee f for female like this and as i already said in order to assign a value to a character variable you use these single quotations now another type that i want to show you that can store more than one character you can store um more characters is called string like this and let's say username and for that i'm going to use these quotation marks and for example code beauty like this okay and as you can see in order to assign a value to your string variable you use these double quotations okay and then the smallest data type in c plus plus is bull so you say bull and let's say is today sunny like this and the day is not sunny i mean it is currently but it wasn't like two minutes ago the weather is just blah so i'm going to say false here okay and then another type that i want to show you is going to be array how you can create array in c plus and you can look at an array as a collection of variables of a certain type now array takes a continuous space in memory and that means that when you declare an array you will have to specify its size up front now um in c plus plus as well you cannot have an array that stores a character and then an integer and then a string so for those of you coming from javascript world i'm sorry so in civil class you cannot do that so you have to specify the type of your array up front so you have to say for example string and then let's say colors like this and i want to have for example five colors and here i want to specify uh those colors so i'm going to say red and then let's say green and pink oh pink and then blue and let's say black for example so this is going to be my array of colors now if you do if you create your array and then you initialize it you can omit this size of your array and now your your array is going to have size of five of these string variables and you can as well initialize your array and say that it is going to have a size of 10 for example but that you want to initialize only the first five and then you have five spaces uh five of these variable places free to add later okay and then you don't even have to initialize these up front so you don't have to give a value to your variables that you are storing in your array initially now after i have created this array let's show how you can write out the uh value that is stored at a specific position of that array so for that i'm going to use c out command and let's say for example that i want to write out whatever is stored on the first position of my array and for that i'm going to use index of 0 because indexation of your array starts with zero so indexing uh actually now when i say see out colors of zero i expect to see this red written out in my console so when i run my program you are going to see that it says red now if you want to reassign the value that is stored on this position in order for that we are going to use another command and i'm going to see for i'm going to say for example c in and let's say colors of zero oh of zero like this and now i'm expecting my user to console input the value that is going to be stored at this first position of my array so when i press this you can see that my cursor is blinking and let's say for example that i want to say yellow that is the color that we haven't oh i haven't written out this value so i'm going to stop my program and i will have to again write out the value that i have stored on this first position so i'm going to run it one more time and i'm going to say for example yellow like this and then as you can see it has written out yellow as the value that is stored on the first position of our array so we have seen how you can enter value in your application for that you use c in and then you can as well change the value that you are storing on a certain position of your array by just saying for example colors of let's say one and then you hard code that value here so you say for example um gray like this okay so as i said since your array is holding a continuous space of data in memory that means that you will have to give it this maximum amount on its creation and in this particular situation this means that your colors array can hold 10 strings not more than that now because your array is holding that continuous space of data in memory once you come to that max limit you will have to recreate your array you will have to re-initialize it so that it can take that continuous space of memory somewhere else bigger space of memory now because of that as well accessing these members is going to be quicker because they are one after the other now here i have shown some of the most important basic data types in c plus but there are variations to these for example you can say that you want to store only positive numbers in your integer or you can say for example that you want to use long and or a long long and in order to store a bigger number in your integer variable so there are many variations to these and in order for you to see that i'm going to include table here so if you want you can pause the video and check that out for a couple of minutes and before we continue i want to make a quick digression because i know that some of you are wondering why i'm using this light visual studio team it's because i don't have any other lighting except the one coming from my monitor so if i use dark then you wouldn't be able to see me that's the reason i prefer dark visual studio okay now the next thing that i want to talk about is going to be conditions selection in c plus uh meaning how can you execute one part of the code rather than the another one depending on a certain condition and in order to demonstrate that i'm going to use this variable is today sunny and now i want to check it i want to check if is today sunny and and if this here results as true i want to write out a message to my user i will say go to the park like this and let's add and lie so if this here is true our user has to go to the park else meaning if this here results as false i want to write out a different message to my user and that is going to be for example take an umbrella okay now when i run my program since this variable here is holding the value of false it says take an umbrella okay now i'm going to stop my program and there is as well shorter way to write if else statements and that is going to be using ternary operator or conditional operator and what is the syntax of it you first specify the variable or condition expression that you want to check okay like this and then you put question mark and after this question mark you are going to specify whatever is going to be executed in a situation where this here is true so this line of code and then in the case that this here results as false you are going to execute something else so you put this column sign and then you put the line of code that you want to execute if it is false like this so now i'm going to comment this and as you can see this is multi-line comment and now if i run this you can see that it says take an umbrella so this condition has been checked and it resulted as false and this part here has been executed okay i'm going to stop my program and i'm going to comment this so that it is not executed anymore and this here is single line comment now i'm going to uncomment this okay and now i want to show you how you can bind and nest your if else statements meaning how you can make if and if else and how you can make if or if else in order to do that i'm going to add another variable and let's call that variable uh for example is today weekend like this okay now what i want to do is i want to check i want to check if is today weekend and at the same time is sunny like this and in a situation that today is weekend and it is sunny i want to write out the message go to the park else what i want to do oh i'm going to remove this and in a situation where either one of these results is false i want to check something else so i say else if and then i say please check is today weekend like this and then check as well if it is not sunny like this and in that particular situation what i will do i will write out something else to my user and that is going to be take an umbrella so let's write out go to the park but take an umbrella so it is still weekend but it is not sunny and then let's cover another situation and that is going to be our final else and in that situation what i want to do is i want to say to my user for example go to work like this okay now this was an example on how you can write multiple if else's and now if i run my program you can see that it says go to the work because this variable here is false and then this one here is false as well so these two have not been executed and then this final else has been executed okay now if i change these and i say for example that today is weekend and it is not sunny and i run my program it says go to the park it says go to the park okay go to the park but take an umbrella okay we should correct that okay now i want to show you something else and that is going to be how you can nest if else statements so i'm going to comment this here so that it is not executed anymore and then i want to show you how you can nest your if else statements and that is going to be our following example so for example you say if please check is today weekend like this and then if this here results as true what you can do is you can check something else and that is going to be if is today sunny like this and then if the day is sunny what you can do is you can write out a message to your user to go to the park like this but if the day is weekend but today is not sunny then you write a different message to your user so you say else and then you say go to the park but take an umbrella with you okay now in a situation where this here results as false then you are going to execute another part of the code and that is going to be this one here so you say else go to the work like this okay now when i run if i run this program let me just show you whole code like this and if i run this program you can see that it says go to the park but take an umbrella because today is weekend but today it is not sunny so you can go to the park but you have to take an umbrella with you okay now i'm going to stop it and let's do um let's say that today is sunny that is true as well and if i run it it says go to the park because it is sunny and it is weekend okay now both of these do the same thing this part of the code and then this part of the code as well it just depends on how you really want to write this code let's now talk about switch case and switch case is a form of selection that you use when you have multiple options to choose from so multiple cases and switch case is usually used when you don't necessarily check on a boolean value so you check on some other data type so i'm going to create here an enumeration so i haven't mentioned enumerations earlier but i'm going to show you how you can create an enumeration in c plus because we are going to use that for this example so you say enum and then let's call this enumeration i color like this and then here you put variance you put um elements of this in a variation so you say for example brown and then blue and then um green i think and then we have gray as well and let's say other to cover other possible eye colors but let's say that these are the main ones so now we have created enumeration called eye color and we have given it these five values these five variants so brown blue green gray and other and let's now create a variable which is going to be off type eye color and let's call it eye color as well only lowercase e like this and i'm going to give it a value of brown because that's my eye color i guess okay and now i want to show you how you can use switch case in order to check for all of these five values and then write out how many other people have that particular eye color in order to write your switch case you say switch like this and then here in these parenthesis you put the variable that you want to check like this and now in the curly brackets we are going to cover all of these five cases so you say case and then let's put brown first okay now in the case that our user has brown eye color i want to write out a message to my user and i want to say that eighty percent of people have brown eyes okay and let's as well cover all of these other five options so i'm going to um copy this and i'm going to say in the case that eye color is blue i'm going to say that i think 10 of people have blue eyes like this and then in the situation where our user has green eyes i'm going to say that um i think two percent of people have green eyes and then check gray like this one percent of people has gray eyes like this and then let's cover this other case meaning all of the other colors and let's say that five percent or how many is left seven percent of people have uh some other eye color seven percent of people have other eyes no seven percent of people have some other eye color like this okay now one thing that we are missing here is going to be break statement so i'm going to write break like this and i will have to copy this on all of my cases and this break is used in order to stop your switch case so once you've found the right case the right code to execute that code is going to be executed and then you break your switch case which means that other cases are not going to be checked but the first one that is executed after that when you find break you are going to leave your switch case statement okay now when i run this code it says that 80 of people have brown eyes let's change this to say for example blue like this and now it says that ten percent of people have blue eyes now since this enumeration here um these variants in the background are really integers which means that this here has the value of zero and then this here has the value of one and then two three four that means that here it is really checking integers now here i can put an integer as well then so i can say for example 99 and that situation is not covered in this switch case so let's cover all of that all of those situations where our user puts a value that is not valid that is not covered with any of these cases so here i want to write out a default case which is this one here and what i want to say in this default situation is going to be following so i want to say not valid eye color okay and now if i run this and i have hard coded this 99 value you can see that we will get this not valid eye color because that is how our switch case works and then here you can put break if you want but you don't have to because you are not going to get that bug that you are going to get in the case that you for example here don't have uh break so let me delete this break and let's put this eye color to be blue so i'm going to copy this variable and paste it here just to demonstrate to you what is going to happen and now as you can see it says 10 of the people have blue eyes and then it continued executing other cases because we have forgotten to put this break statement at the end of this line here so at the end of this blue case the next concept that i want to talk about are loops and loops are used when you want to execute the same part of code multiple times or as long as a certain condition is true and first i want to show you an interesting concept which is a concept of infinite loop that you can make in c plus and then i'm going to demonstrate examples of while loop and do while loop and i'm going to show those two together because um i really want to point out the difference between while loop and do while loop so the first one that we will create is while loop and on that example i'm going to demonstrate an infinite loop so let's create a variable which we will use as counter like this and let's initially give it a value of one okay now what i want to do is i want to write out the syntax of while loop so you say while and then in these parentheses you put the condition the condition that your loop is going to check each time that it wants to make an iteration and then in these curly brackets here you put the block of code that your while loop is going to execute now here i just want to write out the value of my counter so i'm going to say c out counter and then add end line like this and then here my condition is going to be for this to be iterated as long as my counter is less than or equal to 10 like this now this what we have written here is an example of an infinite loop why because here we haven't changed the value of our counter our our while loop enters this block of code and writes out the value of our counter and then it goes back here and then checks whether this condition is true this condition is true because our counter is one and then it iterates through this again and again and again because we have never changed the value of our counter and if i run my program i can demonstrate that and as you can see our loop is running and that was an example of an infinite loop and you can make infinite loop with either while or do a while or four it really does not matter if you miss uh if you skip the step of changing the value of your counter or if you skip the step uh where you are going to make this condition here false at a certain point you have gotten yourself an infinite loop now how we can reach this condition here how we can make it false at a certain um at a certain time well we can say counter plus plus meaning in each iteration of our while loop we will increment the value of our counter by one so now when i run this code you can see that our while has been executed 10 times and then it has stopped because in 11th iteration our counter was 11 and this condition here was no longer true so our while loop is not going to execute at that point okay so that was an example of while loop let me now show you the example of do while loop so i'm going to copy this line here so that we can really make a difference between what our while loop has written out and then what our do-while loop has written out so in order to create a do-while loop you say do and then here you will put your code that you want to execute and here you put while so at the end you put the condition and i want to use the same condition so i'm going to copy this condition from our while loop and as well i want to copy this here like this okay so now when i run this program you can see oh we have used uh the same counter so what i have to do is i have to change this counter so i'm going to use different counter uh for my do while loop so i'm going to say for example do um do while counter like this and let's very quickly change this counter to be our do while counter like this okay now it should work and now if i run my code as you can see it says it says while loop and then our while loop has run 10 times and then our do while loop as well has been executed 10 times now what is the difference the difference is in the following your do while loop is going to execute at least once always but your while loop doesn't have to meaning in a situation where this condition is false there is not going to be iterations of your while loop and if this condition is false before your first iteration started then your while loop is not going to be executed your while loop is not ever going to enter here and execute this block of code but your while loop it first does it first says do and then it checks this condition so in order to demonstrate that i'm going to change the value of this counter to be 11 for example and then here as well and now if i run this code you can see that our while loop has not been executed not once but our do while loop has been executed once even though if we hit even if we have here this do while counter is less than or equal to 10 and our counter is 11. we have had one iteration of our do while loop because it first does and then checks conditions so that is the difference between while loop and do while loop now another loop that is often used in c plus is for loop and for loop is often used when you know in advance how many iterations you will have now one example could be to enter or to write out elements of an array so let's do that let's create array of strings like this and let's call it animals for example and let's say that we will have five animals and i'm going to initialize this array so cat and then dog and then let's say for example cow and goat and let's say b oh b and i'm on purpose choosing animals with short names so that i don't make typo and i'm sorry if i did somewhere in this course okay now when we have this array what i want to do is i want to iterate through all of the elements and write out the values so for that i will be using for loop so you say four and then there are three things that you will have to give to your for loop so the first one is going to be to initialize a counter that you will use so i'm going to say int i is equal to zero and i want to start with zero because indexing of our array starts with zero so the first element that you that we would like to access is going to be the one on zeroth position so the first thing that we give to our for loop is going to be uh initial value of our counter after that we will need to put a condition so let's say while i is less than 5 and then the third thing is going to be to either increase or decrease value of this counter so that we don't end up with an infinite loop so let's say i plus plus okay and then what i want to do here is i just want to write out the element that has that index so i'm going to say c out and then let's say animals of that index so animals of i and let's add and lie and now if i run this code as you can see we get these cat dog cow goat and b so all of the elements of our of our array in this chapter i want to talk about functions in c plus and a function is a block of code that is organized and grouped together in a way so that it can perform a specific task now dividing your code into functions is going to make your code reusable and that is going to make your code easier to read and easier to maintain now i want to show you two examples uh i want to show you example a function that returns a value and then example a function that does not return a value and the first one is going to be a function that returns a value it is going to be a function that is going to sum two numbers and then return the result to us so i will say float that is going to be the return type of our function and then i will call my function sum and here you pass arguments to your function so you say which arguments your function is going to receive and those are going to be two arguments both of type float so float a and then float b let's call them like that let's call them a and b and this here is called function declaration now this function is missing definition and definition of function goes after your main function and then declaration of function goes before your main function so i'm going to copy this and then paste it here and here i want to write the definition of my function that is called sum so this function is just going to sum these two numbers and return the result so i'm going to say return a plus b like this okay now how do you invoke this function here in my main function i will say sum and then you pass two arguments two parameters to your function so here i will say let's say 2.2 and then the next one is going to be 5 like this and what i want to do with this function is i want to write out whatever this function returns to me so i want to write out the result of summing these two numbers and for that i say c out and then write out the result and let's add end line at the end and if i run this as you can see it has written out the result of two point two plus five now as i said this is going to make your code reusable so if i duplicate this and here i say 5.5 plus 3 for example and then here i say 4 plus let's say 8 and if i run my code now as you can see here are the results of summing these numbers that we have passed to our function now that was an example of a function that returns value in this particular case it returns float and another type that i want to show you is going to be a function that does not return a value so in order to declare that function you will use return type void meaning nothing so you say void and then let's say that that function will be called introduce me like this and that function will receive two arguments the first one is going to be name and then the second one is going to be age so i will say string name and then let's say int age like this now as i already said this here is the declaration of your function and then definition goes after your main function so here and here i will say that this function is just going to write out whatever we pass to it so c out my name is and then name and let's add endline as well and i want to write out age so i will duplicate this and i will say i am and then age variable and let's say years old like this so my name is and then this name variable and then i am whatever our user has passed as age years old now i'm going to invoke this function as well and i cannot write out this function so i cannot use c out operator but i just invoke function that is going to be of type void of return type void so i will say introduce me like this and here i will pass for example saldena and let's say the age will be 25 and now if i run this function as you can see it says my name is saldina i am 25 years old and let's use it one more time so i will say for example anna and let's say that she's going to be 35 and if i run it as you can see it says as well my name is anna i am 35 years old now there is as well a concept of default parameter in c plus plus so for that i'm going to assign a default value to my age parameter here and i'm going to say that default value is zero and now this means that you don't have to pass this age parameter to your function anymore so if i delete it from here and then i run my function i'll write my program as you can see it says my name is anna i am 0 years old and now you can do whatever you want with your function in the situation that you haven't passed this age parameter in the previous chapter we talked about functions and then in this chapter i want to talk about pointers so let me explain what are those as i already said a variable is a container that can store a value and then a pointer instead of storing a value itself it is going to store an address of a variable so for example an integer variable can store an integer number so a whole number and then an integer pointer is going to store an address of an integer variable now pointers in c plus plus have many uses and if you really want to understand and work with pointers you will probably have to take more time than just these couple of minutes that i can dedicate two pointers in this crash course however in this course i will explain two of the most common uses of pointers and those are going to be using pointers with functions and then using pointers with arrays now the first example that i want to show is going to be using pointers with functions and in order to demonstrate that i will create a function and then i will introduce a problem to it and then in order to solve that problem we will use pointers so the first the first thing that i will do is going to be to create a variable and let's call that variable my age like this and i'm going to assign it a value of 25. now what i want to do next is i want to create a function so that function is going to be of return type void and let's call that function celebrate birthday like this and it is going to receive an integer parameter which is going to be called age now let's define this function here so the only thing that this function is going to do is going to be to increase the value of this variable so increment it and then write out a message so this function will say yay um celebrated let's say i want to write out this age oh i want to write out this age and then let's say birthday okay and let's add end line like this so this function receives age variable increments that variable and then writes out yay celebrated 25th 26th 27th whatever birthday okay now let's invoke this function here this celebrate birthday function and i'm going to invoke it in my main function but before invoking it let's just write out this value this my age so i will say c out like this and then let's say before function like this and i want to write out my age here like this and now let's invoke this celebrate birthday function here and what i want to pass to this function is going to be my age variable like this and then after this function i want to write a message as well so i want to say after function okay and let's just say age because we are writing out age like this okay now if i run this code what do you expect to see and i'm running it and as you can see it says before function age is 25 and then it says yay celebrated 26th birthday and then it says after function ages again 25 what has happened here so what has happened here is that this function here this celebrate birthday is not receiving this variable meaning it is not working with this variable here but it has its own copy and then whatever it performs of the calculations it is going to perform those calculations on its own copy which means that those changes are not going to reflect on this variable here and that is why we have 25 in this line and then invoking function and then again here we have 25 even though we have increased that value here now in order to solve that problem we will use pointers so how will i do that well here instead of passing my age variable what i can do is i can pass address of this variable so here i'm going to say please pass address of my age variable and this ampersand symbol here is going to do that it gives us address of a variable and as you can see since we have passed an address here here we really have to receive a pointer to address and in order to do that i'm going to add this star symbol okay and then let's add it here as well so here we have passed an address and then here we have received a pointer now if i run this we are going to get a problem it says before function age is 25 and then after function ages 25 but here in our function it says yay celebrate it and then this here is address okay so this does not make sense and why has that happened because here we have received age as a pointer and here we are trading at as a pointer as well meaning that here we are increasing address and not the value that is stored on that address and in order to do that in order to access a value we have to de-reference that pointer so that we do with this star symbol and i'm going to do it like this so i'm going to say please dereference this pointer meaning access the value that is stored on that address and then please increment that value and here as well i want to access the value rather than the address itself so i'm going to write out the value and i'm going to use this star symbol once more so now when i run my program as you can see it says before function age is 25 and then celebrating 26th birthday and then after that our function so after our function our main function has this my age holding value of 26 which means that these changes that we have made here applied to this my age variable and this is going to allow your program to have different components of your program accessing same memory locations meaning sharing data that is stored on those memory locations and that is one example of how pointers are used in c plus plus now another use of pointers in c plus plus is going to be using them with arrays and in order to demonstrate that i'm going to create an array let's make it an array of integers and let's call it lucky numbers like this and i'm going to make an array of five integers and here i will say that my lucky numbers are 1 3 5 7 and 9 for example and what i want to do here is i want to demonstrate you first what this name of array represents so i want to write out that like this so the name of this array really is the address of this array which is the address of this first element of array now if i run this as you can see we get an address but in order to demonstrate that that address is the address of the first element i'm going to stop the program and i'm going to copy this line and here i want to say please give me this first element and what i want is i want address of this first element and now if i run this program as you can see we have two of these same addresses which means that the name of an array is going to be address of its first element and in order to access the value on that address you just use this index so without this ampersand you say please give me whatever you can find on this index 0 of my array because indexing of arrays starts with 0. and if i run this now as you can see the value that is stored on this index position is going to be 1. now how you can use pointers with this well let's create a pointer which is going to point to int variable and i'm going to call it let's say lucky pointer now is that a meaningful name for a variable no i guess but it's cute so let's keep it like that let's say that our lucky pointer is going to point to whatever address our lucky numbers array is holding and that is going to be address of our first element as we already demonstrated now what you can do with this let me very quickly write out whatever my pointer is pointing to so i'm going to say pointing to and then let's write out lucky pointer so the address that this pointer is storing and then let's write out as well the value on that address like this and oh i'm sorry and we can access that value using this star symbol which is going to dereference our array like this and then add endline at the end now if i run this program as you can see here we have this pointing to the the same address as we did in this situation here and then the value that is stored on that address is going to be one same as here which is the first element of our array now what i want to do with this pointer is i want to increment it so i won't say lucky pointer plus plus which is just going to increment the address meaning that now our pointer is going to move to the second element and now if i copy this again here and i run my program once more as you can see now our pointer is pointing to the second address the address of our second element and the value that is stored there is going to be 3 which is the value of our second element of our array now this was an example of how you can use pointer in combination with arrays and this here can give you the possibility to iterate through your lucky numbers array by incrementing the value of your lucky pointer now one thing that you will have to be careful with is going to be to not access memory addresses that are not yours which means that this lucky numbers array has five elements the first one starts with zero and the last one ends with index four and that means if you try to access addresses with indexes 5 and 6 and 7 and so on those are memory addresses that are not yours meaning those memory addresses don't belong to your lucky numbers array and if you try to do that there are many issues that can arise from there one very important thing that i haven't mentioned so far is that c plus is object oriented language now what that means what is object-oriented programming well object-oriented programming allows us to represent real-life objects in programming and those real-life objects are going to be represented together with their properties or attributes and then their behaviors as well one of the most important concepts of oop are classes and objects now a class is a blueprint a template and then an object is going to be a concrete example of that class or an instance of that class for example a class can be an animal and then an object is going to be a concrete example of that animal for example a monkey a dog a cat cow you know or a class can be fruit and then an example of that class an object of that class is going to be strawberry banana apple i don't know peach you know now we have already talked about data types in this course and we have mentioned integer float double string boolean and what a class is class is as well data type but it is user defined data type now how can you define your own class how can you make your own class well in order to do that i'm going to go here and here let's create a class that is going to be called car for example so in order to do that i'm going to type class and then i'm going to give it a name car like this and don't forget to put the semi column at the end and then here in this car class what i want to do is i want to declare i want to define properties of my class now let's say that this car is going to have string name so it is going to have property that is called name and that is going to be of type string and then let's say for example that our car is going to have as well string color like this and then let's give it another property let's say that it is going to be for example double price like this now a regular oh double like this now a regular car is going to have many more properties than just these three but in order to demonstrate how classes are created and used i'm going to use just these three properties okay now how can you create a variable that is going to have this type so how can you create a variable an object of type car in order to do that i'm going to go here and i'm going to say car and then let's create a car let's say that that variable is going to be called my car like this okay now we have created a variable an object of type car and in order to set value to these properties what i can do is i can say my car like this and then if i press dot these properties should appear however they are not appearing and why is that case why is that not happening because in c plus all the properties all the members of your class are going to be private by default and that means that they are going to be accessible only within this class now if you want to make those accessible outside of your class what you have to do is you have to put access modifier which is going to be public so now when i put public here these properties should be available outside of my class and if i press dot here again as you can see these appear so we have caller name and price now let's set the name of my car and let's say that the name is going to be for example ford like this and then i'm going to copy this a couple more times so i'm going to set the color of my car and i'm going to say that the color is let's say red and then let's set price as well so let's say that the price is going to be let's say oh the price is of type double so we are going to assign that as we should so let's say that the price is uh what is this 50 000 okay that's okay for a ford now we have created an object of this class of this class car now what i can do with this let's write out these properties let's say that we want to write out all of these properties so in order to do that i'm going to say see out and then let's write name oh name like this and then i'm going to say my car dot name and i'm going to copy this couple more times so let's write out a color of my car i'm going to say here color and then let's write out price as well here and i'm going to add for this price i'm going to add dollars at the end like this so now if i run my program as you can see it says you have a car which is ford and that car is red and it costs fifty thousand dollars okay now if i wanted to create another car what i would do is i would copy this and then paste it here and let's call that car my car 2 and here i'm going to say that my car 2 dot name is going to be volvo oh volvo and then my car 2 dot color is going to be black and here my car two dot price will be for example seventy thousand dollars and in order to write out these properties of my volvo i will copy this and then paste it here and here i'm going to say my car to and then here and here as well so now if i run my program we will get information about both our ford and then our volvo however writing this is a bit tedious so now i'm going to show you how you can do this in an easier way in this chapter i want to talk about constructors and a constructor is a method that is called automatically when you create an instance of a class so when you create an object a constructor of that class is called now so far we had to assign values to these properties like this so we had to do that manually and then here as well but if we had a constructor we could assign these values on the construction of our object so let's create a constructor and in order to create a constructor we are going to do that in our class and there are two important rules the first one is that constructor does not have a return type and then the second one is that the constructor has the same name as your class so car like this this would be an example of constructor now because i want to assign values to these variables on the construction of my car i'm going to pass these parameters to my constructor so here i will say string name and then string color oh color and then double price like this okay and what i want to do with these parameters that i have received in my class i want to assign them to the variables so i want to say name is going to be equal to the name that i received in my constructor oh name like this and then color is going to be equal to the color that i received in my constructor and then price is going to be equal to that price that we have received in our constructor as well now what i can do instead of this here is i can say here that my car is going to have initial values of ford and then color is going to be red and then it is going to cost 50 000 dollars like this and now i can delete these because i have already created my car like this and i have assigned values to these properties in my constructor and then we can do the same thing with our second car so i'm going to remove this and i'm going to move my car here and let's say that our second car is going to be again volvo and the color is going to be black and that is going to cost 70 000 okay now when i created these two objects these should behave the same so if i run my program as you can see we still have these informations about our ford and then our volvo another important thing is going to be class methods and in order to represent a behavior of a class you are going to use class method which is going to be basically a function so so far we had these attributes of our class written out here and then here as well which means that we have repeated this code so we have written out attributes of our forward and then attributes of our volvo repeating this code however we can move this code to our class so i'm going to cut it from here and then here i want to create a method which is going to do that job so i'm going to say void void and then let's call that method get info like this and what i want to do in this method is what we have done here so here i'm going to delete this my card too and then this color as well and then price okay and that is because these properties are accessible directly within this class so we don't have to access these properties through an object but these are directly in the class okay so now if i delete this as well and i say for example my car dot get info and then let's say my car 2 dot get info we should get that same information information as we did before so i'm running my program and as you can see it says ford red color fifty thousand dollars and then volvo black seventy thousand dollars so that would be an example of class method three most important characteristics of oop are encapsulation inheritance and polymorphism and in this chapter i want to talk about encapsulation so what is this rule of encapsulation rule of encapsulation says that these properties of your class should not be public meaning these should not be available for your user so that he can change them as he wants but you should rather create some methods that your user can invoke and then through those methods obeying the rules of those methods he can change this data and now we are not obeying that rule of encapsulation because we have said that everything in our class is public and now i can say my car dot and then as you can see we have this color and the name and then price property as well these are available for us outside of this class and in order to obey this role of encapsulation of encapsulation what i will do is i will make these to be private so i'm going to say here private like this and then these my constructor and then this get info method these are going to stay public so i'm going to put that access modifier here now if i try to access these three properties outside of my class now as you can see these are not available anymore the only thing that is available is my get info method and then my constructor is available as well and we invoke that here okay now how am i going to access these properties of my class now that there now that they are private well what you can do is you can create get and set methods so you can create method that is called get name and set name which are going to get your name and then set the value of name and then you can create get color and set color get price set price but what i want to do here is i want to give you another example so let me add another property let's say that that property is going to be of type bool and that property is going to be is broken like this and what this property should represent it should represent whether our car is broken or not and i'm going to assign the value to this property in my constructor so initially when my car is created i want to say that that car is not broken so here i'm going to say is broken is equal to false like this okay now how i want to allow my user to change this property i'm going to collapse this once more and in order to allow to my user to change this property i'm going to create two methods and those two methods are going to be for example crash car and then repair car and our crash car method should set this property to be true so our car is broken after it crashes and then after it is repaired our car is not going to be broken anymore so i'm going to say void crash car like this and then here i want to write out a message c out and let's write out the name of our car first and let's say crashed like this okay and here i want to set as well this is broken property so i want to say once my car crashes i will set this is broken to true because my car is broken then and now let's create another method let's say repair car like this okay and here i want to say car repaired like this and i want to set the value of this is broken to false because after my car is repaired it is not broken anymore okay and now after we have hidden these after we have hidden these properties and then exposed these two methods what we should be able to do is access these public methods and then these should not be available anymore so now if i try to access the properties of my car as you can see the only available ones so the only available members of my class are going to be crash car get info and then repair car and all the other properties are going to be private meaning not available outside of my class now what i want to do in order to test these two methods let's create another method let's create third method which is going to be for example void move because cars can't move so what i want to do in this method is i want to check i will check if is car broken and now if the car is broken what i will do is i will say see out car so the name of my car and then let's write out broken let's write out is broken okay and line like this and then if the car is not broken else i'm going to write something else and that is going to be car is driving like this great okay now after we have created these three methods what i will do here is i will try to say my car and let's rename this let's rename this to be named ford and then let's call this other to be volvo okay now here i'm going to say ford dot move like this and if i run this as you can see ford is driving now let's try to tamper with this is broken property if i try to say ford dot is broken that is not available anymore however what i can do is i can crash my car so i can say ford dot crash car and then after i have crashed it let's try to move it then so here i'm going to say forward dot move again and as you can see ford is driving and then i crashed my for it and then it says fart is broken so you cannot drive your for it anymore okay now if i stop this and then i try to repair my car so i say forward dot repair like this and then i try to move it again oh i tried to move it again and we are missing the semi column here and i run my program as you can see it says ford is repaired now and then you can drive your ford so then for it is driving and that would be an example of encapsulation so we have hidden all of these properties from our user we have made these to be private and then we have exposed methods which are these three which our user can invoke and then in this method for example he can tamper with this is broken property so obeying the rules of this method he can change his broken property now these two methods this crash car and then this repair car would be much more complex in real life so for example in this crash car you would have to check how serious that crash is so um if the car is crashed to the point that it cannot drive anymore and then in this repair method you would have to check probably whether the car is even repairable so how the serious damage is you know but for this example we have just said that once the car crashes it is broken and then once the car once you even try to repair your car it is repaired okay another very important characteristic of oop is inheritance so i'm going to explain how inheritance works in c plus plus on a simple example so here we have this car class and this class has four properties it has name color price and then this is broken property and we have defined as well couple of methods for our car so we have defined this gut info and then crash car repair car and move and we have made a constructor for our car as well now what is going to happen if i wanted to create another car another type of car a very very special one which is going to be for example a flying car and then that flying car should have these same methods and properties as our car so it should have a name and then a color price is broken property and then it should have as well the ability to get information about that car and then that car should be able to crash and repair and move as well so what we should do probably is we should copy all of these right wrong in c plus we can use inheritance in order to inherit all of these members of a class in another class so here i can create another class which is going to be called let's say class flying car like this and then what i can do in order to inherit this class is i will put this column sign and then i'm going to say public car like this now there are a couple of things that i want to explain here and the first thing is going to be that this flying car class is called derived class and then this car class that we are inheriting from that is called base class now this public modifier it says that whatever is public here so whatever is public in this car it is going to be public here as well okay so now after we have created this flying card this flying car should have everything the same as this car here does so let me create an instance of this class so i'm going to say flying car like this and let's call it flying car and here i want to create my flying car so i want to create these properties for my flying car so let's say that the name of my flying car is going to be uh what's a good name for a flying car let's say sky fury like this and then color is going to be for example black and it should cost let's say half a million dollars like this is that too much though i don't know okay now once we have created this as you can see we have this error so i'm going to end this command and i'm going to return to this arrow it says no instance of constructor meaning that this flying car does not have a constructor and we have passed these three arguments but there is no constructor to receive them so here i'm going to create a constructor and i'm going to say flying car like this and then here in this constructor i want to receive these three so i'm going to copy them from this constructor here like this and then what i want to do instead of instantiating these three properties here in our flying car class i want to invoke my base class constructor and i can do that so i'm going to set this column sign and then i'm going to say please invoke my card constructor and then since my car constructor knows how to instantiate these properties i'm going to pass these properties to it so i'm going to say you are going to receive name and then color and price as well and you deal with those okay now this should be all set but but as you can see we still have this error here now what this error says now it says that the constructor is inaccessible so why is it inaccessible because uh if you remember all the members of a class are private by default and private members cannot be accessed from outside the class so in order to make this constructor accessible i'm going to make it public so i'm going to say here public like this and now this error should disappear okay now we have successfully created flying car and the principle of inheritance says that this derived class this flying car should be able to access methods from this class here so it should be able to access public methods and protected methods and properties as well from its base class so now if i try to say flying car dot as you can see we get all of these public methods from our base class and let's say that we want to write out information about our flying car like this and if i run my program now you can see that we have successfully written out the information about our flying car so it says name is sky fury and then color is blatz it should be black okay i'm going to correct that and then price is what is this half a million dollars okay so the color of our car should be black okay now we have successfully accessed method that was defined in our base class and we have accessed that method from our derived class and we have been able to do that using inheritance another very important concept related to oop is polymorphism now a polymorphism describes the ability of an object to have multiple forms to have many forms now in this particular situation we have this car and then we have created a derived class of this car and we have called it flying car and let's create another class in order to demonstrate how polymorphism works so i'm going to create another derived class and i'm going to copy this flying car and then let's say that this derived class is going to be called underwater car like this and let's make its constructor underwater car as well okay now after i have created this underwater car what i want to do is i want to create an object of that so i'm going to copy this and then paste it here and let's say here that it is going to be called underwater car like this and the name should be let's say sea storm and the color should be blue and then let's make it 600 000 for example and now what we should be able to do in polymorphism is to have the same method the method that has same name but different implementation and in order to demonstrate that i'm going to use this move method that we have already created in our car and it is a method that checks whether our car is broken and if it is it says that the car is broken and then if it is not it says that the car is driving and that is the situation with our car so with our base class now i'm going to copy this method and i'm going to make different implementation of this method here so here what i want to change is that my flying car is not driving but it is flying so that is different implementation and then for our underwater car what i want to do is i want to say that my underwater car is diving for example okay now these are some errors that we got and this name and then this broken property they say that the member is inaccessible now why is that happening this has nothing to do with polymorphism but it has to do rather with this access modifier we have said here in our car that these properties these four are private and that means that these four are going to be accessible only within this class so you are going to be able to access these only from this class now in order to make these accessible outside of the class you would make those public but we don't me we don't want to do that what we want to do is we want to make these accessible from derived classes and there is a third access modifier that is going to do that and that is protected access modifier so i'm going to create that here i'm going to say protected like this and then i'm going to move this name and then this is broken to my protected area like this and now we should be able to access these two from our derived classes and if i scroll down as you can see the errors disappeared okay now we can access is broken and then name so returning to polymorphism we have implemented this move method in our underwater car and in our underwater car it says that the car is diving and then in our flying car it says that the car is flying and then in our car class it says that the car is driving so what am i going to do now i'm going to create or i'm going to actually invoke this method for these three objects so i'm going to copy my forward that is just um base car class and i'm going i'm going to say four dot move like this and then let's copy this two more times and i'm going to invoke the same method for my flying card and then the same method for my underwater car like this and if i try to run my program now as you can see each of these objects has invoked its own method so ford is driving and then sky fury is flying and sea storm is diving which is okay so you can do that with polymorphism so you can have the same method the method that has same name but different implementation now there is also one interesting thing that is related to polymorphism as well and i'm going to demonstrate that in a minute so if you remember when we talked about pointers we said that pointer is going to hold an address an address of a variable and we said that pointer is going to point to the same data type that that pointer has so if i create for example an integer variable like this and let's assign it a value of 5 and now i want to create a pointer that is going to point to this variable i'm going to say int pointer and then let's say end pointer like this and i want to assign it the address of this variable here so i'm going to say please assign the address of this variable like this now this is okay this works however if i try to change this the type of my variable if i try to change that to float for example like this as you can see we are getting an error it says a value of type float pointer cannot be used to initialize an entity of type endpointer so you cannot do this now what you can do in polymorphism is you can have a pointer of the base class pointing to an object of derived class so let me demonstrate that i'm going to delete this example here and what i want to do is i want to create a pointer of type car like this and let's call that pointer car 1 and what i want to assign to that pointer is going to be an address of flying car so i'm going to say assign address of flying car like this and let's copy this once more and here i'm going to say card two and this card two pointer i want to assign address of my underwater car okay now after i have done this what i can do is now i can invoke methods using this pointer for this flying car and then for this underwater car so here i'm going to say let's say for example car one and now let's invoke method that is called crash car and if you noticed it has changed my dot to this symbol here because this is used when you want to invoke a method using a pointer okay so i'm going to say car 1 crash car and then this crash car is a method that we have implemented previously and there is this one here it just says that the car crashed and then it changes the property of a car to this is broken property of a car to true and then you cannot drive that car later so you cannot move that car because it is broken okay i'm going to collapse this again and then i'm going to return here and this method that i have invoked on my first car let's do that with the second car as well so let's do that with my underwater car okay and now if i run my program as you can see it says ford is driving sky fury's flying storm is diving sea storm is diving those are these three methods and then here we have invoked these crash car methods and it says sky fury crashed sea storm crashed as well and now if i try to invoke these methods once more let's see and i run my program again as you can see it says these three lines these three last lines say that for this driving we haven't crashed that but our sky fury is broken and then our sea storm is broken as well now let's repair those i'm going to copy this and i'm going to call invoke the repair method instead so i'm going to say repair car and then here i want to repair my underwater car as well using this notation here so i'm going to invoke these methods again just just to see if they are going to move again after this and if i run it you can see that these last three lines indicate that ford is driving sky fury is flying and then sea storm is diving and before that we have successfully repaired our sky fury and then our sea storm as well using this here so that is something that you can do using polymorphism so you can invoke this um repair car and then this crash car method for your flying car and your underwater car which are derived classes you can do that using this base class pointer i hope that you enjoyed this video and if you did give it a big thumbs up and also subscribe to traversie media and the code beauty channel as well and i hope to see you in some other video bye and then let's change the name to be for example see storm like this and then color should be blue and the price what is this half a million let's say that it is going to be four hundred thousand so it is going to be a bit cheaper than this flying car should it though when you're driving underwater you're dealing with a lot of pressure that's not cheap is it submarine cheaper than an airplane 2 000 years later let's just not answer that question right now and let's assign a name to my car so let's say that it is going to be for example uh koenigseggera why not like this okay and then now i'm going to make a typo let's use bmw instead [Music] [Music] my
Info
Channel: undefined
Views: 178,531
Rating: 4.8840075 out of 5
Keywords: c++, c++ programming, c++ crash course, c++ tutorial, c++ for beginners
Id: 1v_4dL8l8pQ
Channel Id: undefined
Length: 94min 39sec (5679 seconds)
Published: Wed Aug 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.