Creating Virtual Functions: The Virtual Keyword

 < Day Day Up > 



Purpose of Virtual Functions

The following excerpt comes straight from the ANSI C++ standard, Section 10.3 on page 168:

“Virtual functions support dynamic binding and object-oriented programming. A class that declares or inherits a virtual function is called a polymorphic class.”

Declaring and Using Virtual Functions

To declare a virtual function simply add the keyword virtual to the function declaration. Example 13.15 gives the source code for a slightly modified Foo class declaration originally given in example 13.10.

Listing 13.15: foo.h

start example
  1  #ifndef FOO_H  2  #define FOO_H  3  4  class Foo{  5   public:  6      Foo(int _i = 0);  7      virtual ~Foo();  8      virtual int getI();  9   private: 10      int i; 11  }; 12  #endif
end example

The only difference between this version of Foo and the last is the addition of the keyword virtual to the getI() function declaration. This now makes getI() a virtual function and the Foo class a polymorphic class. No other changes are required to the previous set of example files and the main.cpp file shown in example 13.14 can be recompiled and run to produce the output shown in figure 13-10. Notice now that when the getI() function is called via the base class pointer that the Derived version of getI() executes. This is an example of polymorphic behavior.

click to expand
Figure 13-10: Results of Running Example 13.14 After Modifying foo.cpp

Virtual Destructors

You have seen the keyword virtual added to destructors in this book before being formally discussed here. Virtual destructors are necessary in that they ensure that derived class object destructors will be called when the base class pointer is deleted. The best way to demonstrate the importance of making base class destructors virtual is to show you what happens when they are not. Example 13.16 shows the Foo class declaration once again modified so that the virtual keyword is removed from the destructor declaration. When example 13.14 is again compiled and run it produces different results as shown in figure 13-11.

Example 13.16: foo.h

start example

click to expand

end example

click to expand
Figure 13-11: Results of Running Example 13.14 After Removing the virtual Keyword from Foo Class Destructor Declaration

Compare figures 13-10 and 13-11 carefully while studying example 13.14. Before the program exits the pointers are deleted. The first pointer to be deleted is the Foo base class pointer. When the Foo destructor is declared virtual the DerivedFoo object’s destructor is called before the Foo object’s destructor. This is shown happening in figure 13.10.

However, when the Foo destructor is not declared to be virtual, the DerivedFoo object’s constructor is not called when the Foo pointer is deleted. This is shown happening in figure 13-11. Since a DerivedFoo object was created in memory and its destructor was not called when the base class pointer was deleted, this results in memory leak.

Quick Review

Virtual functions support dynamic binding and object-oriented programming. If a base class function is declared to be virtual and a derived class declares a function with the same function signature as the virtual base class function, the derived class function overrides the base class function and will be called via a base class pointer if the base class pointer points to a derived class object.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net