C++ Language



Admission Enquiry Form

  

Virtual Function in C++:


Program of Virtual Function in C++

//virtual function C++
#include<iostream>
using namespace std;
class Base
{
public:
void fun1()
{
cout<<endl<<"I am fun1 of Base class";
}
void fun2()
{
cout<<endl<<"I am fun2 of Base class";
}
virtual void display()
{
cout<<endl<<"I am display of Base class";
}
};//end of Base class
class Derived : public Base
{
public:
void fun3()
{
cout<<endl<<"I am fun3 of derived class";
}
void fun4()
{
cout<<endl<<"I am fun4 of derived class";
}
void display()
{
cout<<endl<<"I am display of Derived class";
}

};//end of Derived class
int main()
{
Base b,*bptr;//object and pointer to Base class
Derived d;//object of derived class
bptr=&d;//pointing to derived class
bptr->fun1();// is the function of Base class
bptr->fun2();// is the function of Base class
bptr->display();

}//end of main


Output:

I am fun1 of Base class
I am fun2 of Base class
I am display of Derived class