Skip to main content

Virtual function in c++

Hello friends, in this post today, we learn about what is Virtual function in c++ and what is the rule of virtual function in c++so lets start...

Virtual function in c++,virtual function
Virtual function


Virtual Function:-

It is a run time polymorphism and also known as late binding or linkage binding.


Virtual function is a function which is declared virtual by placing keyword virtual before the function definition/declaration.


The virtual function is usually defined in the base classes and is overridden in the derived class.


Base class and derived class have same function name and base class pointer is assigned address of derived class object then also pointer will execute base class function.


To execute function of derived class, we have to declare function of base class as virtual.


After making virtual function, the compiler will determine which function to execute at run time on the basis of assigned address to pointer of base class.


Virtual function behaves like any other member function but they show their importance when accessed via pointers of base class.


Rules for virtual function:-

1. The virtual functions must be member of any class. They cannot be declared outside the class.


2. They cannot be declared as static members.


3.They can be called using object pointers and even using objects but actual works can be seen only with pointers.


4. A virtual function can be friend to another class.


5. A virtual function in a base class must be defined, even though it may not be used.


6. If two functions with the same name have different prototypes, C++ considers them as overloaded functions, and the virtual function mechanism is ignored.


7. When virtual keyword is present, functions are called on the basis of which type of object pointer hold in it.


8. We cannot have virtual constructors, but we can have virtual destructors.


9. Pointers follow the hierarchy of inheritance and pay regards to the older. The derived class pointer cannot point to the object of base class.


10. Never use ++ or − − operator on pointer of base class pointer to get the next object of derived class. It will only forward/backward the pointer relative to its own class type. Therefore we should not use this method to move the pointer to the next object.


11. If a virtual function is defined in base class, it need not be necessarily redefined in the derived class. In such cases, call will invoke the base class.


Example:

#include <iostream>
using namespace std;

class A
{
    public:
    virtual void display() // virtual function
    {
      cout<<"Hello from class A"<<endl;
    }
};
class B:public A
{
  public:
  void display()
  {  
   cout<<"Hello from class B"<<endl;
  }
};

int main()
{

A *ptr;
A a;
B b;
cout<<"A class pointer ptr assign address of A class object a"<<endl;
ptr=&a;
ptr->display();
 
cout<<"A class pointer ptr assign address of B class object b"<<endl;
ptr=&b;
ptr->display(); 
return 0;
}

Output:-

A class pointer ptr assign address of A class object a
Hello from class A
A class pointer ptr assign address of B class object b
Hello from class B


I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with the something new information.

Thank you so much for reading. And take care about yourself and your family.



Comments

Popular posts from this blog

What is reference variable in c++

Hello friends, in this post today, we learn about What is reference variable in c++. and what is its major use  in c++ . so lets start... Reference variable in c++ Reference variable:- Reference variable is a new concept in C++. A reference variable is a variable which provides an alias (alternative name) for a previously defined variable or any constant. Reference variable is an internal pointer. Declaration of reference variable is preceded with & symbol ( but do not read it as ‘address of ‘). Reference variable must be initialized during declaration. It can be initialized with already declared variables only. Reference variables can not be updated. This mechanism is useful in object oriented programming because it permits the manipulation of objects by reference, and eliminates the copying of object parameters back and forth. It is also important to note that references can be created not only for built-in data types but also for user- defined data types. Syntax: Data_...

'this' pointer in c++

Hello friends, i n this post today, we learn about 'this' pointer in c++ and what is the  use  of   'this'  pointer .  so lets start... this pointer in c++ 'this' pointer:- The this pointer is a special pointer which is a built-in pointer. and  'this' is a keyword. It stores the  address of current object in context. That is the current object which can be referred using this  pointer anywhere in the class. Important Points About this Pointer:- The this pointer can be used only inside the class i.e., only  inside the member function of the class and cannot be used outside and  static member of the class. The object’s address is available from within the member function as the this pointer. It  is legal, through unnecessary, to use this pointer when referring to members of the class. The  expression (*this) is commonly used to return the current object from a member function. this is a local object pointer in every instanc...

Destructor in c++

Hello friends, in this post today, we learn about what is Destructor in c++ and why we use Destructor in c++. so lets start... Destructor in c++ Destructor:- Destructor is a special and  instance member function of a class. Destructor is a member function whose name must be same as class name but is preceded by a tilde sign (~). The purpose of destructor is to destroy the object when it is no longer needed or goes out of scope. It should be defined to release resources allocated to an object. Features of Destructor:- Destructor should be declared in the public section. Destructor can never be static. Once a destructor is called for a object, the object will no longer be available for the  future reference. Destructor never  takes any argument nor it returns any value nor it has return type (no overloading is possible). Destructor is invoked automatically by the complier when object goes out scope  of the program. There is no explicit or implicit category for a dest...