Sunday, September 20, 2015

What is a virtual function? Describe with example.

Solution:-
Virtual Function:-
i. Same function use in different class with different bahaviours.
ii. Member function access using pointer methods.

Syntax:- virtual keyword return type function name ( parameter )

Example:-
#include< iostream.h >
#include< conio.h >
Class A
{
        public:
        virtual void study( )
        {
                  cout<< " study in school " ;
        }

};
class B : public A
{
        public:
        void study ( )
        {
                  cout<< " study in college " ;
        }
};
void main ( )
{
        A *ptr;
        B np;
        ptr = & np;        

        ptr study ( );
        getch( );
}