 Hello friends, I am Sanjay Gupta. In this video, I am going to demonstrate you how you can implement virtual function in C++. Before starting, you can note my information. You can follow my YouTube channel that is sanjaygupta underscore tech school. You can download my app Techimus that is available on Google Play. Now coming on to the topic, for demonstration I have implemented a code for you. Here you can see I have included a header file that is IO string. Then STD namespace is used. Here I have implemented a class A which is having attribute x. Then its read function is implemented which will display a message enter a value on console. Then the entered value will be stored inside x using scene. Then show method is implemented which will display value of x on console. Now I have inherited class A into class B. So class B is derived class and class A is base class. Then y attribute is declared. It is also having read function which will display enter value message on console. The entered value will be stored inside y. Then show method will display value of y on console. Now in main function I have declared two pointers one for a class one for B class. Then first I have created object of class A and its address will be assigned into P1 pointer. So here pointer and object both belongs to class A. So class A object is created and its address is assigned into P1 pointer which also belongs to class A. So here read and show function of class A will be called. So we can see that through its execution. I am entering a value 5 and that value is assigned into x which is the attribute of class A and through show function of class A it is printed on console. Now through P1 I am creating object of class B. So here pointer is of class A and object is of class B. So base class pointer can point to derived class object. It is possible. So P1 arrow read and P1 arrow show. Now object of B class is created and its address is assigned into P1. So here P1 arrow show and P1 arrow read. These two functions are called. So according to us functions of B class should be called because we have created object of class B. But here functions of base class will be called because pointer belongs to base class. We can see that through its execution. First read and show functions are called because of this P1 arrow read and P1 arrow show. Here object of A class is created. Again a value is required here. So I am entering 5. You can see again x equals to 5 is displayed. It means base class read and show functions are executed here. Now if you have created object of class B and you want that class function should be called. So here you have to use spatial keyword that is virtual in base class functions. So I have used virtual keyword with both the functions that are read and show. So these functions are now called virtual functions which are available in base class. So while I am creating object of B class, this address will be assigned in P1. Then at compilation time read and show of base class will bind with these functions called. But at the time of execution compiler will check which class address is assigned into P1 and sorry into P1 pointer. So P1 is containing address of class B object. So control will be automatically transferred to B class read and show functions through virtual keyword. So you can see it while it's execution first time entering for it is printing x equals to 4. Now I am entering 5 it is printing y equals to 5. So this time a second time read and show function of derived class are executed. So with the help of this virtual function we have executed functions of derived class because object of the derived class is created here. So friends I hope you have understood the difference. If pointer of base class is created it can hold address of base class object as well as address of the derived class object address. While compilation pointer links with base class functions. But if you want to transfer control to respective class functions then you have to use virtual keyword in base class. So virtual keyword takes care whose class address is available inside the pointer and it transfers control to the respective class. Now I am using P2 pointer and I am creating object of A class. Now here I am calling read function through P2 and I am calling show function through P2. So here you can see pointer is of class B and object is of class A. So this operation is not possible. This will provide an error message. So I am compiling this code. You can see it is showing an error message. Invalid conversion from A pointer to B pointer. So this is not possible in C++ that you cannot assign address of A class object into B class pointer. So we have to create comment for these three statements because these statements are invalid statements. Now I am creating object of class B and pointer is of class B also. So pointer and object both belongs to class B. This is a valid statement. So this time we can call read and show functions properly. So here you can see object of class B is created and its address is assigned into B class pointer. So it will call read and show functions of B class only. So this time three times read and show function will work first for base class that is a second time for derived class and third time for also derived class. So now I am executing this complete program. First time entering three. So x equals to three is printed. Now I am entering four y equals to four is printed. I am entering five again y equals to five is printed. So one time base class read and show functions are executed and two times derived class read and show functions are executed. So friends with this scenario when object of B class or derived class is created and base class pointer is pointing to that object. In this situation only you have to use virtual function. In remaining cases you don't require virtual function because you can see here in this case pointer and object both belongs to class A. In this case pointer and object both belongs to class B. Only in this case pointer is of class A that is base class and object is of class B that is derived class. So in this scenario only you require virtual function in rest of the cases you don't require any virtual function in base class. And this fourth case is invalid that is pointer of class B that is derived class and object of class A that is base class. So this situation is not applicable in C++. So I hope you have understood in which scenario you have to implement virtual function in base class. So this is all about virtual function in C++. I hope you have understood the concept well. If you want to watch more programming related videos you can follow my YouTube channel that is Sanjay Gupta Anderson for Tech School. Thank you for watching this video.