Sunday 21 April 2013

What Is Virtual Funtion?

Virtual Is a Keyword in C++, whenever it is Preceded by any Function known as Virtual function.


When Derive a class overrides the base class method by redefining the same function, then  if a client wants to access redefined the method from derived class through a pointer from base class object, then your function defined this function as Virtual function.Virtual function mainly used to restrict overriding.

 Syntax:
  class <class name1>
                 {

                           Public: Virtual return type function()
                                           {
                                            --------------
                                           ------------------
                                             --------------
                                           ------------------

                                           }
              };

               
              Class <class name2>:public <class name1>
            
             {

                           Public: return type function()
                                           {
                                            --------------
                                           ------------------
                                            --------------
                                           ------------------

                                           }
              };



Rules to Declared Virtual Function:


1. Every Virtual Function should be identified with a virtual keyword.

2. Every virtual function should be declared in the Public scope of base class.

3. If any base class contains at least one virtual function known as Virtual Base class, every virtual base class should be inherited at least in one class.

4. Virtual function of base class should be Re declared in the public scope of derived class.

5. Virtual function of both base and derived class should be called using based class pointer Object.

6. One Base class can contain any number of virtual function as well as normal function.

7. One virtual base class can be Inherited. In any number of another class.

8. Normal Function of virtual base class can be called using any type of derived class object.



0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More