Program to Illustrates Virtual Function


#include<iostream.h>
#include<conio.h>
class Base
{
public :
     void display( )
    {
           cout<<"\n Display Base ";
     }
     virtual void show( )
    { 
           cout<<"\n Show Base ";
     }
};
class Derived : public Base
{
            public :
                  void display( )
                  {
                         cout<<"\n Display Derived ";
                   }
        void show( )
       { 
           cout<<"\n Show Derived ";
     }
};
void main( )
{
Base B;
Derived D;
Base *bptr;

cout<<"\n bptr points to Base  ";
bptr = &B;
bptr -> display( );                   //calls Base version
bptr -> show( );                      // calls Base version

cout<<" \n\n bptr points to Derived  ";
bptr = &D;
bptr -> display( );                  //calls Base version
bptr -> show( );                    // calls Derived version

                getch( );
}

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|