20120113

Program in C++ to learn virtual function

//========================
//Program in cpp to learn virtual function, vrtuual1.cpp
//========================

#include<iostream>
using namespace std;
class base
{
public:
      virtual   void func()
        {
        cout<<"base function \n";
        }
};

class derived:public base
{
public:
        void func()
        {
        cout<<"derived function \n";
        }
};
int main()
{
base b, *bptr;
bptr= &b;
bptr->func();
b.func();//same as above but not run time
bptr= new derived;
bptr->func();//if func is not virtual then this also will call the base func()
return 0;
}
Previous                             Home                               Next

No comments:

Post a Comment