Friday, 8 March 2013

C++ Virtual Functions


C++ Virtual Functions

http://www.exforsys.com/tutorials/c-plus-plus/c-virtual-functions.html

What are Virtual Functions?

Virtual, as the name implies, is something that exists in effect but not in reality. The concept of virtual function is the same as a function, but it does not really exist although it appears in needed places in a program. The object-oriented programming languageDescription: http://images.intellitxt.com/ast/adTypes/icon1.png C++ implements the concept of virtual function as a simple member function, like all member functions of the class.
The functionality of virtual functions can be overridden in its derived classes. The programmer must pay attention not to confuse this concept with function overloading. Function overloading is a different concept and will be explained in later sections of this tutorial. Virtual function is a mechanism to implement the concept of polymorphism (the ability to give different meanings to one function).

Need for Virtual Function:

The vital reason for having a virtual function is to implement a different functionality in the derived class.
For example: a Make function in a class Vehicle may have to make a Vehicle with red color. A class called FourWheeler, derived or inherited from Vehicle, may have to use a blue background and 4 tires as wheels. For this scenario, the Make function for FourWheeler should now have a different functionality from the one at the class called Vehicle. This concept is called Virtual Function.

Properties of Virtual Functions:

·         Dynamic Binding Property:
Virtual Functions are resolved during run-time or dynamic binding. Virtual functions are also simple member functions. The main difference between a non-virtual C++ member function and a virtual member function is in the way they are both resolved. A non-virtual C++ member function is resolved during compile time or static binding. Virtual Functions are resolved during run-time or dynamic binding
·         Virtual functions are member functions of a class.
·         Virtual functions are declared with the keywordDescription: http://images.intellitxt.com/ast/adTypes/icon1.png virtual, detailed in an example below.
·         Virtual function takes a different functionality in the derived class.

Declaration of Virtual Function:

Virtual functions are member functions declared with the keyword virtual.
For example, the general syntax to declare a Virtual Function uses:
Sample Code
1.  class class_name //This denotes the base class of C++ virtual function
2.  { 
3.  public: 
4.  virtual void member_function_name() //This denotes the C++ virtual function
5.  { 
6.  ... 
7.  ... 
8.  } 
9.  };
Copyright exforsys.com


Referring back to the Vehicle example, the declaration of Virtual function would take the shape below:
Sample Code
1.  class Vehicle //This denotes the base class of C++ virtual function
2.  { 
3.  public: 
4.  virtual void Make() //This denotes the C++ virtual function
5.  { 
6.  cout << "Member function of Base Class Vehicle Accessed" << endl; 
7.  } 
8.  };
Copyright exforsys.com


After the virtual function is declared, the derived class is defined. In this derived class, the new definition of the virtual function takes place.
When the class FourWheeler is derived or inherited from Vehicle and defined by the virtual function in the class FourWheeler, it is written as:
Sample Code
1.  #include <iostream>
2.  using namespace std; 
3.  class Vehicle   //This denotes the base class of C++ virtual function
4.  { 
5.  public: 
6.          virtual void Make()   //This denotes the C++ virtual function
7.          { 
8.                  cout << "Member function of Base Class Vehicle Accessed" << endl; 
9.          } 
10.     }; 
11.      
12.     class FourWheeler : public Vehicle 
13.     { 
14.     public: 
15.             void Make() 
16.             { 
17.                     cout << "Virtual Member function of Derived class FourWheeler Accessed" << endl; 
18.             } 
19.     }; 
20.      
21.     void main() 
22.     { 
23.             Vehicle *a, *b; 
24.             a = new Vehicle(); 
25.             a->Make(); 
26.             b = new FourWheeler(); 
27.             b->Make(); 
28.     }
Copyright exforsys.com


In the above example, it is evidenced that after declaring the member functions Make() as virtual inside the base class Vehicle, class FourWheeler is derived from the base class Vehicle. In this derived class, the new implementation for virtual function Make() is placed.
Output:
Description: http://www.exforsys.com/images/cpp/33_1.jpg
The programmer might be surprised to see the function call differs and the output is then printed as above. If the member function has not been declared as virtual, the base class member function is always called because linking takes place during compile time and is therefore static.
In this example, the member function is declared virtual and the address is bounded only during run time, making it dynamic binding and thus the derived class member function is called.
To achieve the concept of dynamic binding in C++, the compiler creates a v-table each time a virtual function is declared. This v-table contains classes and pointers to the functions from each of the objects of the derived class. This is used by the compiler whenever a virtual function is needed.

 

 

 

 

C++ Pure Virtual Function and Base Class

In this C++ tutorial, you will learn about pure virtual functions, declaration of a pure virtual function and virtual base class, virtual base class and how to implement a virtual base class, explained with examples.

What is a Pure Virtual Function:

A Pure Virtual Function is a Virtual function with no body.

Declaration of Pure Virtual Function:

Since pure virtual function has no body, the programmer must add the notation =0 for declaration of the pure virtual function in the base class.

General Syntax of Pure Virtual Function takes the form:

Sample Code
1. class class_name //This denotes the base class of C++ virtual function
2. { 
3. public: 
4. virtual void virtualfunctioname() = 0 //This denotes the pure virtual function in C++
5. };
Copyright exforsys.com


The other concept of pure virtual function remains the same as described in the previous section of virtual function.
To understand the declaration and usage of Pure Virtual Function, refer to this example:
Sample Code
1. #include <iostream>
2. using namespace std; 
3. class Exforsys 
4. { 
5. public: 
6.         virtual void example()=0; //Denotes pure virtual Function Definition
7. }; 
8.  
9. class Exf1:public Exforsys 
10.    { 
11.    public: 
12.            void example() 
13.            { 
14.                    cout << "Welcome"; 
15.            } 
16.    }; 
17.     
18.    class Exf2:public Exforsys 
19.    { 
20.    public: 
21.            void example() 
22.            { 
23.                    cout << "To Training"; 
24.            } 
25.    }; 
26.     
27.    void main() 
28.    { 
29.            Exforsys* arra[2]; 
30.            Exf1 e1; 
31.            Exf2 e2; 
32.            arra[0]=&e1; 
33.            arra[1]=&e2; 
34.            arra[0]->example(); 
35.            arra[1]->example(); 
36.    } 
37. Since the above example has no body, the pure virtual function example() is declared with notation =0 in the base class Exforsys. The two derived class named Exf1 and Exf2 are derived from the base class Exforsys. The pure virtual function example() takes up new definition. In the main function, a list of pointers is defined to the base class.
38. Two objects named e1 and e2 are defined for derived classes Exf1 and Exf2. The address of the objects e1 and e2 are stored in the array pointers which are then used for accessing the pure virtual function example() belonging to both the derived class EXf1 and EXf2 and thus, the output is as in the above example.
39. The programmer must clearly understand the concept of pure virtual functions having no body in the base class and the notation =0 is independent of value assignment. The notation =0 simply indicates the Virtual function is a pure virtual function as it has no body.
40. Some programmers might want to remove this pure virtual function from the base class as it has no body but this would result in an error. Without the declaration of the pure virtual function in the base class, accessing statements of the pure virtual function such as, arra[0]->example() and arra[1]->example() would result in an error. The pointers should point to the base class Exforsys. Special care must be taken not to remove the statement of declaration of the pure virtual function in the base class.

 

 


No comments:

Post a Comment