Wednesday, 6 February 2013

Virtual function in OOP

#include<iostream.h>
#include<conio.h>

class A
{
    public:

        virtual void show()           //this function enables us to execute different functions by the same function call
    {
    cout<<"parent class A:    ";
    }
};

class B:public A
{
    public:

        void show()
    {
    cout<<"child class B:    ";
    }
};

class C:public A
{
    public:

        void show()
    {
    cout<<"child class C:    ";
    }
};

        void main()

{
    A obj1;
    B obj2;
    C obj3;
    A * ptr;
    ptr = & obj1;          //ampercent is used to access address of variable and store in a pointer
    ptr -> show();
    ptr = & obj2;
   ptr -> show();
    ptr= & obj3;
    ptr -> show();
    getch();
}

2 comments:

  1. A virtual operate or virtual methodology is a vital a part of the polymorphic portion of object-oriented programming (OOP). A virtual operate permits a derived category to override functions in categories it inherits from – even once the derived category is forged because the style of object from that it inherits.
    The Equation

    ReplyDelete
    Replies
    1. Thanks!Henry jorden
      for sharing good and informative knowledge to others!
      keep sharing knowledge with other!

      Delete