Example & Tutorial understanding programming in easy ways.

Define virtual function in C++

Virtual function in C++
A virtual function a member function which is declared within a base class and is re-defined(Overriden) by a derived class.

How make virtual function ?
using the keyword 'virtual' we make virtual function


#include< iostream>
using namespace std;
class base {
virtual void print () {
cout<< "print base class" << endl;
void show () {
cout<< "show base class" << endl;
class derived:public base {
void print () {
cout<< "print derived class" << endl;
void show () {
cout<< "show derived class" << endl;
int main() {
base *bptr;
derived d;
bptr = &d;
//virtual function, binded at runtime
// Non-virtual function, binded at compile time


print derived class
show base class

Read More →
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!