Virtual Functions


You are probably wondering what a virtual function is. A virtual function is a function that must be overridden. You declare a virtual function in a base class when you want all the derived classes to have that function, but you want to force them to override it with their own specific implementation. Put another way, if you declare a virtual function in a base class, all the derived classes will inherit it, but they cannot use it as is. They must override it with their own implementation.

Declaring a function as virtual is very easy. Simply precede the function name with the keyword virtual. Perhaps an example would be helpful. For this example, we will work with an error class example with inheritance, and simply add a virtual function to it.

Example 11.7

Step 1: Open your favorite text editor and write in the following code. Save the file as 11-07.h.

const int DIVISIONBYZERO = 1; const int FILENOTFOUND =2; //************define the base class********************* class customerror { protected:      int type; public:      int errortype();      virtual void errormsg(); };// end of class definition int customerror::errortype() {  return type; }// end of error type void customerror::errormsg() {  cout << "you will never see this because it will be  overridden!\n";   }// end of errormsg //*******end of base class********************* class divisionbyzero: public customerror { public:      divisionbyzero(int);      void errormsg(); };//end of divisionbyzero class definition divisionbyzero::divisionbyzero(int errortype) {      type = errortype; } void divisionbyzero::errormsg() {  cout << "You cannot have divide by zero \n"; } class filenotfound: public customerror { public:       filenotfound(int);      void errmsg(); }; filenotfound::filenotfound(int errortype) { type = errortype; } void filenotfound::errmsg() {  cout << "Cannot find that file!\n"; }

Step 2: Type this code into your favorite text editor and save it as 11-07.cpp.

#include "customerror.h" #include <fstream> #include <iostream> using namespace std;   int main() {  divisionbyzero de(DIVISIONBYZERO);  filenotfound fe(FILENOTFOUND);  float dividend, divisor, quotient;  try  {   cout << "enter a number \n";   cin >> dividend;   cout << "enter another number \n";   cin >> divisor;        if (divisor==0)        { throw de;        } quotient = dividend/divisor; cout << "The answer is "<< quotient << endl; fstream myfile; myfile.open("c:\\something.txt",ios::in);    if(!myfile) {  throw fe; } }// end of try catch(divisionbyzero de) { de.errormsg(); } catch(filenotfound fe) { fe.errormsg(); }// end of filenotfound catch catch(...) {  cout << "Something bad happened \n"; }//end of final catch return 0;    }// end of main

Step 3: Compile and execute your code. You should see something like what is shown in Figure 11.7.

click to expand
Figure 11.7: Virtual functions.

Now that you have seen how a virtual function works, you might be wondering why one would wish to use them. Could you not simply write individual functions in each of the derived classes and skip the virtual function in the base class? The answer is “yes, you could.” However, the virtual function accomplishes a few goals. First, it forces anyone making a derived class from this base class to create an implementation of that function. This guarantees that such a programmer using classes derived from your base class cannot forget to implement this function. Furthermore, it ensures that all such functions will have the same basic declaration line.




C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

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