Skip to main content

C++ Virtual Function

Why use virtual function in c++?

We know about inheritance, In this process derived class will used the properties of Base class. In this situation virtual function are applicable when base and derived class are same name of functions (methods). Or special can when derived class are overridden the function of base class. see example.

class Base{
  public:
  void item(){
    cout<<"item of base class!"<<endl;
  }   
};
class Derived :public Base
{
  public:
  void item(){
    cout<<"item of Derived class!"<<endl;
  }   
};

Note that in this class member function are same signature. and Derived class are publicly inherit for Base class. In this example derived class object are capable to access the base class all public function. When we are execute item() function using help of derived class object. Then it will execute on derived class function. Interesting are if we are need to execute base class function. Then derived class object are not capable. Because same name of both function. in this case compiler are execute only of derived class function. see example.

#include <iostream>
using namespace std;
class Base{
  public:
  void item(){
    cout<<"item of base class!"<<endl;
  }   
};
class Derived :public Base
{
  public:
  void item(){
    cout<<"item of Derived class!"<<endl;
  }   
};

int main(){
  //make Derived class object
  Derived obj;
  obj.item(); //call item function
    obj.item(); //call item function
}
Output
item of Derived class!
item of Derived class!

know question is arising in your mind? and we can solve this problem by using create base class object. then access the function of base class. see the example.

#include <iostream>
using namespace std;
class Base{
  public:
  void item(){
    cout<<"item of base class!"<<endl;
  }   
};
class Derived :public Base
{ 
   public:
  void item(){
    cout<<"item of Derived class!"<<endl;
  }   
};

int main(){
  //make Derived class object
  Derived d_obj;
  //make Base class object
  Base b_obj;

  b_obj.item(); //call item function
  d_obj.item(); //call item function
}
Output
item of base class!
item of Derived class!

This process is called early binding but help of virtual function we are implements ' Runtime Polymorphism'. there is important feature of c++.

Virtual function

We are declare virtual function when same methods are exist in base and derived class. virtual function is start with virtual keyword. there are not compulsory to declare both class member function is virtual. When we are declare base class function as virtual then compiler are automatically consider same function of derived class as virtual. In This process we will create an pointer of base class and utilize base and derived class object reference. In this process compiler are runtime decide which class function are executing.

For example
#include <iostream>
using namespace std;
class Foo{
    private:
  //data member
  int x,y,z;
  public:
  Foo(){x=y=x=0;}
  virtual void info(){
    cout<<"(x, y,z):"<<x<<y<<z;
  }  
  void size(){
    cout<<"Foo size function"<<endl;
  }   
};
class Bar :public Foo
{ 
  public:
  //by default virtual  
  void info(){
    cout<<"Bar info function.!"<<endl;
  } 

};

int main(){
  //create base class pointer
  Foo *ptr;

  //create Derived class pointer
  Bar b;
  ptr=&b;
  //dynamic binding
  ptr->info(); //derived class function execute
  //early binding
  ptr->size();//base class function execute
}
Output
Bar info function.!
Foo size function

Understand the concept of early and dynamic binding in this program. we are directly access non virtual function using base class pointer but virtual function are always need to reference of class object. see this example.

#include <iostream>
using namespace std;
class Foo{
    private:
  //data member
  int x,y,z;
  public:
  Foo(){x=y=x=0;}
  virtual void info(){
    cout<<"(x, y,z):"<<x<<y<<z;
  }  
  void size(){
    cout<<"Foo size function"<<endl;
  }   
};
class Bar :public Foo
{ 
  public:
  //by default virtual  
  void info(){
    cout<<"Bar info function.!"<<endl;
  } 

};

int main(){
  //create base class pointer
  Foo *ptr;
  /*
    most of case this line are not produce
    any compilation error and we execute this
    program then not print any output.
    this is runtime error.
  */
  ptr->info(); 
}

Output : empty result in gcc compiler or Segmentation fault.

Note that base class info function display private data member of class and there are can not possible to access those data member without class object. So how to execute both same function in class. the solution is to using proper object of class reference. See the example.

#include <iostream>
using namespace std;
class Foo{
    private:
  //data member
  int x,y,z;
  public:
  Foo(){x=y=z=0;}
  virtual void info(){
    cout<<"(x,y,z) : ("<<x<<","<<y<<","<<z<<")"<<endl;
  }  
  void size(){
    cout<<"Foo size function"<<endl;
  }   
};
class Bar :public Foo
{ 
  public:
  //by default virtual  
  void info(){
    cout<<"Bar info function"<<endl;
  } 

};

int main(){
  //create base class pointer
  Foo *ptr,f;
  Bar b;
  
  ptr=&b;//Reference of Derived class object
  ptr->info(); //dynamic binding
  ptr=&f; //Reference of base class object
  ptr->info(); //dynamic binding (late binding)

  //early binding (static binding)
  ptr->size();

}
Output
Bar info function
(x,y,z) : (0,0,0)
Foo size function

make sure you understand the concept of virtual function.

Important Points

1) We can declare virtual function of any access specifiers like (public , protected , private section). but help of pointer variable and class object we can access only public member function. see the example.

#include <iostream>
using namespace std;
class News{
  private:
  virtual void example1(){/*code*/}
  protected:
  virtual void example2(){/*code*/}

  public:
  virtual void example3(){/*code*/} 
};
class Channel :public News
{ 
  protected:
  void example2(){/*code*/} 
  public:
  void example3(){ /*code*/}

};

int main(){
 //logic here
 /*Help of pointer and class object.
   we are access public data member and functions. 
   note that not access private and protected.
 */
}

so this reason. try to declare virtual function in public section.

2) Virtual function is provided runtime (dynamic binding). that is called runtime polymorphism.

3) we can not declared static of virtual function. see example.

class News{
  public:
  virtual static void example3(){/*code*/}  
};
class Channel :public News
{ 
  public:
  virtual static void example3(){ /*code*/}
};
int main(){
 //logic here
}
Error
error: member 'example3' cannot be declared both 'virtual' and 'static'
  virtual static void example3(){/*code*/}




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment