Transcript for:
Essential OOP Principles in C++

so in the series of learning object-oriented programming in c plus plus in the previous video we have discussed what is object-oriented programming and how it is different from processor oriented programming the difference we have seen the basics about object-oriented programming like now in this video we'll see oops concept right before we deep down into the object-oriented programming in c plus plus you must have some idea about the concepts the no the things because of those things we call it as object-oriented programming what are those concepts like class objects abstraction encapsulation inheritance polymorphism six concepts are there so in this video i am going to talk about these concepts briefly i just want you to get familiar with these concepts in detail we will discuss about these concepts one by one in later videos once we will move you know deep down into c plus plus course right but at first you must be familiar about these concepts like what are these concept at least the basic idea you should have right so those things will discuss in this video every concept in brief i'll relate it with the real life example i hope you will get it right but before that i just want to tell you one thing with theory practice is also important the key to success in any interview specifically in programming interview coding interview is practice practice as much as possible it's not like that start practicing coding problems just you know few days before your interview no the best method of preparation is incorporating your interview preparation into your everyday coding practice it will help to improve your logic building skills your thinking capability your problem solving skills and that's exactly the industry wants a problem solver but the question is how to get started from where you can practice the problems coding problems so here is the solution this is the practice page of geeks for geeks here you will get many coding questions different different topics like from dsa array linked list stack in algorithm you will get different coding questions on the topic searching sorting and many algorithm based on many algorithm as well as company specific coding questions practice questions you also get suppose if you want to practice the problems which are being asked in amazon interview then just click on that and you can you will find out the list of problems and you can start solving these problems and it is not good for students only but as well as for working professional if you are working somewhere and you want to switch you are applying for new position then also it will help you practicing these problems will definitely help you so i think this is a great resource to practice these coding problems don't worry the link i'll put in the description box of this video you can go and check out now let's back to the topic so what are hoops concepts six concepts are there basically these are six concepts in object oriented program see as the name suggests object oriented so obviously object is the main thing everything revolve about the around the object so you'll see these concept and these four are known as you can say pillars pillars of object oriented programming but we will discuss these six things all these six concepts so let me just take one example see let's take one example like student maybe you will you know related better student so simply student in student in your class you have male as well as female student right now every student is having some common properties like two hands two legs two eyes nose height weight color these are some common properties they have right and some functions like sleep can eat can study or speak and sit here these are some common functions right every student is having either male or female so we can consider it like a student is a class which is what it is like you can say a user defined data type in technical terms if you say then it is a user definer type as in c we have structures user defined data types which is having some data members and member functions now what are those data members the attributes the properties they have like student is a class and having like you can say legs and hands some body parts you can say then you can say height weight and color right these are some properties or you can say attributes or you can say data or you can say data members so whatever the data that would be considered as in the form of that would be stored in the form of variables in the class next functions what we can have like eat sleep and sit speak and many more so these are some common functions right so class is basically what a blueprint of an entity which defines the core properties and functions of that entity now entity means one entity is male and female you know students actual students are entities like student name can be ram you can say jenny rahul nisha these are some students so male students female students right so these are and you can say real world entities and these are known as known as objects okay in my class i have a student like rahul rahul is an object right of class male class right two more classes are there you can say like male and female here we have male class and female so object is what a real world entity rahul physical existence rahul is having physical existence class is just a logical thing it's just a definition it's just a blueprint so no memory allocation would be done when you define a class no memory allocation because it is just a logical thing it's not physical object is physical thing now we can access these properties and functions of a class using objects of these class or you can say instance of the this class right rahul is what you can say an object of male class so whatever is there in male the attribute and the functions rahul can access using rahul we can access those properties and those functions so rahul is physical thing rahul is having some space right in this environment here in the class right so same when you create an object then only memory allocation would be done in the if i you know say in the form of programming so class is what it is just a blueprint or a prototype through which we can create objects or you can say it is you can say group of objects which share common properties right common properties in common you can say attributes these are attributes this is you can say behavior or methods behavior or methods or functions we can say don't worry i have notes also i'll provide you notes everything i have written in my notes handwritten notes right you just have to understand now right what is class and what is object now see object also i hope you got it right it's a physical thing or it's a real world entity right which is having some behavior or some properties of its own behavior and properties fine that is object so like suppose i have an object one object is ram you can say nisha and pile these are some objects right so objects were just an instance of a class rahul and these are instances of male class initial pile these are instances of female class so they they are having their own data and functions or you can say the code right each object is having its own data and the functions the functions which can manipulate that data each function has its own function and data like ram is having its own you can say height weight color right and on function is there ram is having its own functions rahul is also having different height color and weight every object is having its own you can say code and uh data using code or the functions we can manipulate that data fine and each object can interact with each other with the help of message passing and for interacting they don't need to have the internal knowledge of their data and code suppose if i want to rahul want to communicate with ram so it's not like that rahul has to know the internal everything about ram right what rahul need to know in which form the message would be accepted or you can say the language rahul understand hindi or english that's it and in which language rahul will respond that's it so same in programming one object can interact with other object without knowing the internal detail the code or the ah no the data of that object what just need to know the type of message would be accepted by other object and what type of output the what type of response that object will give right so that the objects can also communicate with each other using message passing so class is just a logical thing it suggests a blueprint a blueprint to create objects and objects are physical thing identify you know you can identify that entity real world entity which is having its own you can say properties and behavior right now first thing we'll discuss inheritance because through this diagram you can get it better see male and female these students are also having like these properties and these functions and female will also have female in a student will also have these properties and functions and these properties and function we have already defined in a class student so rather than defining these things rather than defining these functions and everything here also here also we will do what we just reuse this thing and how you can reuse this is one of the most important characteristics so you can say pillar of oop oop oops you can inherit these properties through inheritance so they will inherit these properties here also and here also so whatever is there these functions you can say these functions and the attributes attributes means the variables and the functions right here also and here also and male and female each class can have their extra their own attributes like some body parts in female are having which male are not having so they can have their own data their own function right male can also have their own function and data and thus common properties they can inherit from this class from this class rather than rewriting again and again so this is what beauty of inheritance these are known as child class and or you can say derived class and this is known as base class or parent class right so code reusability is there so that is why rather than writing same code again and again productivity would increase we can reuse some other code and we can just do whatever we want to do one more thing in object i want to tell in technical terms we say objects are having its own data and code through which we can manipulate the data like ram is having its own data and code means the functions this code right whatever we write here so if ram suppose i can say if ram will eat more then weight would increase so through these functions we can modify these data of that object it's not like that if rahul will eat more then weight of ram would increase no because these details are specific to these objects if jenny will eat more weight of jenny will increase not the weight of nisha or pile right so this data and code are binded together in the these objects using that code we can manipulate the data of that object only right i hope you got class objects and inheritance now we will see what is abstraction see let's take one example first suppose there is a coffee machine what is there one button is there make coffee you just press this button and your coffee is ready right if a machine second machine is something like that there we have a button like add some cold water to the pan boil the water second button is boil the water third button is add one cup of coffee there add sugar this something like this we have many buttons the complete process we have right so which one is better just the first machine having one button make coffee you just press this button and coffee is ready you don't need to go into the detail the implementation part means the making part of that coffee right so means in first machine we are just hiding the detail the process of making a coffee from the user that is what abstraction hiding implementation detail from the other world only showcase the relevant details which are relevant to the world which are relevant to the other users only showcase those details hiding the implementation detail that is just abstraction like when you switch on your fan you don't know what is happening behind there you just switch on you just press that button and your ac is on your fan is on your tv zone like your dvd player once you switch on your you know you can say the tv and the dvd player or that you can say what is that setup box then on the screen the whole world is in your room you can see everything right but behind the scene what is happening how you are getting that data on your tv on your you know set of works through satellite or what is happening you don't know right so that is what abstraction same hiding something into a function or in java we can uh you can we can achieve this abstraction with the help of interfaces and abstract classes 100 in abstraction we can achieve in java right so hiding something into a function or you know an interface these kind of things that is called as abstraction because when you call a function sometimes you don't need to go into the detail of that function that how that process is doing you just need an output you call that function and you need the output that's it because see there are in large scale application there is a large code base if you go in the detail of each and every function then it would take you even months to read out all the functions and you would never code right so rather than going into the implementation detail just showcase the relevant details only that is simple abstraction like in this you can take some other classes their animal or you can say alien i hope spelling is correct maybe animals or aliens know that like student or male 1 female can walk can uh you know eat can do these kind of things but how they don't know the implementation detail so that is simply obstruction in programming also we can achieve obstruction but how we will discuss later just i you know i just want to get you familiar with these terms basic terms next is encapsulation as the name suggests in capsule capsule means means you have seen a capsule medicine the tablet there we have a wrapper in which we have some medicine right there is a wrapper around that that thing right so that is what we are hiding the inner thing we are hiding from the inner whatever is there in the capsule we are hiding that thing by putting the wrapper from outside the world right like around the chocolate we have a wrapper so that chocolate is hidden from the outside world with the help of that wrapper right so sometimes encapsulation is also known as referred as data hiding right so wrapping up of these things data and the functions into a single unit that is known as encapsulation or you can say like something like this you can take this example binding this this you know data and the code together in one unit is known as encapsulation so simply what encapsulation we can say like here in a class this is a class in class we have we have data or attributes you can say right and we have methods or functions or behavior so binding these data and these methods or the functions together this is known as encapsulation in c plus plus that's it this is encapsulation right now you can hide this data from the outside world because we have a uh in a concept we have something in c plus but that is x specified means you just keep the data private public private protected three specifiers so you just keep the data private right and this suppose this object is having ram this is the data forum and these are methods these are the methods right for rahul also this is the data and these are methods so you can keep that data private only these functions these are public methods so only these functions these methods can can what access this data this object another object cannot access this data or another class cannot access that data let's take uh one example like uh we have in college we have multiple departments csc mechanical civil ec right suppose i am faculty in computer science department and i need one information i need some data from mechanical department right so it's not like that i can directly access the data from mechanical department right i will first talk to i will reach out to a person in mechanical department right and i i would request to that person to get me that data and after that through that person i can access the data right directly i cannot access exist so the data of mechanical department the employees or the faculties of mechanical department completely they are a single unit known as mechanical department so they are wrapped up they are in camps encamps encapsuled so the data is private to mechanical department only the faculties or employees of that department can access that data so this will increase the privacy the security of the data that is why in the previous video i have told you the main reason we have switched from pop to oop is data security and through encapsulation we can achieve that thing data security privacy by keeping the data private because we have access specifier concept here just keep all your data private and only these methods only the methods of that class access that data only right no other class can access that data if another class want to access that data they will communicate through these functions right and through these functions they can access that private data right we have some concept of friend function friend class but that also will discuss later will not go in this much detail i hope you know the basic idea of encapsulation right so it is you can say just a shield uh which prevents your data from being accessed by the code outside of that shield right or you can say the data or the variable this is the data or you can say the variables of one class can be accessed by the methods of its own class not by the other class right like other classes alien so they cannot access this data this data of human being class or a student class and how to achieve this encapsulation just keep your data private use a specifier private and keep your methods public and using these methods you can manipulate this data only these methods these functions of that class only can access this data private data this is how you can achieve encapsulation that's it now next is polymorphism see this poly means many and move means forms many forms polymorphism now how this would be related let's take a real life example here see me jenny is one person right but i have multiple forms or you can say many behaviors or many characteristics i can i have like i am i'm a faculty right i am a suppose a class teacher a tutor of any class uh or a friend or a colleague so with according to the situation i will change my behavior jenny is one only one person only but having multiple forms as a faculty like if i'm interacting with hod my behavior would be another means different if i'm in class as a class teacher or a tutor then my behavior would be different with a friend my behavior would be different with my colleagues suppose not friend but just a colleague my behavior would be different right so i can have multiple forms but person is one so this is what polymorphism right now how you can relate this in programming in programming we can have two types of polymorphism or we can achieve polymorphism uh by two types overloading and overriding see what overloading means let us take this example we have a function let me just rub this first suppose we have a function sum suppose we have this function sum one function is this i haven't written properly right some data would be there semicolon and all we have but it just just to get you familiar with this thing this function is having two argument integer this function is one end one float two float this is having three arguments right so this is what overloading of the function the function name is same but it will do sum of two integer it will do some of one in one float it will do some of two float number it will do some of three integer numbers this is what overloading just function name would be same maybe you can change the number of parameter the type of parameter the return type of that function according to that that function will behave differently so this is what overloading of the function sum so this is overloading function is same but having multiple forms one is overriding overriding means what actually overriding allows a child class to override a function which is already defined in its parent class right like let us take this example this is overloading and in overriding function name function would be same function name number of argument type of argument the return type of the function everything would be same just we change this code the implementation thing the mechanism suppose in parent class sum is equal to f x plus y this we have done sum is a variable or you can say another variable is one s x plus y maybe a child class will inherit this thing from the parent class but totally change the implementation and it will do some is equal to x minus y also it it's not correct but i just want to you know differentiate the overloading and overriding with the same example right so i have just everything is same name number of argument type of argument the return type everything is same but we have changed the implementation detail the child class has implemented that function specifically according to its need so this is what overriding of that function right let us take that example real life example ah suppose here we are we were having a function uh what walk right and male and female will inherit this function work right so male and female according to their self they can modify this like walk with legs argument will pass legs but in student class in parent class it is what normal walking like this we can walk right fine it's normal walking and they override this function walk with legs the same thing but they change this part this implementation part suppose female class they they'll change this part walk reverse something like this so they have changed the mechanism of walking although the name is same argument is same they are working with legs but they have changed the mechanism so that is overriding right and overloading means what simple if you take this example walk with legs right but another thing we have changed the argument like walk with the hands we haven't changed this mechanism this is just walk normal walking something like this but we have changed like the argument if here we are passing legs we have changed hands two hands we are working something like this so i hope you can relate this example also or if you are not able to relate you can take the previous example that some function and all right so the through these concepts we can achieve uh polymorphism like operator overloading is there function overloading is their function overriding is there in detail we will see these things in the later videos and this is known as if you achieve polymorphism using overloading that is known as compile time polymorphism this is known as runtime polymorphism so in detail we will discuss about these concepts one by one i hope the basic idea about these concepts are clear to you the four pillars of uh this object-oriented programming as well as the you can say six concepts of oops right if you have any doubt you can tell me in comment box maybe in the next video we can revise that concept or we i can take another example i can discuss that thing right so that's it for this video and the next video will see introduction to c plus plus so now i'll see the next video till then bye take care