C++ Programming | In One Video

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to the draft Academy my name is Mike this video is part of a series of videos where I'll be going over the basic syntax of different programming languages and one of the most annoying things about picking up a new programming language is having to learn all the syntax from scratch and my own experience it generally manifests in a lot of google searching and Stack Overflow from experienced developer the problem isn't understanding the concepts of programming it's knowing how to implement those concepts in the new language so things like variables data types data structures loops conditionals an object orientation all of these things are done a little bit differently in each programming language and having to research how to do it all can be extremely annoying and tedious in this video I'll go through and show you the basic syntax for the most common parts of the language I'll give you a brief history of the language and we'll touch on some of the best practices and conventions used in that language now this is meant to be a quick run-through I won't be showing you how to install or configure anything and we won't really have time to learn how to do everything you see I've distilled it down into what I consider the core concepts so the goal for this is to be a quick video so I can't cover every aspect of the language without being said this video is meant for programmers who already understand the core concepts of programming it's not going to teach you any programming concepts I'll simply show you how to do all the common things in the new language so if you argue to programming draf Academy has hundreds of videos where I hold your hand and I walk you through everything you need to learn to program but this is meant for developers who already understand programming so without further ado let's get started C++ is a general-purpose statically typed object-oriented programming language that began in 1979 by Danish computer scientist Bjorn Stroh Stroh for several years the language evolved until its official release in 1985 C++ is essentially an extension of the C programming language it actually began as a fork of an early pre standardized C and while C is not a strict subset of C++ the two languages are largely compatible and in many instances C code can be used with little or no modification as C++ code because C both cost is an extension of C many of the things you can do in C you can also in C++ but in addition C++ gives you all the power and flexibility of object orientation so you can use low-level system calls manage memory and deal with pointers while at the same time working with classes objects inheritance and all the features of an object-oriented language so C++ really lives in two worlds all C++ programs must utilize a compiler whose job is to compile the C++ code down into machine code which is readable by the computer so anytime you want to run a C++ program you have to compile it first C++ relies on the user to manage the program's memory although there are garbage collectors available for C++ many developers choose to write C++ using a basic text editor but there are also more specialized integrated development environments some of those popular are code blocks Eclipse and NetBeans and also Microsoft's Visual Studio so what I want to do is just give you guys a full overview of the basic C++ syntax we'll look at all the different things you can do in the language and how you can do them and hopefully this will just be a great introduction into this language so notice over here I just have a basic C++ program set up this is kind of like the skeleton for a program up here I have a couple include statements and generally these are going to allow you to work with things like strings and also this iostream will allow you to print out and get information from the user and then down here I'm saying using namespace STD and this stands for standard so it's just a standard namespace and if you put this up here it'll make your life a lot easier because we won't have to prefix all of our C++ method calls and things like that with STD so just put this up here and then you'll be able to follow along with everything that I'm doing then down here we have this main function and this function is basically what's gonna get executed when we run our C++ program so any code that I put inside of here is gonna get executed when I run the program so I'm gonna be executing my code over here in the command line and that way you guys will be able to see the code and then you'll be able to see what the result of executing the code is first thing we'll look at is printing and the way that we can print is by using this C out command and see how it just stands for like console out and basically I can say C out and then less than signs and then basically it'll print out whatever I put over here and then I can also make two more less than signs and put ndele now ndele is a special command that we can use and this will basically just print out a new line so you'll see over here I'm printing out a new line down here I'm not I'm just ending it off and then down here I am so you'll see over here when I execute this code that we're printing out hello world and then an exclamation point so depending on whether or not I use ndele it's gonna do different things but you can use these less than signs to basically print out different things so now let's talk about variables so variable the names are case sensitive and they can begin with letters or an underscore and after the first character you can have letters numbers or an underscore and convention says to start with the lowercase word and then additional words are capitalized so this is the same rules as the C programming language so if I had a variable like my first variable I would name it just like that a lot of people will call that like camelcase and because c++ is statically typed we have to rigorously define the types of each of our variables so over here I have a string and a string isn't actually a primitive it's not a built-in type you'll see up here I'm including string like that that'll allow me to use the string although a lot of compilers will just go ahead and include that for you automatically so you don't necessarily have to put it up there so I can create a string with these open and close quotation marks and a string of characters again it's not primitive then we have a character and you're gonna delineate it using these single quotes and that's an you know a simple 8-bit character and down here we have integers so integers are going to be whole numbers and there's a bunch of different types versus a short and that's at least 16 bits signed integer and then we have the int which is just like the default and that's gonna be at least 16 bits signed integer but it's gonna be not smaller than a short then we have a long which is at least 32 bits signed integer and then a long long which is at least 64 bits assigned integer and you can make any of these unsigned by using the unsigned prefix so I could say like unsigned int and now this is gonna be unsigned instead of being signed and then down here we have our floating points so this is going to be decimal number so I can have a float and you'll see over here when I create the float I put after it and that's just a single precision floating point and we have a double which is gonna be a double precision floating point and then a long double which is an extended precision floating point so depending on the amount of decimal places that you need to take these to you can use different ones double is gonna be the default though and finally we have a one bit boolean so this is gonna be a true or a false value and you can see I don't have to give this a value right away I could just declare the variable and then down here I can give it a value you can also modify the values of variables just by referring to the name and setting it equal to something different and then if I wanted to print out a variable I could use this see out statement so I can say see out again less than less then I could type in like a string and then in here I could just weave in the variable just like that and then you'll see over here we're printing it out so that's probably the most common way to do this in C++ so next I want to talk to you guys about constants and a constant is basically a special type of variable that can't be modified so anytime that we're using constants in C++ we can just use this Const keyword so I could say int Const Berthier in this case I'm defining someone's birth year and generally as a convention whenever we're naming constants you always want to name them as uppercase so everything's in uppercase and words are separated by this underscore also if you wanted you can put the Const over there like behind it and that's gonna be just as fine as well what you can't do with these those you can't modify them so like I said Berthier is equal to 1988 we couldn't actually do that because it's a constant so we're not gonna be able to modify it so now let's talk about casting so with casting we can convert data types into different data types so for example down here I have a double and if I can cast it into an integer so it'll basically take this double and put it into an integer and then I can do the same thing for an integer so I have this integer 3 I could cast it to a double so now it'll be 3.0 I can divide 3.0 by 2 and we'll get 1.5 so you can see over here is the result of doing those casts so since we're on the topic of data types and variables I want to talk to you guys about pointers now a pointer is basically just a type of information that we can represent in our C++ programs it's basically a memory address so we can work with and access different memory addresses using C++ and actually this is kind of taken from the C programming language which like I said C++ is a fork of the C programming language so a pointer is basically just a memory address so for example I have down here int num is equal to 10 so I have this number integer what I can do is I can print out the memory address of that integer just by using this ampersand so if I prefix the variable name with an ampersand this is gonna give me the memory address where it's stored so over here you'll see we have that memory address and that's a hexadecimal number and then over here if I wanted I could take those memory addresses and I could store them inside of a variable so again memory addresses we would just refer to them as pointers in C++ and just like you can store an int or a double inside of a variable you could store a pointer inside of a variable the way that I do that is just by specifying the name of the datatype and then if I want to make a pointer I need this asterisks and then type in the name of the pointer variable and then over here I can store the pointer so I can store the memory address which again is access to using this ampersand and because I'm storing the memory address of an integer like up here num is an integer I need to put an integer over here then what I could do is I could print out p num so over here you'll see that I'm printing out that memory address again and then we can also do something called dereferencing a pointer which is basically going to go to that memory address and grab the actual value it's like an dereference a pointer by using this asterisk and that'll dereference p num and it's gonna get 10 so we're getting the value that's stored at that memory address now I want to talk to you guys about strings so a string is one of the most common data types that you can be working with and C++ there's a lot of information that can be represented using strings so like I said before whenever we want to use strings technically you need to use this include statement so include string I had that up there at the top of the file but a lot of compilers will automatically include that for you so in a lot of cases it's not super necessary that you put that in there but just to be fully correct you're gonna want to include that and down here I have a string greeting and the indexes with strings start at 0 so I would say that this age character is at index position 0 all the way up to 4 so even though hello has 5 characters the last character is at index position 4 because we're starting the counting at 0 that's pretty standard across all programming languages now down here I'm just printing out some different functions or attributes about the strings so I can say greeting dot length open and close parentheses and that will give me the length so actually if I put and I'll here it'll be a little bit easier to see and you can see over here we're getting five that's how many characters are there you can also access specific characters inside of the string so I could say like greeting open and close square per square brackets and then the index so if I say greeting open close square bracket zero gives me this H because that's the first character I could also check to see if a substring is inside the string so I could say greeting dot find llo and this is gonna tell me whether or not LLO s in the string and if it is it'll give me the index so this gives me a 2 because llo starts at index position two we can also get a substring so I guess a greeting dot su be STR it stands for substring and if I put a two here it'll basically grab all the characters starting at in exposition to going to the end so it starts at two which is this L and it grabs all the way all the way to the end which you can see here you can also pass in two arguments so I could pass in like one and this is gonna be the starting index and I can pass in a three and that's going to be the length so it'll start at one it'll start at this e in order to grab one two three character so we're gonna have a ll and you can see we grab that over there there's a bunch more of these string functions and if you just Google C post class string functions and you'll find a full list of them but that's kind of just an introduction into some of the most common ones in addition to representing strings and plain text we can also represent numbers so a lot of times you're gonna want to be working with numbers and your C++ programs so down here I'm just showing you guys some basic operations that we can do on numbers so I can say like two times three and that will give me the result of that multiplication you can also do addition subtraction division and multiplication so all the basic math operations you can also use the modulus operator so the modulus operator will divide two numbers and return the remainder so if I said like ten mod 3 this will divide 10 by 3 so 3 and will give me the remainder which is 1 so that's why we get a 1 over here if you want you can also specify order of operations so down here I have this little math equation so it's 1 plus 2 times 3 by default c-plus possible do the multiplication first so they'll say 3 times 2 which is 6 plus 1 and then we'll get seven but if I wanted to do the addition first I could just surround it in parentheses like that and now when I run the program we're gonna go ahead and get nine because now it's doing one plus two is three times three is nine I also want to show you guys how integers and doubles interact so how in whole numbers and decimal numbers interact so if I have a whole number and I do a math operation on it with a decimal number like that we're gonna get a decimal number back so if I do 10 divided by 3.0 we get 3.3 repeated but if I did was to make this an integer so now this is no longer a double now we're just gonna get three back because when you do math operations on two integers you get an integer back when you do math operations on two floating points you get a floating-point back or if you do a math operation on an integer and a floating-point you get a floating-point back down here there's a couple other things we can do so I have this integer called num I gave it a value of 10 I can say num plus equals a hundred and what this will do is it'll add a hundred to the value stored inside of num and it'll set num equal to that value so now when we print out num you'll see it's equal to 110 because we added a hundred to ten and now has the value of 110 you can also increment so I get say num plus plus and what this will do is it will print out num with one add it to it so plus plus increments you could also do - - which would decrement so now we get 111 over here there's also a bunch of different useful math functions that you can use in C++ so what you can do is just Google like C++ and math functions you can get access to you know square root power logarithmic stuff sine cosine tangent stuff all that you know standard math function stuff you can get just by like I said looking up a library that will give you access to that so now let's talk about getting user input specifically getting input from just like the consul's this is like the most basic form of getting input what we can do is we can actually use a specific C++ function called C in so over here you'll notice that I'm just creating a string called name and then I'm saying C out enter your name so I'm prompting the user to enter information you can see that's what's happening over here then I can use this C in function and what this is going to do is it's basically going to allow the user to input information so it'll pause the execution of the program and wait for the user to enter something and I can store whatever they entered inside of this name variable so here I'm just passing that string name and then over here I can just see out hello name so if I was to come over here and enter my name Mike now when I hit enter it says hello Mike I also have this other one down here this is gonna demonstrate how to get numbers from the user so I have two integers num1 and num2 and again I'm just printing out a prompt to enter the first num you can see that's over here and then I can see in for num1 and what this will do now is it'll take whatever the user enters and store it inside of this num1 variable which is an integer so it'll store it as an integer and I'm doing the same thing for a second number and then I'm just adding them together so I could enter in over here like a 10 and I can enter in a 20 and now you'll see I'm getting 30 so it's getting that input as an integer and it's able to add those two integers together no programming language course would be complete without arrays so imma show you guys how to use arrays the first thing we have to do when we create an array is declared the type of information that we want to store inside the array so this is similar to making a variable so I'm just gonna make an int I'm giving it a name lucky numbers and then you need this open and close square bracket after at the variable name then I can set it equal to some values so inside of these open and close curly brackets I can specify a different array element so you'll see over here the elements start at 0 so when we index these elements this first element element 4 would technically be at index position 0 8 would be at 1 15 and 2 etc so just like strings we start indexing at 0 if I wanted to access a specific element inside of the array I could just access it like this so referring to the name of the array inside of square brackets the index of the element that I want to access so in this case I'm accessing 0 which is going to be this for setting equal to 90 so down here if I print out lucky number 0 it's gonna be 90 since I modified it and if I pronounced 1 it's gonna be this 8 over here you can also create arrays a little bit differently so in this case I knew what values I wanted to put in here initially but if you don't know that you can just specify the array like this and not give it any information all I have to do here is just specify how many elements I want the array to be able to store that way C++ can allocate the appropriate amount of memory so if I just say int lucky numbers 6 now this we'll be able to hold six integers I can say lucky number zero and for example I could assign this the value of ninety so now I could technically just assign each of these values individually and I wouldn't have to populate the array up front you could also represent n dimensional arrays so these would be like a two dimensional or three or four dimensional array here I just have an example of a two dimensional array which is pretty common so it's the same thing as the other enrages and number grid I'm calling it this time and over here I'm specifying like the number of rows and the number of columns and then I'm giving this some initial information so here I have like my overall array inside of these open and close curly brackets but then I have two individual arrays as elements so the first element is an array and the second element is also an array and each of these arrays have three elements and that's reflected over here so this is basically saying that I have two elements inside of here and each of those array elements is going to have three elements and you can access them just like this so 0 1 is actually going to refer to the zeroth array and then the first element in that array so it's going to be that 2 and so down here I'm printing out 0 0 and 0 1 so I'm printing out 1 which is gonna be this element and then 0 1 which I modified to be 99 so it's gonna be 99 and that basic structure will carry forward for n dimensional array so if you want to have like 3 or 4 or 5 dimensional arrays it's gonna be essentially the same concept you can also if you don't know what information you want up front just declare it like this so you can just say number good to 3 and then again you could just populate those as you go maybe like from a looping structure or something like that right now I want to talk to you guys about another type of array it's actually called a vector and vectors are essentially just dynamic arrays so it's an array that allows us to do special things so I don't have to you know define how big this array is gonna be up front with a vector you can just kind of populate it as you go you can use all sorts of methods on it like push pop and stuff like that so over here we want to use these vectors you need to include it so I'm just doing hash tag include greater than less than vector and that'll allow us to use it I can create a vector just by saying vector and then inside of these open-closed greater than less sons we can define what type of data we want to store inside so in our case is going to be strings and I'm just calling this if I want I can add elements to this friends vector so I can just say friends dot pushback and that'll just push an element into the vector so you can see I'm pushing Oscar Angela and Kevin if you want you can also insert an element at a specific position so I could say friends dot insert and over here you always need to say friends dot begin and that's basically just gonna be like a pointer to the beginning of the array and then I could say friends stop again plus one so that'll insert at index position one so at an exhibition one I'm gonna insert the friend Jim and down here you'll see I'm printing this stuff out so I have like friends at zero so zero is gonna be Oscar which we get up here and then I'm printing out friends dot one which again we put Jim here so now Jim's gonna be at index position one and then friends out at two is gonna be Angela and if we printed out friends at three it would be Kevin and also I can print out the size which is four if you want you can also erase a friend so I could say like friends dot erase and again in here you just want to put an index position so I'm gonna start with friends dot begin and then I'm gonna add one to it so now this is gonna erase the friend at the next vision one so it's actually gonna erase Jim and now when I run this program again you'll see that it's Oscar Angela Kevin so Jim is completely gone so that's how we could erase a friend from the vector like I said vectors are just more useful it's a more dynamic structure than just a static array where you have to like rigorously define how many elements you want to hold inside of it now I want to talk to you guys about functions a function is basically just a container where we can store reusable code and then we can call it throughout our program I want to create a function in C++ it's really easy I have one up here you'll notice that we have our main function which again this is the function that gets executed when we run our C++ file but when I want to create a new one I can just go outside of here and I'm gonna go above it and I can just first thing is specify a return type so you can also save void if you don't want to return anything or you can specify any one of those data types and then we give it a name I'm calling this add numbers and I'm taking two parameters so an int num1 and then it ain't gnome two and then down here I'm just returning num1 plus num2 so this return keyword will break you out of the function although if you put void here you don't need that return keyword you can just kind of run the function and then down here I'm saying in some so I'm making an integer called sum and I'm setting it equal to the result of adding four and sixty so over here I'll see we get 64 when we print out some so this function is returning the value of adding those two numbers so over here I'm putting this function up above this main function but if I was to put this below here now what I have to do is I need to put a method stub up above so the main method isn't gonna know anything about the odd numbers method if I declare it below here so if I do that I need to basically just specify the method signature up above the main method and now I should be able to use it no problem but if I was to get rid of this then I would actually get a compiler error over here now let's take a look at conditionals so if statements if statements allow us to respond to different things in our programs and essentially just make decisions so here I have two boolean variables is student and is smart and they're both equal to false so what I could do is I could have my program respond to those variables using this if else block so I can say if and I'm basically checking two conditions so I can use this and to check two conditions or I could also use or like that which is just gonna be two bars so I'm saying if is student and is smart and then if both of these are true then we'll execute this code down here I can also use an else if so again now I'm checking if is student and I can use this negation operator which is that exclamation point and that'll just negate the condition that's next to it so if I say exclamation point is smart it's basically saying if they are a student and they're not smart and then also I just have this else down here which is pretty self-explanatory that'll just execute if neither of these conditions is true so that's how we can do that using boolean so you can also obtain boolean conditions by using comparisons so I can compare like if 1 is greater than 3 so obviously this is false but if I was to make this less than now when I run this program you'll see that that was true and it says the number comparison was true looks a guy at a typo here so the comparison operators are useful we have greater than less than greater than equal to less than equal to not equal to and double equals which is going to equal to you can also check to see if characters are equal to each other so I could say like two characters are equal or even greater than or less than and then if I wanted I could do the same thing with strings so I can create a string like my string set it equal to cat and I can use this method down here my string dot compare cat so this is going to compare it to cat and if I say not equal to zero that'll check to see that it's not true but if I said equals this will basically check to see if this string is equal to cat which now when we run our program you'll see it says string comparison was true so that's how you can compare strings you can also compare primitive data types like characters and numbers or you can just use straight-up boolean's as your conditions and that's gonna work as well all right now we'll look at another type of conditional which is a switch statement a switch statement is essentially just a special type of if statement where we're comparing one value to a bunch of different values so over here I have a character my grade and it's just equal to a and what I can do is I can make a switch statement and inside of the switch statement I can pass in that variable my grade and now what we can do is we can check that my grade variable against a bunch of different cases so I could say like in the case that my grade is equal to a I can just see out you pass so you can see over here since my grades equal to a we pass if I set this equal to F now this case over here is gonna be true so it'll say you fail and then this default is gonna cover every other situation so if none of these cases match up so I put like a Z here now it's gonna say that it's an invalid grade because we executed this default so also you need these break statements here on these cases so you can break out of the switch statement otherwise they'll just keep executing through the switch statement even after one of the cases matched now I'll move on to looping looping is a staple in any programming language and C++ has a while loop it's a very basic standard loop essentially just gonna loop through while a certain conditions true over here I have an integer index aside to equal to one and while index is less than or equal to five so as long as that conditions true we're gonna keep looping through and you'll see over here I just print out index and you can see I'm printing it out over here and then I'm incrementing index so index plus plus so that loops gonna keep looping until this conditions false and one thing you want to watch out for is infinite loops so I was to get rid of this index plus plus now this is gonna end up being an infinite loop because this condition up here will never be false in addition to a while loop we can also define a do-while loop so do while loops pretty similar the only difference is that in a do-while loop you execute the loop body and then you check the condition so even if I had index up here equal to 6 I'll still be able to execute one iteration of this do-while loop because it executes the code before it checks the condition whereas in a normal while loop it checks the condition and then it executes the code in addition to a while loop we can also use a for loop for loop is it's essentially a while loop but it's just a lot more convenient and a for loop will allow us to keep track of a variable as we go through the structure so over here inside of this for loops parentheses I can specify three things first thing is a variable so I can instantiate a variable so I'm just saying in I is equal to 0 and then over here we have our loop guard which is the loop condition so I'm gonna keep looping as long as I is less than 5 and then over here this is a line of code that will get executed after every iteration of the loop so I'm just incrementing I and then down here you can see I'm just printing it out so starting at 0 it's we go through the loop five times printing out 0 through 4 and again we're gonna loop as long as this condition over here is true so that's a for loop but not all allow you to keep track of like I said a variable as you go through the loop so now let's talk about exception caching so a lot of times in your C++ programs certain circumstances will arrive which will throw exceptions thus terminating the execution of your program and in a lot of cases you're gonna want to be able to handle those and allow your program to keep executing so down here we can do basic exception caching I can use try catch block so I can say try and then open and close curly bracket and inside of here I can put any code that might break my program so let's say I wanted to enforce a rule where you can't divide numbers by 0 so down here inside of this try block I have this division function and then up here I defined the division function basically takes in two parameters and if B is equal to 0 so in other words if you're trying to divide a number by 0 then over here I'm throwing an exception so basically it's saying division by 0 error or I guess I could say exception but basically you can throw exception which will you know result in your program terminating and that's you know essentially just like something went wrong but otherwise this division function will just return a divided by B so in this case I'm trying to divide 10 by 0 which can't happen so this division function is going to throw an exception which this try block will catch and then down here in the catch block again you can just pass as a parameter so we can just define a parameter like this I'm calling it message and down here this will print out whatever message got thrown so when I run my program you see it prints out division by 0 error so that's basically how you can catch exceptions in C++ alright so now let's talk about object orientation C++ is great because it's an operand programming language we can do a lot of object-oriented programming stuff inside of it so up here I actually created a class and I can create this class right alongside this main function or what a lot of people will do is they'll put classes inside of their own files but for our case this is just easier to see so I have a class book and what you can do is you can define some public attributes so public basically means that all this stuff down here is gonna be accessible to all the other code in my program and later we'll talk about private which is kind of the opposite so public and then inside of here I can define some attributes so I can say like string title string author right those are two things you might define about a book and then down here I can define a function read book and you'll see this read book function is basically just printing out reading this title by this author and when I use this keyword and then I make this little arrow what that's doing is it's gonna access the title or the author for the object for the specific object that's calling this read book function so I might have like a couple objects in my program and if each of them call this it'll access a different title and a different author and yeah a second so down here I can just say book book 1 and this will allow me to create a book object and then I can assign the book object certain attribute so I could say like book 1 title is Harry Potter book 1 dot author is JK Rowling and then if I wanted I could come down here and execute that function so I could say like book 1 read book and over here it says reading Harry Potter by JK Rowling so because I set the title and the author when this read book function up here said this arrow title it accessed Harry Potter and then accessed JK Rowling because I was calling that function on this book one object and then also if I wanted I could just print out like the title so I could say book one title and that'll give me Harry Potter so that's sort of basic classes and objects and how they work alright now I want to talk to you guys about constructors so a constructor is a special function which gets called when you create an instance of a class whenever I create a book object for example the constructor will get called and I can use that constructor to assign initial values to the object so up here in my book class I added this book constructor and you basically create it just by saying the name of the class and then again you can put over here some parameters and you can define multiple constructors in this case I just have one but this constructor is gonna take in a title and an author and then over here I can say this title and this author is equal to the title that got passed in and the author that got passed in so that's pretty standard constructor stuff and then down here when I create an instance of the book class when I create a book object I can just say book book one and then inside of parentheses here I can just pass in the title and the author and that makes it a lot easier for us to create these objects and then down here I can just print out like book one title and we're printing out Harry Potter so instead of having to individually assign book title and book author I can do it using the constructor another thing we can do is create getters and setters so a lot of times in C++ when you're building classes you're gonna want to control the access to the attributes of those classes and you can use getters and setters so I have an example of that up here now you'll notice over here I have this private keyword and then a colon and then down below here I just have string title and string author basically when I use that private keyword what that means is that anything under here is set to private so that means that only code inside of the book class can access the title and the author so if I was outside of the book class I wouldn't be able to access the title in the author because these are under this private block and then over here because these are private I can't access them down here like if I was gonna create a object so what I can do is I can create accessors so I can create a get title method and a set title method and the get title method just returns the title and the set title method just sets the title I can do the same for the author and then over here in the constructor when I'm setting them I'm using those setter methods as well so that's basically how you can use getters and setters and then over here I could say like book 1 get title so I can't access the title directly anymore I have to go through the getter and if I wanted to set it I have to go through the setter so that's how you can tighten up your classes with getters and setters alright now I want to show you guys inheritance so inheritance is a special thing we can do in object-oriented programming where a class can actually inherit functionality and attributes from another class so up here I have two classes that I created I have this chef class so over here it just says class chef and this chef class has three public functions it has a make chicken function has a make salad function to make special dish function and it basically just you know prints out what the chef is doing and then down here I have another class this Italian chef class and this Italian chef class actually is inheriting from the chef class so over here I just have this colon and then I say public and then the name of the class that I want to inherit from so down here the Italian chef actually gave it another function which is make pasta so the Italian chef can make pasta and the Italian chef is also overriding the make special dish function from its superclass which is that Chef so down here I have an example I have I'm creating a chef object and I'm saying my chef make chicken so the chef is gonna make chicken put on the Italian shop over here I'm creating one down here can also make chicken so both of these can make chicken even though the Italian chef didn't define a make chicken function inside of here because it's inheriting from the chef class it's able to make chicken and then also I overrode the special dish function so if I said my chef makes special dish and then I made the Italian chef make the special dish and I ran the program these are both gonna make different special dishes so the chef is just making a special dish and the Italian chef is making chicken parm because I overrode it so that's basically how we can use inheritance in C++ another thing I want to talk to you guys about with inheritance which is using instructors in subclasses and super classes so over here in the chef class you'll notice that I actually have two attributes now so I have a name and I also have age and then down here I have my constructor or my initialize function and basically it's taken in the name and the age and its setting both of them so the chef the chef class and chef objects can have a name and they can also have an age down here in the Italian chef class which again is inheriting from the chef class I added another attribute which is country of origin so down here I have my own constructor for the Italian chef class you see over here how this constructor is set up so it's just Italian chef it's taking in a name and age and also a country of origin and over here what I'm doing is I'm saying : chef and basically what this is doing is it's calling the constructor for the superclass so this is calling the constructor that was defined up in the chef class or the superclass and I'm passing in name and age so what this is gonna do is it's gonna take care of setting up the name and the age using like I said the superclass constructor and then down here I'm setting the country of origin which is the attribute that I added in the Italian chef class so this is pretty common and it's a pretty common use case with inheritance and then so down here you'll notice that I have my chef's object and I just passed in Gordon Ramsay and 50 so the chef's name and age and down here I have my Italian chef object pass in a name and age and then also the country of origin which in our case for this chef is going to be Italy and then down here I printed out my Italian chef age just to kind of prove that everything worked si si over here we're printing out 55 so that's how you can override essentially override the superclass constructor in the subclass and then you can also call the superclass constructor for example to initialize name and age all right one show you guys one more thing with inheritance which is abstract attributes so actually in C++ we call them virtual attributes but the basic idea is that you can define attributes or functions which are virtual which basically means that the superclass isn't to define them it's gonna be up to the subclasses to define them so essentially everything has to be overwritten by the subclasses so down here I have this vehicle class and inside the public block I have virtual void move so I'm defining a essentially just a function stub but I'm saying that it's a virtual which means that any classes that inherit this move function need to implement it and I'm setting it equal to zero and this is basically how you could set up like a virtual function and then down here I have just a normal function which could get inherited this isn't virtual so down here I have subclasses I have a bicycle subclass in a plane subclass so the bicycle subclass you'll see is implementing this move function that was defined up here so it's implementing it on its own and then the plane subclass is doing the same thing so it's implementing that move function and in order for these to be valid subclasses of vehicle they need to implement the move function that's what that virtual key word is specifying so down here you see I'm creating a plane object and then I'm saying my plane move and then my plane get description so when it says my plane move it's the plane flies through the sky but when it says my plane get description it just says vehicles are used for transportation which is what was defined up here inside of this get description function so that's how we can use virtuals like I said in other languages a lot of times people will call these abstract functions but that's how we can do it in C++ so that's sort of a basic overview of a lot of the core you know things that you want to do in C++ obviously I didn't cover everything but hopefully this gives you a good idea of the syntax of C++ comp how things work some of the best practices it kind of gets your foot into the door and gets you a little bit more comfortable take some of the mystery away from the C++ programming language again I will just say one note on this which is because C++ is technically like it was forked from a pre standardized version of C a lot of times what developers will do is they'll use C and C++ interchangeably inside of their C++ programs and one of the things you want to do is make sure that when you're using C++ you actually need it and you're using everything for the language because otherwise you just want to use the C programming language like what you don't want is some faster program that has C and C++ and it's kind of not doing things in a C++ manner but also not doing things in a C manner like you want to make sure that if you're using C++ that you're using C++ and you're not just using C inside of your C++ program hey thanks for watching if you enjoyed the video please leave a like and subscribe to drop Academy to be the first to know when we release new content also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below finally if you're enjoying traffic Academy and you want to help us grow head over to draft Adam EECOM forward slash contribute and invest in our future
Info
Channel: Mike Dane
Views: 47,777
Rating: 4.9053254 out of 5
Keywords: Programming
Id: raZSmcariyU
Channel Id: undefined
Length: 40min 26sec (2426 seconds)
Published: Tue Dec 05 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.