hi everyone and welcome my name is saldina i am a software engineer and i make programming related videos on code beauty youtube channel and in this video i want to talk about oop so object oriented programming this course will start on a beginner basic level and then as the course progresses we will see some of the more advanced concepts i will explain those advanced concepts and i will show you how you can implement all of those concepts in code you can find the full content of this course in the description of this video so make sure to check that out and then if you want to see some different examples if you want to learn more c plus and you want to upgrade your knowledge i have a lot more videos like this one on my channel so i invite you to visit code beauty youtube channel and i'm going to see you there in some other video so let's start with this video so the first thing that i want to explain is what is oop what is object oriented programming oop is programming paradigm which means that it is a set of rules and ideas and concepts it is basically a standard in programming that we use to solve a specific type of problem now besides object-oriented paradigm there are other programming paradigms as well you may ask why well we as humans have a lot of different types of problems and our computers help us solve those different types of problems and if one paradigm is good to solve one type of problem for example that does not necessarily mean that it is good to solve all other types of problems that we have so the idea behind object-oriented paradigm is we want to be able to represent real-life objects real-life entities together with their characteristics with their attributes and their behaviors we want to be able to explain those objects to our computer and to represent those objects in our program and that is exactly what oop is used for so how does this work on a real life example let's use the example of car so a car as an entity in real life has many different attributes many different characteristics and some of those are manufacturer of that car so is that car a volvo a ferrari or a ford or some other type of car and then that car certainly has a collar it has price it has max speed that that car can obtain and many more characteristics i'm sure that you can think of many more characteristics of a car than i can do so those would be some of the attributes and characteristics of a car in real life and then some of the behaviors of a car are for example a car can drive a car can accelerate it can uh stop you can open the door of a car you can tank your car so you can put fuel in your car and then you should also have the option to show how much fuel that car has left so those would be some behaviors that car has and as i said object oriented programming the idea of object oriented programming is to be able to represent that car from real life in your program so how do we do that how can i represent a real life entity like a car in my program or another entity like a game or a sport or student or a book or animal you know so the answer to this question is by using classes now class is very important concept in oop class is building block of object oriented programming and um a class is basically user user-defined data type and in order to be able to understand what are user-defined data types you first need to understand what are pre-defined data types what are built-in data types so data types like integer and float and double and character and so on and if you are not familiar with these i am going to refer you to my video that i made on my channel related to variables and data types so just search code beauty variables data types and you should be able to find that video and then if you are familiar with these predefined data types i'm going to tell you that a class is a little bit more complex data type and in what way well let's say for example that in your program you want to represent username of your user in order to be able to do that you will just create a variable of type string and call it username and you should be able to store the username of your user in that variable but what happens if you want to store the entire user in your program that user has many more characteristics than just username user has his name as well and then gender and email address and then he has height and width and um i'm sorry wait he has weight and then he also has some other characteristics and all of these characteristics are going to be variables that have its own type and then all of these variables together are going to make one group which will be a class that represents your user one thing to keep in mind though is that you will need to store the information that is important for your specific program for example if we are talking about facebook user then height and weight of that user is not that important but if you are building a fitness application then height and weight of your user is extremely important so you should store the information that is important for your specific program so let me show you now how we can build a class in c plus code and the ide that i'm using here is called visual studio and you can download community version of visual studio for free in case that you want to follow this tutorial and type at the same time together with me so let me show you now how you can create a class in c code the class that i want to create is called employee so let's do that now let's create an employee class in this program here so the first thing is you type class keyword and then you give a name to your class so i'm going to call my employee like this and then you use these curly brackets and at the end of these curly brackets you need to put semicolon because if you don't if you forget this semicolon you will get a compile time error and if i try to run this program now that is not going to be possible as you can see here and it says here immediately it says expected a semi column so you need to put semicolon here okay now that we have created this employee class we see that this class is empty and what we need to put inside this class here so inside these curly brackets because that is the body of our class we need to put members of this class now the members of this class are going to be its attributes and its behaviors so what kind of attributes an employee has well an employee definitely has a name so let's put that in so i'm going to say std string and then name like this okay and then employee definitely has a company that he works for so let's add that as well i'm going to say std string company like this okay and i want to move this uh string i'm going to include it here so i'm going to say using std string so that i don't need to type it every time that i'm using string okay and now i can remove this now it looks much more readable okay so so far we have added a name and a company for our employee and let's add one more thing let's say that every employee has age so inch age we are going to we are going to represent age as an integer number so as you can see at the end of each of these attributes you put semicolon as well okay so far our employee has three attributes name company and age and you can add many more attributes here if you want you can add his title you can add his email you can add employment date his birth date uh and so many more but i'm not going to do that in this situation because i don't want to add a bunch of code that we will not be using because i'm going to add that code as we need it later in the program so this class here does not represent data this class here represents a blueprint so whenever you want to create an employee this class here will serve as a model for that employee so this class here will say hey your employee needs to have a name a company name and age so how do you create an instance of this class how do you create an object of this class let's show that now so how do you create a variable of type int for example well you type data type first so int and then you give the name to that variable so let's call it number okay and with this we have successfully created a variable of type int and it is going to be the same with the variable of type employee so you first specify the name of that type and the name is going to be employee so this here is user defined type which is called employee okay and then how do i want to name this variable let's call it employee 1 like this and with this we have successfully created a variable of type employee this here is going to be an object of this class here so let's remove this because we don't need it anymore it was just for demonstration purposes so how do you access now these attributes how do you access these members of employee class well the approach would be the following you type the name of that object like this and then if i put dot here we should be able to see all of these members here but that is not happening so why is that the case well there is one rule that says that everything inside class in c plus plus is private by default which means that all of these members here are private and in order to be able to understand this and fix the problem that we have i need to tell you a little story about access modifiers so in c plus plus we have three access modifiers we have private we have public and then we have protected and let's explain now what each of these mean well private means that whatever is private in your class that is not going to be accessible outside of your class that is basically going to be hidden and that public means that whatever is public in your class you will be able to access that outside of your class as well so anyone else outside of your class is going to be able to access public members of your class and then protect it is somewhere in between private and public and it has certain rules to it so we will be talking more about protected access modifier when we start talking about inheritance later in this course so for now you need to remember that there are three of them there is private there is public and protected so in this particular situation one rule is that everything inside your class is private by default so this situation here is the same as if we wrote it like this okay this is just explicit way to say hey everything beneath this private access modifier is going to be private so this situation is the same as this situation now let's return our private access modifier and let's see how we can fix the problem that we have and the problem is that we cannot access these members of our employee class and we want to do that so let's try to change this private access modifier with a different access modifier let's try protected for example well if i try to access these members name company and age now if i press dot as you can see nothing is happening so i cannot access these name company names if i try to type for example name i get an error which says member employee name is inaccessible so protected is not helping as well so let's try the third one let's try public okay and now as you can see the error has disappeared and if i try to do this once more we have all of these members name company name and age we have all of them here they are offered to us so that means that by changing this access modifier to public we will be able to see all of these properties here so let's now set the values of these members let's say that the name of our employee will be for example saldina that's my name okay and then let's copy this two more times like this so the company will be let's say for example youtube and then code beauty which is my channel let's say that that is the company that i work for and then um age okay age is an integer variable so we are going to set it as an integer and age is going to be 25. okay so with this here we have created an object of type of type employee that object is called employee 1 and here we have set values for the properties we have set values for name company and age of this employee object here but we also said that we can describe behaviors as well so how can we describe a behavior of an employee let's first think of a behavior that an employee has let's say for example that he can introduce himself so he comes to work and he says hello my name is so and so i work for this company and i am 25 years old so how can we describe that behavior in this class here well we can describe that with a class method and what a class method is it is basically a function so we are going to create a function inside this class employee so let's do that i'm going to create a function of return type void and i'm going to call that function introduce yourself like this okay and then inside this function what i want to do is you can write hello my name is and then you put this name and then i work for this company and i am 20 years old for example but i'm going to make it a little bit more formal so i'm going to say the following c out and then i'm going to write out name like this okay and then let's put name and i'm going to add endline and then i'm going to write out these two informations as well so company and then paste here company and then age as well okay now how can i invoke this introduce yourself function well the same way as we did here i'm going to say employee and then i put dot and as you can see this introduce yourself function is available here so let's invoke that function like this and now we should be able to test this so if i run my program okay as you can see here it says name salina company youtube code beauty and then age 25 okay great and now if we needed to introduce our employee five times for example we just copy this function five times like this and our user is going to introduce himself five times and if we didn't have this class method here we would have to copy this code five times so each time that we want to introduce our user we would have to copy this code here instead of doing that we can create a class method which represents a behavior and then we can invoke that class method whenever we need it and if i run this program this user should introduce himself five times okay two three four and then oh sorry four and then five okay let's now delete this because i don't need it anymore because i want to show you something else so let's say that we want to create another employee in our program so how would we do that well we would use the same approach that we had here so employee the name of my class and then employee let's call it employee 2 which is going to be the name of the object so let's assign the values for these attributes for our second employee as well i'm going to say employee two like this and his name is going to be john for example like this let's copy this two more times so company will be amazon for example john works for amazon and he has uh let's say 35 years like this okay so now if i want to introduce john i'm going to say employee 2 dot introduce yourself excellent so if i run this program now as you can see here is me salina youtube code beauty and then here is john john amazon35 okay now one problem that i already see here is this part of the code and then this part of the code what would happen if we wanted to create 10 more users or 100 more users we would have to repeat this for every single user that we create and that is not really optimal because there is a better way there is a better approach of constructing our objects and in order for you to be able to understand this i will have to tell you a story about constructors so now you may wonder salvino what is a constructor well a constructor is a special type of method that is invoked each time that an object of a class is created so whenever you create an object of a class a constructor is invoked so does that mean that here and here as well a constructor is invoked the answer to that question is yes now you may wonder okay we have not created any constructor you must be lying well let me demonstrate you something let's comment this code here and now let's comment this code here as well and let's see what will happen if i run my program so we are not assigning any values to the attributes of this employee nor this employee here so if i run my program okay here is what happens as you can see this is our first user and this here is our second user so here we don't have anything and then here we here we also don't have anything and then here is a number which i am not going to try to read okay but basically this here is the work of default constructor so what is default constructor default constructor is a term to describe a constructor that is automatically generated by your compiler so in case that you don't create a constructor of your own your compiler is going to automatically give you a constructor which is called default constructor okay now let's stop this and let's see how we can create our own constructor because those values that you saw that is something that we cannot work with i mean we don't want to work with those values we want to use our own values we want to use this and this and then this here and for our second user we want to use these values here so how can you create a constructor of your own there are a few rules when it comes to creating constructors there are three rules actually so the first rule is following we already said that a constructor is just a method but unlike other methods a constructor does not have a return type so let's scroll this up and as you can see this method here introduce yourself this method has a return type of void which is nothing but we still have to specify that but a constructor will not have this at all so that's going to be the first rule the second rule is that a constructor has the same name as the class that it belongs to so the constructor of class employee will be called employee so that is the second rule the third rule is that constructor must be public now this i'm seeing as a rule at this level of knowledge because a constructor does not necessarily need to be public always there are certain situations specific situations when you would want to make your constructor private but at this level of knowledge i would advise you to make sure that your constructors are public because if you remember when we talked about access modifiers and when we said what private means that means that everything that is private is locked is hidden inside your class and you most definitely don't want to make your constructor hidden so uh third three rules uh the first one is that constructor does not have return type the second one is that constructor has the same name as the class so let's create constructor of this class here it will be called employee and i am going to put that constructor in this public area because if i decided for some reason to put this constructor if i decided to make this private let's do let's make this private like this as you can see immediately here we get an error so our employee one and then our employees too this error says employee constructor is inaccessible and here as well because we have made this constructor private and we cannot access it outside of this class here and we do not want to do that we want our constructor to be public so i'm going to remove this private access modifier and as you can see this error here has disappeared now so the job of this constructor here is going to be to construct the object of employee that means that whenever we create employee we want to pass these three values here so name company and age we want to pass that to our constructor here so we are going to receive that in our employee constructor as parameters so i'm going to say string name and then string company like this and int age okay now that we have received these three parameters here what i want to do with them is i want to assign them to these three properties here so i'm going to say name of my employee will be this name here like this and then company that our employee works in it will be this value here so company and then age like this will be whatever we passed as this parameter here okay and now if you look at our employee here we have an error again so let's check what their what that error is it says no default constructor exists for class employee so what this error here means it means that when we decided to create our own constructor at that moment we lost the default constructor that was automatically generated for us so when you decide to create your own constructor you are going to lose that default constructor and you can fix that by either making your own default constructor or by providing here these three values that we have specified that our constructor is going to receive so let's do that let's provide here name company name and age so how do we do that well let me show you how we can use this constructor here so in this line i'm going to say that my employee 1 is going to be equal to and i'm going to invoke this constructor basically so i'm going to say hey i am invoking this constructor here and here it expects to receive argument list what is argument list it is these three parameters so name company and age so here i'm going to pass name which is salvina and then company is going to be this company here so youtube code beauty i'm going to copy that and then age is going to be 25 okay so with this we have successfully constructed our employee one object which means that i can delete this code here and then if we do this same for our employee tool we will be able to delete this code here as well so let's do that i'm going to say employee and then here i want to pass these three values so name is john okay and then company is amazon like this and then age is 35. excellent so i am able to delete this as well and let's check what is going to happen if i run my program i'm just going to format this so if i run my program nothing should change i should still be able to introduce this user and then this user as well so let's do that let's run our program okay and as you can see selena youtube code beauty and then 25 john amazon 35. so now we have managed to reduce the code that we had in our main function to only two lines per employee so the first line is to construct that object and then second line is to introduce that user and we have managed to do that with our employee constructor which is now doing that work of constructing the object based on based on the parameters that we passed to that constructor that constructor receives those parameters here and then it constructs the object here it initializes the values of that object and then in our introduce yourself function whatever we invoke that function for an object of an employee that function is going to do this code here so it is going to basically introduce that employee so i hope that this part so far was understandable for you because here i'm going to take a quick break and i'm going to pause the video and then i might be back in a couple of minutes or in a couple of hours or even in a couple of days but that is going to be just a couple of seconds for you and i want to make a transition that i never did on my channel i never did this on code beauty but i'm going to do it here and that is this transition here hello and welcome back and let's continue talking about object oriented programming but before i continue i want to make a quick summary of the things that we have learned so far in this course so first i have explained what is object-oriented programming and what is the main idea behind object-oriented paradigm and then i have explained what our classes and what our objects and how you can use those classes and objects in order to represent real life entities together with their attributes and their methods in your programs describe them to your computer and then we also talked about access modifiers so private public and protected and then we have seen what our constructors and how you can create and use constructors and now the time has come to talk about four most important principles four pillars for most important concepts of object-oriented programming and those are encapsulation abstraction inheritance and polymorphism so the first one that i want to talk about is encapsulation so what is encapsulation the idea of encapsulation is idea of bundling or tying together data and methods that operate on that data so that they are grouped together within a class and why do we do this well we do this with the purpose of preventing anyone or anything outside of our class to be able to directly access our data and to interact with it and to modify it so i am not saying that we don't want anyone to access our data at all i'm just saying that i don't want that to happen directly i don't want that other class to be able to directly modify and change and interact with my data because i want to provide my own way for that to happen so how do i do that how do i provide a way for other classes to interact with the properties of my class well i provide very specific public functions that that other class can invoke and in that way interact with my data so again how do you access encapsulated properties of a class the answer is through its methods and these methods are very often implemented as getters and setters so now i'm going to show you in code how those getters and setters look like and how we can modify this program that i have here so that we obey that rule of encapsulation so the first thing that i want to do here in this program is i want to make these three properties i want to make them private so i want to encapsulate these three properties hide them within this class so let's do that here i'm going to put private modifier so this here is private access modifier and we don't actually have to write this because everything that does not have a modifier at all in c plus plus is private by default within a class but i just find it more readable if i do it this way so now here in this private area i am going to move these three properties okay and then paste them here like this and now we have hidden these three properties within this class called employee which means that now we will not be able to access these three properties anymore outside of our class let me just collapse this and let me prove you what i just said okay so if i try to access name or company or age on my employee object i should not be able to do that now so if i type employee 1 for example and then i type this dot you can see that the only thing that we can access is this introduce your introduce yourself method which is this one here and that is the only thing that is public besides this constructor here so how can we access these properties then well as i said we have to expose our own methods which are going to be public and then by using those methods other classes and other users will be able to interact with this data here with these three properties so let's create getters and setters for these three properties let's first do it for for our name property so i'm going to do it here uh let's say void set name and then what this method what this function is going to receive well that is going to be a string name okay string like this and then once this function receives this parameter called name what i want to do with this is i want to assign this value to this property here so i'm going to say name is equal to name okay and then i want to create so this here is a setter and then i want to create a getter method as well so i am going to say string get name like this and then here i'm just going to say return name okay now this method here receives one parameter which is called name and then it sets the value of our property which is encapsulated which is this one here to that value that we have received in this setter and then this method here get name that method is going to return the value of our name property that is also encapsulated and that value is going to be returned to whoever invokes this method and because these two methods are public anyone outside of our class is going to be able to invoke set name and get name so these are examples of setter and getter and now i'm going to do the same for our company property and age property and i will be back to show you that code in a moment so i have created getters and setters for our name property here so this is sether this is getter for our name property and then here is a setter for our company and then getter for company property and then here are setter and getter methods for age and now because these methods here are public everyone outside of our class should be able to access these methods whereas these three properties are now encapsulated which means that they are private they are hidden within this class and no one else besides members of this class here are going to be able to access them directly so let's test now these getters and setters in our main function okay so let's say that employee 1 and let's say set age and i'm going to say for example that my employee 1 now has let's say 39 years for example okay and in order to test these gutters i'm going to say for example std c out and then let's say employee 1 dot get name okay so our getter should work like this and then let's say employee one is and i'm going to test if this value has been applied successfully so i'm going to say employee one oh what is this employee one dot get age okay and here i want to say employee one is 39 years old like this okay and now if i run this program we should get the result of this line here and we should be able to see if our getters and setters work as they should so let's run our program okay and now this last line of code here says salina is 39 years old okay so i shouldn't have done this for myself i should have done this for john for example so i'm going to close this program and as you could see these get name and get age and also get company and then set their methods as well are now public whereas the properties that they are hiding are hidden within the class and they are encapsulated so the only way to access these three properties is going to be using these methods now what we can do with these methods is we can provide special rules to interact with this data here for example let's say that employee cannot be anyone who is not older than 18 years old so here when actually here when i'm trying to set the age of our employee i'm going to add a check so i'm going to add a validation rule and i will say if age age this age that i have received in my setter is greater than 18 years old or actually greater or equal to than 18 because person that has 18 years old is of age only in the case that this property here is greater than 18 years old only then i am going to assign that to my age property here in the case that our user anyone else decides to assign value which is less than 18 so 17 or 15 or 10 or whatever i am not going to apply those changes so let's test this also here instead of setting age to 39 i'm going to say that salina now is 15 so let's test those changes okay and now as you can see it says selena is 25 years old why because this set age method here says that only valid age is 18 or greater than 18 and only in that case those changes are going to be applied okay so you can apply certain validation rules to your setter methods and then i'm going to leave to you to apply certain validation rules to your set company name and then set employee name this property here so you can do that as a homework you can decide if you want to allow only letters or you want to allow also numbers and special characters as the name for your employee and then as the name for your company okay so once more the idea of encapsulation is to make this let me close this so the idea of encapsulation is to make these properties private and then whoever wants to access these properties outside of this class will have to go through the methods that you expose that do have access to your private properties okay so let's collapse this and here are those six getters and setters for these three properties here the second principle that i want to talk about is called abstraction what is abstraction abstraction means hiding complex things behind a procedure that makes those things look simple so in order to explain this let's use an everyday life example let's use the example of your smartphone so one of the main characteristics that smartphones have these days is that they can take pictures so how does that procedure of taking a picture looks like well it's pretty simple at least on your side you just press a button and you have taken a picture where you press a button and then you make a call or you send a message but that is not really that simple because for you it is just a button click but for the company that makes those smartphones there is much more complex logic that they need to implement in order for you to be able to press a button and then take a picture so there is some sort of interface between you and then that company that produces those smartphones so on your side everything is pretty simple and easy you just press a button and you make a call you press a button and you send a message but then all of that complexity behind those functionalities is hidden from you and that complexity is on the side of the company that produces those smartphones now all of this complexity that is hidden from you you don't actually need to know anything about this in order to be able to use this site here in order to be able to make calls and send messages and take pictures so what happens if another company comes and they decide that they want to produce smartphones as well what they have to do is they have to sign this contract here they have to provide you as a user with this nice and beautiful and clean and simple interface so that you are able to press a button and take a picture and then they have to take care on their side to implement all of that complex logic which is hidden from you because if they decide to show you that ugly side that complex side chances are that no one is going to want to use their smartphones at all and even if someone decides that they do want to use their smartphones they will probably not understand this complex logic well so that they will so they will make mistakes and they will use those smartphones in wrong ways so this process of hiding that complexity from you as a user is called abstraction and in that way this very very complex system is hidden and this system here is represented as very simple system by this contract so let's see how we can implement this contract here that makes one side look simple and then other side very complex let's implement that on the code and example that we have here in our visual studio so let's take a look at this employee class here what kind of functionality complex functionality can we implement on this class here let's say for example that every employee can ask for promotion so he goes to his boss and he says hey boss can i get a promotion and then his boss needs to go through this very complex thought process in order to decide if this person deserves a promotion or not so his boss needs to consider many things he needs to consider consider for example how long this person has been working for the company how much knowledge this person has and then what kind of relationship this person has with their colleagues how much this person is contributing to the company um and then is this person always late for work are they breaking deadlines and many many more things so how about abstracting that functionality and in this situation we are doing that for another developer that is going to use our class so here in this situation we are the ones who are making this class so we are producing that smartphone so we are the ones who should provide that complex logic for those functionalities that are complex and then we should provide that simple and basic interface for anyone who wants to use this class here so how do you do that how do you create that contract well the answer is by using abstract classes for those of you who are coming from c sharp or java world there is already this concept of interface that some of you might be familiar with and in c plus plus you can simulate behavior of interface by using abstract class so let's create an abstract class so here on the top i am going to create a class and let's create a class called abstract employee like this or you can call it i employee if you want so this class here is going to serve as a contract and this contract will have only one rule and that rule is that whichever class decides to sign this contract that class will need to provide implementation for a method called ask for promotion so a method of return type void called ask for promotion like this and we are also going to make this obligatory which means that we will force any class that signs this contract here to implement this method here so how can we make this obligatory the answer is by making this function here a pure virtual function so i'm going to say here virtual and then here i'm going to say is equal to zero and now this class here has become an abstract class and this method here this function is pure virtual function or an abstract function and this means that whoever decides to sign this contract called that abstractemployee.com that signs this contract will have to provide implementation for this method here so now the question is how can my class my employee class sign this contract here the answer is pretty simple you add here column sign and then you specify the name of your contract which is abstract employee like this okay and now my class has successfully signed this contract and immediately if you could notice these two red dots that appeared below if we hover over these errors because these are errors you can see that it says pure virtual function abstract employee ask for promotion has no overrider which means hey you signed this contract here but you are not obeying the rules of that contract you are not providing implementation for this function here so in order to avoid these errors and fix these errors that we have now what we have to do is we have to provide implementation for this method here so let's do that okay i am going to copy it here and then i will come here and paste it and here in these curly brackets we are going to implement that method but as you could see immediately the errors that we had below have disappeared so here inside this class i want to provide logic for ask for promotion method so once more we said that that logic is pretty complex and that we should take in consideration many things so things like how long that person has been working for a company how much that person is contributing to the company what kind of relationship they have with their colleagues and so on but considering that we don't have all of that information what i'm going to do is i'm going to make it a little bit more simple so i'm going to say for example any employee that is older than 30 years old can get a promotion for example so i'm going to say here if age is greater than 30. i will do the following i will say std see out and then let's write the name of this employee i'm going to say name and i'm going to say got promoted like this okay excellent and then in the else case so in the case that person has less than 30 years i'm going to write something else so i'm going to deny that promotion to the person who is younger than 30. so i'm going to say name and then let's write out sorry no promotion for you okay excellent and now let's test this method here so i'm going to go to my main function and i will delete all of this code here okay and now i want to test this method here on my employee 1 and employee 2 so that i see if any of them can get promotion so let's say employee one dot ask for promotion and then employee two dot ask for promotion as well and if i run my program it says salinas sorry no promotion for you and then john got promoted and why that happened because let me close this because salina is 25 and then john is 35 and this rule here says that whoever is older than 30 which is john he gets promoted and then if that person is younger than 30 that person is not going to be promoted okay so with this we have accomplished the following we have implemented a contract which is actually an abstract class and that abstract class or that contract has only one rule and that rule is this pure virtual function here which is called ask for promotion so that means that whichever class signs this contract whichever class inherits from this abstract employee which is this class here that class will have to provide implementation for this method here so ask for promotion so in this situation our employee is inheriting from our abstract employees so our employee class is signing that contract which means that that class needs to provide implementation for the method that is in that contract okay now what this allows us to do when another developer comes to our code and wants to use our employee class that developer will see this contract here and he will say oh so this employee class has method which is called ask for promotion so that means that if i use employee class i can invoke this method here and i don't need to worry about complexity of this method i don't need to worry about how this promotion is given or rejected because that is the worry of the person who implemented this class here so that is the worry of the person who is going to sign this contract and in this situation we are that person we are the person who is producing that smartphone we are the person who is writing this class and this class is signing the contract which means that we have to provide implementation for this method here as we did in this situation and we tested it and as you could see i didn't get promotion and john did so that is the idea of abstraction and this ask for promotion method is just that button that we mentioned a button on your smartphone from the beginning of this chapter the third principle of object oriented programming that i want to talk about is called inheritance the idea of inheritance is the following so there is this base class also known as super class or parent class and then there is derived class also known as child class or subclass now this base class here this parent class has certain attributes and behaviors it has members and then if this class here decides to inherit from the base class at that moment this class becomes a child class and by that this class is going to obtain all of the members of this base class here which means this class is going to have all the same attributes and behaviors as its base class and then this derived class can also have its own members that are specific for that class only which this class here does not have so in order to explain this let's use an example that is going to be familiar to you let's let's return to the example of a car that i used in the beginning of this video so a car as a class is going to have certain attributes for example a name a model price a color and so on and then it is going to have behaviors as well for example it has a method called drive now what kind of derived classes can we create from this base class what kind of classes are more specific so what kind of car types are more specific than just car let's say for example that we are going to have one derived class that is called electric car and then another derived class which is called conventional car or gas car now this electric car is also going to have all the same properties that a car has so it is going to have um it is going to have a name a model a color a price and then it is going to have a method called drive and then this conventional car is also going to have all those same properties but they are going to be different between them this car for example is going to have a method called charge because this is electric car and we need to use electricity to charge it and then it is going to have an attribute called for example battery status and then this car which is gas car conventional car is going to have a method called think which means a method of putting fuel in that car and then it is going to have a property an attribute which is named tank status which shows you how much how much gas do you have left in your tank so each one of these derived classes is going to have their specific attributes but they will also inherit all of those attributes that their base class has so now let's see how we can implement that on the example that we have here so here i have class called employee and what i want to do is i want to create a derived class for this class here so what kind of class can inherit from employee class logically what is more specific type of employee let's say for example that we want to create a class called developer so let's do that here i'm going to say class and then let's call this class developer like this okay so with this we have created a class called developer and now what i want to do is i want to make this class inherit from this class here so how do you do that the answer is pretty simple you put column sign here and then you specify the name of your base class and the name is employee so with this a few things happened this class here developer became a child class which means a subclass or divide or derived class and then this class here employee is now called base class or super class or parent class and also another thing that happened is that this developer class here now has all of these properties and then these ones here as well that our employee class has so developer has all of the properties that employee has and then let's create one more property that is going to be specific for developer only so i'm going to make that property public so here i'm not obeying the rules of encapsulation you can do that for homework for example so let's create a property that only developer has but employee does not that is going to be for example favorite programming language developer has favorite programming language but an employee does not necessarily have to be a developer so he does not necessarily need to have a favorite programming language so i'm going to say string favorite programming language like this i hope that i didn't make a typo here so now let's check if everything that i said is correct so if this developer indeed has access to all the properties that our employee has and then to this one here which is specific for developer only so let's delete this code here and then i want to create an object of class developer so i'm going to say developer and let's call that object d like this okay and the first thing that happens is we get an error and it says okay it says default constructor of developer cannot be referenced so this error here is happening obviously because we don't have a default constructor and if you remember when we were talking about constructors we said that every class when you create it every class has automatically generated default constructor but once you decide to create your own constructor you lose that default constructor so now you may ask well selena we have not created constructor for this developer class and i'm going to say you're right but we have inherited from employee class and that employee class has a constructor that we created so that means that we need to provide a constructor for the derived classes of employee class which is our developer class so now what i'm going to do is i'm going to create a constructor for developer class so if you remember three rules of creating constructors first rule is that it does not have return type second rule is that it has the same name as the class and then third rule is that it needs to be public so i'm going to call this constructor developer like this oh sorry and then i need to put these curly brackets here okay so now the job of this constructor here is going to be to construct developer object and considering that this developer object inherited from employee class that means that developer has these properties so these attributes and then this one which is specific for only developer class so now we have to provide this constructor with those parameters so i'm going to copy these three here because i already have them and then paste them here and then i will add this parameter which is specific for developer class so i will say string string language like this so this is favorite programming language or actually let's call it favorite programming language so that we are consistent okay now once we have passed these four parameters to our developer constructor what we have to do is we have to initialize this attribute here and then these three attributes here because those are attributes that our developer class has but one thing that i want to show you is following considering that this developer class is inheriting from employee class and that employee class has a constructor of its own which is this one here that constructor already knows how to construct name and company at age so that means that we don't have to worry about these three properties in our developer class we should only worry about a specific properties of our developer class and these three properties that actually belong to our employee we can pass these three to our employee constructor so i'm going to do that like this here so after this parentheses you put a column sign and then you specify the name of that constructor so employee so the name of your base class and then here i will pass these parameters that our base class constructor receives so name and then company and age okay so now this constructor the constructor of our base class is going to take care of these three properties and we are left with this property here which is specific for our developer class so here i want to initialize our favorite language property so i'm going to say that favorite language is equal to whatever has been passed to this constructor okay now if i go down there to my main function where i tried to create my developer let's try to construct an object of type developer let's try to invoke this constructor here like we did in this line here and then in this line here for our employee so i'm going to say that a developer is going to be equal to developer and then here i want to pass values for this constructor here so those are name and company name and age and favorite programming language so let's call this developer also salvina i'm going to delete these two because we will not need them anymore okay and then let's say that i work for youtube code beauty and then i am 25 years old and my favorite programming language is c plus plus for example so now that i have successfully created an object for developer class let's test this so how am i going to test this well let's create a method on our developer class that is going to use some of these values here so that we can know if these values have indeed been set successfully so what kind of method can we implement on developer class for example we can implement a method called fix bug so i'm going to do that let's collapse this constructor and let's say here void fix bug and then what i want this method here to do is i wanted to say selena is fixing bug using c plus or selena fixed but using c plus plus so here i'm going to do that i'm going to say std c out and then in order to access the name of this employee i'm going to use getter so i'm going to say get name which we created previously and then here i'm going to say fixed bug using and then let's use this programming language here like this okay and now if i want to test this method here i can invoke it on my developer class so i'm going to say d dot fix bug and if i run my program as you can see this method here works and then the values that we passed in our constructor have successfully been set so here it says selena and then fixed bug using and then here is my favorite programming language okay let's close this and now there is one more thing that i want to show you and that is this part here so i want you to notice that this here is the way that we are using in order to access properties of our employee class and then we are accessing directly properties of our developer class so can i access properties of my employee class directly can i access this property here for example name or do i have to access it by using getter write me your answers in the comments down below so i am going to test that now here i'm going to save name and we get an error it says employee name is inaccessible so we cannot access this property here name which is the property of our employee class why well if you remember when we were talking about access modifiers we said that whatever is private that is going to be locked hidden that is going to be accessible only inside that class and then whatever is public we will be able to access that outside of the class so how can i make this property here how can i make this name accessible in the derived classes of this class here again when we were talking about access modifiers we said that there are three of them public private and protected and i promised you that i am going to explain what this what protected access modifier means so in this situation if we make this property here if we make this name property protected that will make name available in derived classes of this employee class here so i am going to say protected like this and then i'm going to take this property and i will move it to my protected area and then if i return to my developer class as you can see the error that we previously had is now gone so now this name property is accessible directly from this derived class here and if i run the program it should work the same as it did and as you can see it indeed works the same so selena fixed bug using c plus okay now there is one more very important thing related to inheritance that i want to show you and that is the following let's say for example that this developer here fixes a lot of bugs so if i run my program you can see that salvina indeed is fixing a lot of bugs so what this developer here decides then is he or she in this situation she decides to ask for promotion so i'm going to try to do that now i'm going to say d dot but if you look at the list that i have here it only has the access to favorite programming language and then fix bug so if you look at this you can see that i don't have access to the other properties that i have inherited from my employee class i do have access to them here so if i try to invoke them in this method here so if i say here for example ask for promotion as you can see i do have access to them here but as you could see on my developer object i cannot access those properties so how can i fix that problem well the answer is pretty simple this inheritance here so this part of the code here this inheritance is private by default and in order to fix the problem that we have we need to make it public so here i'm going to say public and the problem should disappear so if i return to my developer object now and if i try to ask for promotion as you can see that method is available so maybe i'll get a promotion this time let's test this and unfortunately i am not getting a promotion but at least my code is working so now we have access to this method here which is implemented in my employee class so in my base class and we have achieved that by making this inheritance public so while we are talking about inheritance let's create one more class that is going to inherit from this employee class so let's create one more derived class besides this developer so what kind of class can inherit from employee what is more specific class than employee for example let's create a class called teacher so here i'm going to go one more time through this process of creating derived class so i will say class and then i will call it teacher like this okay and with this we have successfully created a class called teacher and now what i want to do is i want to inherit from my employee class like this okay and then what i need to do is let's first create some specific attributes for my teacher so let's say for example that a teacher has attribute called subject so this here is the subject that my teacher is teaching and then let's say also that the teacher will have a functionality that is going to be specific for this class only and that functionality will be called prepare lesson for example so i'm going to say void prepare lesson like this okay and what i want to do here is i want to do something like this so i want to say for example the name of that teacher and then i won't say that he or she is preparing lesson from this subject here so let's delete this empty space so i'm going to say std c out and then let's put the name of my teacher so name like this and then let's say is preparing and then here i want to put the name of my subject like this and then let's add lesson okay now there are a few problems that we have with this class here the first problem is that these here are private which means that we will not be able to access them outside of this class here because everything inside the class is private by default so that is going to be the first problem and let's fix it so i'm going to say public like this okay now second thing that is going to be a problem for us is that this teacher class here does not have a constructor so if i try to create an object of type teacher i will not be able to do that so let's demonstrate that error now i'm going to say teacher like this and let's call it t and well if i do this only we should get an error which says default constructor of teacher cannot be referenced which means that we need to implement a constructor for this class here as we did for this class here so as we did for our developer class so let's do that i am going to create a constructor called teacher like this and then this teacher constructor will receive these three properties that our employee has like this and then it will receive another parameter called subject subject okay now this constructor is going to pass these three properties to constructor of base class so this one here let's do that let's say let's say here employee and then inside i will pass name and company and age and then here this teacher class will take care of constructing this part so i'm going to say that my subject is equal to this subject that we received in our constructor okay and now if i return down here and i try to make a teacher let's name the teacher jack for example and he is going to work in a school called cool school if such thing exists okay and then he's going to be 35 years old and he is going to be history teacher for example okay now we have successfully created our teacher jack and i am going to collapse this and then i want to test this method here so i'm going to say teacher t dot and then prepare lesson okay and if i run this program it says that jack is preparing history lesson okay and then one more problem that we have with this class here is that our teacher t does not have access to properties of our employee class so if i type t dot as you can see it has access only to these two properties so in order to fix that problem i am going to add public here okay and if i try to make jack ask for promotion let's see if jack gets the promotion because i didn't get promotion okay it says jack got promoted excellent and i didn't and that is because jack is 35 years old and i am 25 years old okay so if you don't know what i'm talking about you will have to return to the part where we are talking about um encapsulation i believe okay so that was the story of inheritance we created two derived classes one is developer and the other one is teacher and those two classes are inheriting from a class called employee as you can see here and here and we will be using these classes in our next example to explain our next concept which is polymorphism as i said fourth principle of object-oriented programming is called polymorphism and this is the one that i see people struggle with the most for some reason i don't know why because it is pretty simple and cool as you will see so it is very simple but only if you understand the things that i have explained so far in this course so the first thing that i want to explain is what is polymorphism uh the word itself comes from greek language and it is a compound of poly and morph which means many forms and in programming polymorphism describes the ability of an object or a method to have many forms now the most common use of polymorphism in programming is when a parent class reference is used to refer to an object of a child class does that sound a little bit complex when i say it like that i believe that it does and i agree but stick with me for a couple of minutes and let me show you how simple this really is on the example that we have here so in order to explain how polymorphism works let's return to our employee class so this one here i'm going to collapse this and then what i want to do in my employee class is i want to implement one more method and let's call that method work so i'm going to say void work and then here in this method what i want to do is i want to say for example out um let's say name of this employee and then let's say that that employee is checking email and then he is checking also task backlog okay it's backglog okay task backlog and then he is performing those tasks and so on okay and let's add end line like this okay now let's return to this main function here and what i want to do is i want to invoke that new work method that i just implemented and i want to invoke that method on this developer and teacher class because considering the developer and teacher are inheriting from that employee class we should be able to do that so if i say d dot work and then t dot work let's see what will we get if i run this program okay it says here selena is checking email task backlog performing tasks and so on and then jack is also checking email task backlog and performing those tasks but one thing that i don't like is i don't like me checking emails and checking task backlog and so on what i like is i like to write code so how about implementing this work method on my developer class as well so let's return here to my developer class and what i want to do here is i want to copy this method like this but instead of checking email and checking task backlog and so on what i want to do here is i want to say that i am writing code so let's say salina is writing and then let's put this programming language here like this and then let's say code okay and now i'm going to do the same for this teacher class here so let's copy this method and then paste it in my teacher class but instead of writing code teacher is going to be doing something else so what teacher does well teacher teaches so i'm going to say the name of my teacher which is jack in this situation so jack is teaching and then let's put the name of his subject which is history i believe okay now one thing that you must notice is that this work method has the same name here and then here as well and it also has the same name in this base class so it is called work but the implementations of this method this work method are different here it says that an employee is checking an email and checking task backlog and so on and then developer has also its own implementation which says that developer is writing code because that is his work and then teacher is teaching his subject which is his job so i have here invoked these two work methods and let's see what will happen if i run my program now okay now it is better it says that salvina is writing c plus code and jack is teaching history okay now let's make one small improvement to the code that we have here or actually two improvements and the first one is going to be this writing with double t okay what is this this is criminal and then the second one is going to be the rule that i mentioned in the beginning when i said the most common use of polymorphism so let me copy that definition here and then we will implement it literally word by word so here it is and it says the most common use of polymorphism is when a parent class reference is used to refer to a child class object so let's see what this means in practice i'm going to delete these two lines because i don't need them anymore and then what i want to do is i want to create a pointer of type employee like this and one quick disclaimer if you are not familiar with pointers i am going to refer you to my channel which is code beauty with where i have an entire playlist which is dedicated to pointers so make sure to watch that and then you can continue with this example here so again i have created an employee pointer and what i want to assign to this pointer is not going to be an employee object so i want to assign to this pointer here a reference of this developer and then this teacher class so here i'm going to say you are going to hold a reference to this developer here and this here is the rule so a pointer of base class which is this here can hold reference to derive class object which is this part here so so that is just the rule why well because this developer here this developer is deep down inside it is an employee because developer is inheriting from employee as we already could see in the previous examples of this course so now that i have done this let's do the same for our teacher so i'm going to copy this line and then here i'm going to name this employee 1 and then this is going to be my employee 2 and here i will say that employee 2 will hold the reference of my teacher here so now what i want to do and what i want to achieve with this is the following wouldn't it be nice if i could do something like this so employee one dot work okay and then employee two dot work and this symbol here is used when you want to access members using a pointer so instead of dot symbol when you use a pointer you use this symbol here okay so wouldn't it be nice if this here worked well let's see maybe it does if i run my program as you can see it again says alina is checking email and task backlog and performing tasks and so on and jack is doing the same and he's a teacher and i'm developer so that means that this here does not work but if you only knew how close we are to making this work you are literally missing only one tiny little thing that i didn't tell you to make this here work so what that thing is well let's return to our employee class and then here this work method let's make this work method virtual so i'm going to say virtual here so let's run our program now and as you can see it says saudina is writing c plus code and jack is teaching history so it works wait i have to explain this okay so let's return to our employee class to explain what just happened well look at this function here this virtual void function this function with this virtual keyword is known as virtual function and what that means well it means that when a virtual function is invoked it says hey can you please check if there is implementation of this function in my derived classes and if yes please execute that instead so what ends up happening is that the most derived version of this function here is going to be executed and that is this function here for developer and then this function here for teacher so does that mean that if we didn't for example have implementation of function called work in our developer that our developer then would do this that our employee has in its work function the answer is yes so let me comment this just to demonstrate how it would behave so now i commented implementation of work function in my developer class so if i run the program now as you can see it says salina is checking email and task backlog and performing some random tasks and then jack is teaching and this is happening because teacher class has its own implementation of work method work function but developer class does not so here developer is going to execute parents class method work which is this method here so that's why i am checking email and task backlog and performing random tasks instead of writing c plus code so let's close this and let's now return this i'm going to uncomment it like this and let's run my program once more and as you can see everything works again and we have this polymorphic behavior so that ladies and gentlemen is polymorphism so let me give you one little tip of what we can do with this knowledge now so let's close this and i'm going to return to my main function and here as you can see here we are creating a base class pointer to a derived class object which is in the situation developer and then in this situation it is teacher and then in some other situation it can be some other type of employee so if we created more derived classes which we could do we could create for example derived class called bus driver or pilot or singer or actor doctor and so on and then we could keep objects of those derived classes like this so we could reference them with this base class pointer because they would derive from employee as well and then imagine situation where you had 100 or thousand or even 10 000 employees and then you are storing those like this like we did with our developer and our teacher and then you could literally in one line say employee dot work and what would happen is that all of those different types of employees would start doing their own job so painters would paint singers would sing actors would act doctors would talk with patients or something i don't know so that is the most beautiful thing that we have achieved with this here so i hope that you enjoyed this video if you did give it a thumbs up and then also if you want more videos like this one you can visit my channel which is code beauty and there i have a lot of videos related to programming and c plus and also leave me a comment i would love to read something like hey i am coming from precode camp or something like that on one of my videos so thank you very much for watching and i'm going to see you in some next video on this channel and then if you decide to visit code beauty youtube channel i will definitely see you in one of my videos bye so now i'm going to take a break for a couple of hours but for you it is going to be just a couple of seconds because i'm going to pause the video here and for this i want to try a transition that i never did before and it is this one so what is polymorphism the word polymorphism comes from latin language and it means or is it greek 2 000 years later i'm just going to skip this information so if i hover over this as you can see we still have an error but we have a different error which means that we are making progress also visit me on my channel which is code beauty and subscribe there and also leave me a comment i would love to read something like hey selena i am coming from free code camp and your course was great or hey salina i am coming from free code camp and your course was excellent or perfect or outstanding or something like that you know