20120113

How to use Virtual function C++

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

#include<iostream>
using namespace std;
class base
{
public:
        virtual void f1()
        {
        cout<<"base f1 \n";
        }
        void f2()
        {
        cout<<"base f2 \n";
        }
};
class derived:public base
{
public:
        virtual void f1()
        {
        cout<<"derived f1 \n";
        }
        void f2()
        {
        cout<<"derived f2 \n";
        }
};
int main()
{
base *bptr;
base b;
derived d;
bptr=&b;
bptr->f1();
bptr->f2();
bptr=&d;
bptr->f1();
bptr->f2();
bptr=&d;
bptr->f1();
bptr->f2();
return 0;
}
Previous                             Home                               Next

No comments:

Post a Comment