Hello Hello everyone, how are you all? This is Urvi and welcome to another video by PW Skills. In today's lecture, we will be covering a new topic which is Object Oriented Programming.
So C++ as you all know is an object oriented programming language. So when C++ will be used at the industrial level to make complex systems, there will be use of object-oriented programming. The fundamental topics of object-oriented programming will also be asked by them. during your interviews or your MCQ rounds when you are sitting for internships or placements and the advanced topics that we will be discussing in C++ DSA there also we will be discussing some basic topics of object oriented programming so to know all that you will have to understand what all things we will cover in today's lecture A to Z it will be a one shot video we will be covering all the fundamental topics of OOPS in C++.
So, what all we will cover today? First of all, we will start with what is needed for OOPS? Then we will talk about what is OOPS?
Object Oriented Programming. Then we will talk about some of the fundamentals of OOPS like Class, Object, Constructor, Destructor, Encapsulation, Abstraction, Inheritance and we will also talk about talk about access specifiers. After that, polymorphism is one of the most important topics in OOPS which we will cover completely. Virtual functions, friend functions, these things can be asked from you many times in MCQ rounds or in interview rounds, so you should know this for them.
And in today's video, what we will do is, first we will understand each topic and then we will see it in code side by side so that we don't have to use only book language. but also its implementation in the code. So, first of all, the topic is why object oriented programming? Most of the people skip this topic and move to the topic of what is object oriented programming?
But before that, we should understand the need of object oriented programming. Before object oriented programming, what we had was called procedure oriented programming. Procedure oriented programming. Now what is this?
Procedure oriented programming was a conventional programming paradigm. which was used in earlier times where we used high level languages like C, Kobol and Fortran. So this paradigm was called procedure oriented programming.
And what used to happen in procedure oriented programming, whatever problem we used to solve or whatever program we used to use, which was our program that was divided into tasks this was divided into tasks there were many tasks in your program and in tasks were grouped into functions. Functions are nothing but a set of execution statements which perform a particular task. In procedure oriented programming, our main program had a lot of functions. Suppose we have f1, f2, f3 and these functions in place use some other functions. Suppose f1 or f2 are using f4 function and f4 and f3 function are using f5 function and so on.
So in our main program the whole program is divided into many functions then these functions could be related to each other where one function is using another function. So in procedure oriented programming our main function main focus was on functions. And if we talk about data, in any program we have some data on which our functions are operating.
So how did the data flow in our procedure oriented programming? Because we have some data which should be accessible from F1 and F2 and F3. Now, this data, so that F1 can be accessed, F2 can be accessed and F3 can be accessed, for this, we had to make this data global data.
What is global data? Global data is that which is accessible from entire program. In our program, we can access global data from anywhere. If there is any data which is defined inside a function, suppose I have defined some data inside F1, then that data is only accessible inside F1.
But if I am making some data into global data, then that is accessible throughout my program. Means all my functions can access this data. Now, there are some demerits of this. We have some demerits. What happened is, I said that our data, global data, this could be accessed by any of the functions.
Now, because of this, if our data, our global data, if it gets changed, it is difficult to track, it is difficult to track which function changed data. Right, because if it is global data, then anyone can access it. F1, F2, F3, F4 and F5 can also be accessed. So if its value is changing, then we don't know which function has changed it. Right, and second thing, if our global data, its data structure changes, if data structure is changed, So, the F1, F2, F3 functions which are using this data, we have to change their definition and revise their functions.
Because these functions would be written in a way that they are accessing some sort of data structure. If now that data structure changes, then we have to revise or update these functions. So, these are some of the problems which we have to face in procedure oriented programming. and that led to a new programming paradigm which was known as object oriented programming object oriented programming main focus was not on functions but on data this means that our data like in procedure oriented programming our data most of the data was global data and it was flowing freely throughout the function throughout the program sorry object oriented programming may cease ko change kya gaya takhi the data doesn't flow freely throughout our program okay so instead what we did was we binded the data to the function using it to the function using it basically agar hai pass kuch data hai aur hai par koi function hai jo ki iss data ko access karna chahta hai So we bind them together. Now how did we bind?
This we did it using objects. So our program in object oriented programming, our program is divided into objects and each of these objects will have some data and some functions. So in object oriented programming, Our main program will have some objects. This will be divided into some objects. For example, we have object 1, object 2 and object 3. Now each of these objects will have some data and will have some function.
Now what is special here is that the data in object 1 will be accessible only from function. function of object 1. Similarly, the data of object 2 will be only accessible from function of object 2. Similarly, the data of object 3 will only be accessible from function of object 3. So basically, the data and the function that is accessing it, we have bind them together in an object. And every object will have some data and some functions.
So if here, function of object 3 wants to access the data of object one so it will first have to contact function of object 1 and through that function it will be able to access the object 1 data. It cannot directly access the data of function 1. So this is the basic concept of object oriented programming. In object oriented programming, the whole program is divided into many objects.
So it is called object oriented programming. And its advantage is that it protects data from accidental changes. Because we have restricted the function that can access every data. That this data can only be accessed by this function. Now you must have understood the concept of Object Oriented Programming from the top.
But now you will understand how it is used in programming and how we will use it in code. When we will slowly become its fundamental topics. First of all, let's start with class. Now class is the fundamental unit. of object oriented programming and class ko aap aise samajh sakte hai ki it is a user defined data type humne data types padhe the c++ jab humne padhna start kiya tha humne padha tha we have data type of int type we have data type of character we have data type bool and so on and these were all built-in data type which is already built in C++.
Now here I am saying that we can also define a data type on our own and one such data type is class. So class is a user-defined data type. Let's understand this with an example that for example, I want to define mango, apple, grape I have some fruits here, I have to define them in my program.
Like I do integers, characters, even strings, I have to define apples in my program or mango, grapes. Now these all are of what type? What is the type of these mango, apple, grapes variables? These are of type fruit, so what can I do?
I can make a class, I can make a class fruit, okay, where this data type will have some properties like its color, its taste, right, and this defines a data type, okay, fruit which I am telling you here, This fruit is a data type, it is a user-defined data type. And if I make its variables, then the fruit type variable could be mango, it could be apple, it could be grape. For example, our integer type, which is the integer data type, if we make its variables, then it could be 1, its value could be 1, could be 2, could be 3. Similarly, the fruit type, which is the fruit, So, the variable of user defined data type could be mango, apple, grapes.
So, here in every class we will define some data or properties and we can define methods or functions of this class. So, as I told you, this color and taste. These are the properties of this class fruit.
As you can also define some methods or functions. Which will be specific to this class. For this specific class I can define some functions.
And what I am telling you that the variables of this class. Mango, apple, grapes. These are actually called objects.
So what are objects? Objects are variables. variables of type class So, again understand, class is a user defined data type, which allows us to define a blueprint that this data type will have some properties, some methods and functions.
And when I make any variable of this data type, then that is called an object. So, here when I made a variable of fruit class, like mango, apple, grapes, then these will be objects. We can take another example, like if I made a class student and I defined some of its data properties like its name, role number and I can define some functions on it like get marks which will tell me the marks of this particular student.
So, now this class student ka agar mai koi variable banati hoon apne code mein, agar mai koi variable banati hoon of data type student toh that will be called an object and all of these objects will have these properties and these functions jo ki maine apni student class mein define karein. So I told you that the class is giving us a blueprint. Now whatever variables I make in this class, whatever objects I make in this class, they all will have those properties and those functions which I have defined in my class. Let's now see this in code so you will be more clear that how we actually use class and objects in programming. So if we have to define a class in C++, we say class and then our class name brackets and here after brackets we get semicolon, keep this in mind that only this will be a class defined ok and then in this class we can add some properties or some data of this class like some properties like int data1 data2 and so on.
So if we have to define class fruit here, class fruit, so we can do something like this. Now here its properties, suppose we have to add property name, that will be of type string, right and we have to add property color, that will also be of type string. So, whatever properties you want to define for a particular class, how will you do that? Type, whatever the data type of that property and then the name of that property.
Like if there is a student class, so if we make student class, class student, so if I want to put the property name there, then name of the student. would be string so I will say string name. If I want to put roll number as a property of student, then roll number will be integer so I will say int roll number.
So now we will work with class fruit. And also if we want to make object of this class fruit, then how can we make that? So like we have our, Inbuilt data types, how do we use them? If I want to make an integer, a num integer, then I write int num, right?
Which is my data type and then the variable name. Similarly, here my data type is fruit and whatever my variable name is, which is my object. Okay?
So suppose we made an object by the name of apple. So this is how I can create an object of type fruit. I'm...
If I want to set the properties of this object, if I want to set the name and color of this object, then how can I do that? First of all, I cannot directly access the name and color properties in my fruit class until I make these properties public. So these are the access specifiers which we will be reading in detail later. But for now you understand that by default the properties of a particular class are private. If we want to directly access them in our code then we have to make them public.
Now if I want to set the value here. name or color of my object. So how will I do that?
I will say Apple and using dot operator I can access the properties of this object. Okay, what are the properties of this object? Color and name. So I can set the name key Its name would be a string Apple right and the color would be string Red. Now if I want to print these values I can say apple.name, apple.color.
So here our object's name and object's color is printed. There's another way that we can define an object we can do that using fruit star mango is equal to new fruit So here we are creating object using the new keyword and here we are not getting object but we are getting a pointer variable to our object which is mango. So if we are creating an object using new operator then we are getting a pointer variable which will be pointing to that object.
Now how will we use that? mango. say hum yaha pe nahin access kar rahi hunga uski properties balki hum use kar rahi hunga arrow operator yaha pe tk to mango name would be mango and mango color we can say would be yellow and yaha pe hum print kar sakte hai mango name and mango color tk okay sorry we have not changed the operator like i said we will not be able to use dot operator if we are getting a pointer variable we are getting an object pointer in the case of object pointer we will have to use the arrow operator and if object is simple then we will be using dot operator So here we have printed the properties of both the objects.
So here we understood how we can define class and how we can define properties in it. And how can we create objects of that particular class. Moving forward let's see what are constructors.
So constructors are used to initialize an object. Initialize an object. object with some values and this is a function this is a function which is called when an object is created.
So whenever we create a new object in code then this function which will be the constructor function this will be called and this will have the same name as class name so I'll have the same name as class name okay so constructor ke kuch types bhi hota hai jese ki default constructor parameterized constructor and copy constructor jo ki hum sida jo hai code mein dekhta hai iski implementation to aapko ekdum clear ho jayega what is constructor and what are these different types. So suppose I create a class rectangle. Where there are some properties of rectangle. What are the properties? Length, it has properties length and breadth.
These are the two properties of my rectangle. Now if I want that whenever I make an object of this rectangle class, the values of length and breadth should be initialized. For that I can write a constructor whose name will be same as the class name.
and here I can say that give a default value to L and initialize it from 0 and give a default value to B and initialize it from 0 So here this is called a constructor and this is known as a default constructor What is a default constructor? Where no arguments are passed No arguments passed. Here we are not passing any parameters or arguments. So this is known as a default constructor.
So whenever I make a rectangle R1, so now if I try to print R1.L and R1.B, then here 0, 0 values should be printed. Okay? Similarly, another type of constructor is our parameterized constructor. So suppose we want to initialize our values in our class by some other values.
We want to pass some values. So we can do that. Suppose I passed two values here, int x and int y.
So I can say l is equal to x and b is equal to y. So basically the x and y values that I passed in my constructor while initializing my object, while creating my object, so those values will go to L and B. So this is known as a parameterized constructor because here we are passing arguments. We are passing some parameters.
So how do I ensure that this one... Constructor call ho, to uske liye mujhe kuch karne ki zaroorat nahi hai. When I create the object, suppose I create an object R2, to yahaan pe brackets mein I can pass whatever values jinse mujhe initialize karna hai. Suppose I want to initialize by value 3 and 4. To ap agar I print R2.L and R2.B. So here 3 and 4 value are printed.
There is a third type of constructor. Rectangle. The name of the constructor will always be same. This constructor is known as the copy constructor. Now where is this used?
constructor, this is used when we want to initialize an object by another existing object, suppose we have created an object R2, now I want copy of this R2 in a new object R3, so for that I will use copy constructor, what is there in copy constructor? Now we will pass an existing object of the same class. So this would be suppose we have passed an object of type rectangle R.
So here the value of L will be R.L. Similarly, the value of B will be R.L. So basically the values of the objects that we are passing are going in the data or properties of our new object.
So how will we use copy constructor? I can say that I want a new rectangle R3 which is equal to R2. So what happened here? copy constructor use hoga jisse R2 ki values would go in a new object R3 tk though if I print here R3 dot L and R3 dot B the happy again three and four jo hai wo print hora tk for R3 object so this was the concept of constructors licking you happy One thing to keep in mind is that in one class we can have multiple constructors and whenever we want to initialize our object by some values while creating a new object, we will be using the help of constructor and the name of constructors would always be the same as the name of my class. So destructor, which is called when an object is created, destructor is a function that is called when object is deleted.
Okay, so whenever an object is deleted, whether you are deleting manually or at the end of the program, it is deleting itself, so at that time, our destructor function is called. And this destructor function, here you cannot pass any parameters into this. destructor function thirdly is the name of the constructor name was the same name as the class name is the name of tilde operator and then class name ok so this is how we will define a destructor let's see this in code now so if here for class rectangle I create a destructor so that will be tilde tilde and my class name and in this I can say destructor is called okay so we'll be able to check where and when our destructor function is called okay and this is my destructor okay so I run this program So what happened in the main function?
We created R1, R2, R3. And then when this function ends, this main function, at that time these objects are deleted, which are our R1, R2, R3 objects. So at that time this destructor function will be called.
Okay. You can also see that this destructor function is called. If we delete an object manually, suppose... is function co-harm sorry is object core um s a create curtain using new keyword ticket so now we'll be able to use delete keyword on this ticket this delete keyword you can only use on a pointer variable ticket to yaha pay hamara r1 is a pointer variable and I have to change this.
So here we will see that first r1 will be deleted. Here destructor function will be called. And then when our main function will end then again destructor function will be called. So let's run this.
So this is happening here. First of all r1 object is created here. Then when we will delete it then destructor function will be called.
Then after that R2 and R3 are getting created and when our main function is ending then our destructor function is getting called twice. Once for R2 and once for R3. So that's how our destructor function works. Now let's move to next topic which is encapsulation.
So encapsulation is a property of OOPS which ensures binding of which ensures binding of methods and variables together into a single unit and what is this single unit? this is the class which we have just read so in oops, encapsulation is a property which ensures that the methods and variables or the functions and data are bound together in a single unit which is called class. And how do we ensure this binding?
We ensure this by ensuring that whatever our data is, it is only accessible from the class methods. The methods or functions defined in that particular class, only they can access the data of that class. And that's how we ensure encapsulation in object oriented programming. Encapsulation also leads to data abstraction or hiding which we will be reading now. And.
because of this class is called abstract data type abstract data type ADT because of this property of data abstraction Now we can understand encapsulation through an example. For example, any company has some divisions like admin division, tech division, marketing division and so on. So if admin division wants to access some data from tech division, they will not be able to access it directly. They will have to come to me or someone working in the tech division.
and ask us for that data. And we will be able to directly access the data in the tech division and give it to the admin division. Similarly, if we need some data which is in the admin division, then we will not be able to access it directly, but we will have to go through the people who work in the admin division. Similarly, in object-oriented programming, encapsulation ensures that the data of a class that can only be accessed from the functions of the methods defined in that. particular class.
We can see this in the code also. So if I have a class rectangle where some data is x or let's take a general class ABC which has some data x. Now this class ABC's data x, I will not be able to directly access it from my code. So for that I will have to write a function void set where I can pass some value n and it will set the value of x as n similarly I can write a function get which will return the value of x to me okay so if I make an object of ABC type object1 Right, so now by object1.x I cannot directly access the value of x because here x would be private to this class. So if I have to use x now then I would be able to do that using these functions get and set.
So I can say object1.set where I can pass any value. Right and if I want to print its value then I can say object1.get which will print the value of x right which is the property of object1. So if I print run this I will be able to see 3 here.
So did you understand that this couple. So, what is encapsulation? How is encapsulation ensured in object oriented programming?
In most of the complex systems, we keep the data of a particular class private. By default, this is private only to that class. So, if we want to access it, we have to write some functions with the help of which we can manipulate that data and operate on that data of that particular class.
Abstraction is another property of object oriented programming. So, what is encapsulation? which enables us to display only essential information while hiding all the unnecessary details or implementation details. That is known as abstraction or hiding. For example, we have We have seen a function, we have power where we pass two values x and y and it gives us x raised to power y.
Now here we don't know how this function is actually working. That is abstracted or hidden from us. The implementation details are hidden from us.
We only know that if we pass two values x and y in this function, that will return us x raised to power y. So this is an example of abstraction which is followed in object oriented programming. Let's now move forward to another important property of object oriented programming which is inheritance. How to understand this topic?
Through inheritance a class inherits properties of another class of another class. So for example, Class A and Class B and I want that in Class B, the properties of Class A come. So that I can do through inheritance, with the help of inheritance. Where the class whose properties are being inherited, like A class, that will be called as the parent class or super class.
And the class which is inheriting the properties that will be known as the child class or subclass. Now why is this inheritance useful or why do we use this in code? Let's try to understand that.
So suppose I have three children. Classes banani hai class car, class truck and class scooter. And in classes mein I want to define some properties jaisi ki tyres and capacity.
Now these properties can be in all three classes, tires and capacity. So either I can define these properties in all three classes again and again. Or I can make a parent class, class vehicle.
And these properties, tires and capacity I can define in my parent class. With the help of inheritance, I can inherit the properties of class vehicle in class car, truck and scooter. So, what will happen with this? These tires and capacities, I will not have to define them again and again in every class. Because these are properties in my parent class.
That's why when I inherit these properties in my child classes, then they will be transferred to my parent class. properties which are in all my child classes also. So you are understanding why we are using inheritance for code reusability.
So that we don't have to define these tires and capacity properties again and again. Rather we can inherit from a parent class. So this is an example of inheritance which is used in object oriented programming.
So before looking ahead to inheritance, let's understand access space. What are the specifiers? And what are some of the modes of inheritance?
How does a base class inherit the properties of its parent class? It has different modes. We will understand that here.
They can be public, private and protected. First of all, whatever we have seen till now, whenever we create a class, class A, B, C, here I was always mentioning the keyword public. I have told you that this keyword tells that whatever data members we will tell or methods we will define in our class, the rest of the code will be accessible or not that will be decided by this keyword. If I will not write this public there, then by default it will be private. So what is this public?
This is an access specifier. So if my data and functions are public, then in that case they can be accessed from anywhere in the code. If here there is another keyword, protected. If I define class here, I say use protected keyword for some of my...
some data int x. So in that case, whatever data and functions will come in protected keyword, they will be accessible in own class, parent class and derived class. Which is child class.
So, the data and functions in the protected keyword will be accessible in the current class, parent class and derived class. And if the data and functions are private by default, then in that case they are accessible. They are accessible only in own class.
Only the functions of that class can be used. That data or those functions. So, if we see this in code, we will understand.
And in code, we will also see what are different modes of inheritance. Suppose I made a class, parent class. And here we have some...
Data is defined in public as int x Then in protected we defined int y And in private we defined int z So now this x, y, z If we see from here, I can use x here. I can make a parent object and I can write p.x and I can access this data x. If I talk about y and z, y is protected. So I can use this in this class or in the derived class. I can't use it directly in the main function by doing p.y.
Similarly, if z is private, then it will also be accessible in this class. Now, if I inherit the properties of my parent class in some other class, so I create class child1, and here I am inheriting my parent class publicly. So, what happens by adding a public keyword here? I am publicly inheriting the properties of my parent class. Now, what will be the implication of this?
Here, because I have publicly inherited the properties, so x which was public in parent class, x will remain public. Y will remain protected here in this class. And z will remain public. will not be accessible.
Z will not be accessible in the child class as it is private. It will be accessible only in this class. Similarly, if I make another child class, child 2, which I am inheriting privately. Here what I am doing is, I am privately inheriting the properties of my parent class.
So what will be the implications of this? X will become private here, Y will also become private and Z will remain inaccessible here. And lastly, if I take one more child.
three class banau jo ki may protected keyword use car key inherit karu parent class ko to yahaan pay X will be protected Joe Mira X yahaan pay public thar data boy a hopper kiki many protected Lee inherit curry hey parent class key properties to X will also become protected why will be protected and Z will be inaccessible. These are different inheritance modes, public, private, protected. These are also access specifiers, which are telling us that the data that is coming under them, where all is it accessible. Here, the data... This is protected private public.
What is this? These are inheritance modes. Now there are also types of inheritance which we will be looking at next.
So first of all the basic type of inheritance is single inheritance where if we have class A and there is class B. If class B directly inherits the properties of class A, where class A would be the parent class and class B would be the child class. So this is known as single inheritance. And although we have seen this, but let's see this running in code as well. So let's make a parent class here.
I have some parent class where let's define the constructor parent class ke naam se hoge and here let's just print ki kaunsi class mein hum currently abhi hai. Similarly, agar mujhe yahan pe ek child class banaani hai. Class child jo ki is parent class ki properties ko inherit kare to uske liye I will say public parent and yaha pe bhi let's make a constructor child jaha pe we will print the name of the class So now if I make an object of class child, then what will happen here? Because this child class is inheriting properties of the parent class. So first the instructor of parent class will be called and then of child class.
So you can see here that first parent class is being printed and then child class. And this was single inheritance. Next type is multi-level inheritance where the parent class is derived from another class.
Means we have a class B and a class C. C is inheriting the properties of B and B in turn class. is inheriting the properties of class A. So here multi-level inheritance is done. And how will we write this in code?
So now if I make an object of class grandchild, so now what will happen is that grandchild is inheriting the properties of child class and child class is inheriting the properties of parent class. So first of all, the constructor of parent class will be called, then the constructor of child class will be called and then the constructor of grandchild class will be called. So this is known as multi-level inheritance.
After this, there is multiple inheritance where there is a class, the child class is inheriting from multiple parent classes. Suppose there is a class C which is inheriting from class A and class B. So in this case this is known as multiple inheritance. So suppose we have this class parent 1 and we have another parent class parent 2. So we have two parent classes, parent1 and parent2. And in the child class, we are inheriting parent1 and parent2.
So how many classes can we inherit their properties while defining a class? We can just specify the name of every parent class with the inheritance mode separated by commas. So here if I make a child class, child C.
So what will be printed? First of all, the constructor of parent class will be called, parent 1 class, then parent 2 class. Because first parent 1 is inherited, then parent 2. And then lastly, the constructor of child class will be called. Next type of inheritance is hierarchical inheritance, where we will have one parent class and its multiple child classes. Where multiple child classes are there which are inheriting one parent class.
We can see this in the code too. So I have child 1 class which is inheriting parent 1 and I have child 2 class which is again inheriting parent 1. So we have child 1 and child 2 both of these classes inheriting from one parent class. So in this case what will this be called? Multi sorry this will be called hierarchical inheritance.
And there is one last type of inheritance which is called hybrid inheritance. Hybrid inheritance may we have combination. of more than one inheritance type. For example, we have a class A which is a parent class for two classes, class B and class C. And then class C is parent class for another class, class D.
So, what is happening here? Here is hierarchical inheritance B and here is multilevel inheritance B. This is known as hybrid inheritance.
Now in inheritance, there is a problem which is known as the diamond problem. Which is often asked, not in interviews mostly but maybe in your MCQ rounds or in your exams. So you should know about this problem. Diamond problem means the base class has multiple Parent classes having common ancestor a common ancestor class for example I have a class A which is inheriting properties from class B and class C and and class B and class C you are inheriting from another class, class D, class E and it is possible that class D and class E are inheriting from a single class, class F so in this case, in class A, properties of class F will also come from here and from here do you understand?
So, here the properties of class F will come twice in class A. So, this is known as the diamond problem. We can visualize this in code also. Suppose we have one parent class. And we have two child classes, child 1 and child 2, which are inheriting the parent class.
And then there is a grandchild class, class which is child 1 class and child 2 class. If I run this, If I create an object of grandchild class, you will see that the constructor of parent class is being called twice. One time the constructor of parent class is called through child 1 class and second time the constructor of parent class is called due to child 2 class. So this is known as the diamond problem in inheritance. So let's move to the last topic which we want to do here on JOOPS that is polymorphism.
Polymorphism means this is the ability of optician. Objects or methods to take different forms to take different Forms and this co-op SSM at Sakte key for example, Hamara pass. Kui word There's a word right right right this word give different meaning who's at the end.
It can be Left and right while I write yeah, it can be right and wrong while I write So the same word can have different forms, different meanings according to in which context we are using that word. Similarly, in programming polymorphism allows us to define different forms of an object or method. And which form will be used will be decided either on runtime or compile time. And according to that we have two types of polymorphism compile time and runtime polymorphism.
In compile time polymorphism, what happens is, as I told you, if an object or a method has different forms, then which form will be picked from those different forms, that is decided at runtime. And compile time polymorphism is seen in function. is seen through function overloading and operator overloading.
So, we understand these concepts, from which you can understand what is compiled polymorphism. In function overloading, we can define a number of functions with same function name, with same function name but they perform differently according to, according to the arguments passed and what happens in arguments is that how many passes are we passing? how many arguments were passing, what is their type, according to that it decides which function would be called. For example, we have a class shape and in this we have defined a function area which finds out the area of that particular shape.
Now if this area function is being called for a circle, for square for a triangle so in sub area find the formula will be different right but we can make that function with the same name we will define the same function with the name of area but in that function how many parameters are we passing and what type of parameters are we passing that will decide which shape or form of the function would be called and we will see this in code so it will be clear for us for example we have a class called add or some here I am making a function add where if you you are passing two parameters of type int so it will find sum as x plus y and it will print that sum for you ok, with the same function name add, I can define one more function where instead of 2, 3 parameters are coming, here sum would be calculated as x plus y plus z. Okay. So, if I call add function with two parameters, so this function will be called. If I pass three parameters, so this function will be called.
Similarly, if I define this function again, where I pass decimal, float type number x and y. So, there my sum will be float sum x plus y and it will print that. So, now if I say sum s and s dot add 2 3. So, my first function will be called.
If I do s dot add 2 3 4. The second. function call hoga hai mai karu s.add and I say 2.3 and 2.7 toh yahaan pe mera third jo definition hai add function ki wo call hoga. So depending on kitne parameters mai pass kar rahi ho. Ok so idhar I will have to specify that these numbers are float.
Thike varna. It is getting confused that which definition we have to call for our add function. So now I have it, the sum in all the three cases. So depending on how many parameters we pass and what type of parameters we pass, then it will be decided at compile time that which add function will be called.
Now similarly we also have operator overloading. and operator overloading you might have seen for plus operator we see that if we want to do 2 plus 3 we can use plus operator and if we want to concatenate strings then we use plus operator there also so this plus operator this is This can be used for integers and used for strings. So, here we call this operator overloading.
Depending on in which context we are using our plus operator, similarly it would give us the result. Okay. If it is not like this, if we use 2 plus 3 here, then it will not give us 23, it will not concatenate because it knows that 2 and 3 are integers.
Similarly, cat and woman are strings here, so it will concatenate them. Now, we get to see a question in operator overloading. Sometimes, if we have complex numbers, to add them, to add two complex numbers, you overload this plus operator.
So, we will do that and see. You know complex numbers. Complex numbers have two parts, real and imaginary.
And when someone... we add two complex numbers so we add real parts of both and add imaginary parts of both so here we can create a class complex where we will have two parts int real and int imaginary part and let's also create a constructor for initializing so if we are passing two values here, int x and int y then in real, the value of x will go and in imaginary, y so to overload this operator first of all, we will tell the return type that what the operator is returning it must be returning a complex number then we say operator plus we mentioned we have to overload all the operators and then in brackets we will pass our complex argument complex argument C so here this is an object of this class complex and Suppose we made a complex C1 and complex C2. Where I passed 1 and 2 in C1 and 1 and 3 here.
So whenever I will do C1 plus C2. So this one. operator overload which I have done, this function will be called.
And here this function definition, what I will pass in argument, whatever operand is coming after operator, that I will pass. So here what is my operand, that is an object of class complex, so I have passed that here. Now here how this plus operator should work, first of all a complex, object banayenge answer and answer ke real part me ham dalenge this dot real plus c dot real and answer ke real me dalenge ham jo hamara current real hai plus c dot real and answer ke imaginary me ham dalenge current jo hamara imaginary hai plus c dot real.
imaginary and we will return answer. So if I say sum complex C3 is equal to C1 plus C2. Let's define this first and then say C3 and if we try to print this. C3 dot real and C3 dot imaginary.
So here what will be printed? Okay we didn't initialize our answer here. Let's initialize this by 0 0. and here we have 2 and i5 so our real part will be 2 and imaginary part will be 5 So here your focus should be on this function that we have written for operator overloading where once again I am telling you that first of all we will tell you what function will return then we will tell you what operator we have to overload, we will pass in brackets which operand will come on the right hand side.
So here when I write C1 plus C2 so C2 which is the operand coming after my operator, I am passing that here. And then I am creating an answer object in which I am passing real plus c dot real. Now you might be thinking which is this real? So this real is the real part of c1.
And this real is the real part of c2. If I am passing c2 here, then you can understand it as c2 which is coming in the argument. And this real part would be the real part of c1.
So when I am here... Now visualize it like this, suppose if I was doing dot operator instead of add operator So I would have written c1.c2 So this operator would work on the c1 object and c2 would pass as an argument So this I also had a problem in visualizing it, so I visualized it with the dot operator. So it became clear.
But you can write it yourself. Try overloading some other operator on your own. And it will be clear to you. For now, let's move on to runtime polymorphism.
I told you that there are two types of polymorphism. Compile time and runtime. Runtime polymorphism is resolved at runtime and this is done using function overriding function overriding happens when the child class defines a function of parent class for example imagine that I have some function in parent class and I have the same name function in child class also now which function will be called from both the functions at run time that is function overriding ok and if we see this in code so suppose I have a class parent where I have two functions one is print where I am just printing that this is the parent class and I have another function show which is doing the same thing and I have a child class child class which is inheriting from the parent class publicly public parent Here also similarly I have same two functions but the difference is that the print function I made it virtual here.
What happened with virtual function? You will decide on runtime which function will be called. So we have to use the virtual keyword for function overriding.
where we want to decide on runtime which function is getting called for that we have to make that function virtual and we can see this as an example suppose I made an object of parent class P and made an object of child class C and let's make this a pointer variable so on runtime I say P is and see means I have overrided my object variable on runtime now if I say p.print and I say p.show so what will happen here we will change it to child class so if We will know the parent, sorry, print or show function of the class will be run according to what is printed. So, in print function, our child class is being printed. Now, why is this happening?
We have assigned the pointer variable on runtime as C object, okay, object of child class. So, now, here the print or show function, we have called function here child class should be printed because we assigned it on run time child class but for print function only child class is being called not for show function because if you see show function here show function is not defined as virtual function so for show function function overriding will not happen function overriding will only happen if for print function. If you want to do function overriding then for that you have to use virtual keyword before the function name in parent class.
So this is what virtual functions are and how they are used in function overriding. Now we saw compile time and runtime polymorphism. If we see differences between them then we know compile time is Polymorphism occurs at compile time and our runtime polymorphism occurs at runtime.
Compile time polymorphism is achieved through function overloading and operator overloading. And the runtime polymorphism is achieved through function overriding. In compile time polymorphism, the function names should be same but parameters can be different. Parameters can be different. But in function overriding, function name and params both should be same.
Compile time morphism has faster execution time. Run time polymorphism is comparatively slower. Compile time polymorphism is also more memory efficient and runtime polymorphism is less comparatively. Last topic for the day which is friend function.
So friend function is an interesting topic in OOPS. Friend function is non-member function. It is a non-member function.
which can access private members of the class. For example, I have made a class. In this class, I have some data members and methods which are private to this specific class. But there is one specific function Jo main chahati hoon that it should be able to access the data members or the methods of this class A. To uske liye I can make this function as the friend function of this particular class.
And how can I do that? I can just mention meri class mein that this function is a friend function. Now this friend function will be able to access all of the private members of this class A as well.
And we can look at this in code. So if I have a class A where I am defining some variable x into x. And I want that a function is printed here where if I am passing an object of class A So this function should be able to print x value.
Now if I directly try to print here, I made an object of A class and I directly print object.x, so this will throw me an error because now x is a private member of this class A. So I can make this print function as a friend function. And I just have to do this. I will have to provide the definition of my function. That friend, void, print and whatever the param list is.
So if we try to access directly now, it will give us an error that this is a private member. But if we do it through print function, print object. So here we are getting some random value because we have not assigned any value to x yet. Let's do that as well.
Let's create here app constructor a. So if I am creating the object app pass value 5. Now if I run this 5 is printed. Why?
Because my print function is a friend of function of class A. So it is able to access this private member X. This friend function can be an independent function and this can also be a function of another class.
It is possible that I want to access the function of another class. I want that only that function can access the data members of my class. but I don't want to make it a child class or a parent class so in that case I can again use friends function concept and that is all for today's class we have covered a lot of things today starting from what and why is oops classes, objects, encapsulation, abstraction, inheritance and then polymorphism So I hope that today's lecture has covered all the fundamentals of OOPS that are required in C++ because we will use this in the next lecture. In the next lecture, which is going to be in our link list, we will be using classes and objects.
So before that, you should be clear about this. You can watch this video once again and see the questions that have been asked in this, the coding that has been passed, do it yourself and see the function overloading and operator overloading. Do implement these two things yourself.
Thank you so much for watching this video. I'll see you in the next one. Till then take care and goodbye.