• No results found

Virtual Functions

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;

};// end of class definition int customerror::errortype()

cout << "you will never see this because it will be overridden!\n";

};//end of divisionbyzero class definition divisionbyzero::divisionbyzero(int errortype)

cout << "You cannot have divide by zero \n";

}

class filenotfound: public customerror {

Virtual Functions

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;

cout << "The answer is "<< quotient << endl;

fstream myfile;

}// end of filenotfound catch catch(...)

{

cout << "Something bad happened \n";

}//end of final catch return 0;

Virtual Functions

}// end of main

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

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.

Summary

Summary

This chapter introduced you to inheritance. Inheritance is a powerful feature that object-oriented programming offers to you. Learning to use it is critical. Many modern software products have an object model hierarchy that you can access.

These hierarchies are dependent on one object inheriting from another. In this chapter you also were introduced to the various relationships found in inherited classes. These relationships and the associated terminology are of great important in object-oriented programming.

Review Questions

Review Questions

1. What is inheritance?

2. How do you call an overloaded constructor in the base class?

3. What is another word for base class?

4. How would you declare class a so that it inherits from class b?

5. What is another term for derived class?

6. What is a protected variable?

7. What is another word for a function that is part of a class?

8. Are private members inherited?

9. When you have a derived class, which constructor is executed first . . . the derived class constructor or the base class constructor?

10. When one class contains another class, what type of relationship is there?

Chapter 12: Advanced Object-Oriented Concepts

Chapter 12: Advanced Object-Oriented Concepts

Download CD Content

Chapter 11 introduced you to inheritance, the process whereby one class gets a copy of the public and protected methods of another class. In the previous two chapters, you have seen three of the four principle concepts of object-oriented theory. You have seen abstraction, encapsulation, and inheritance. This chapter will show you how to use the fourth principle, that of polymorphism. The word polymorphism literally means “many forms.” It simply means that once you have inherited a function, you can override the function you inherited and change it.

Polymorphism

When a class inherits a base class, it receives all the base class’s public and protected methods and properties.

However, it does not have to take everything it gets as is. The derived class can override any function to suit the needs of the derived class. In other words, you are not stuck with the functions you inherit. You can override them.

Overwriting a function is not the same as overloading a function. When you overload a function it must have different parameters (either a different number or different types). When you override a function it should have the same parameters, but the actual code in the function will be different. The function declaration (the name, return type, and parameters) is called the interface; the actual code in the function is called the implementation. Thus, your interface will be the same when you override an inherited function, but its implementation will be the same. The following example will clarify this.

Example 12.1

Step 1: Enter the following code into you favorite text editor. Save it as 12-01.h.

class baseclass

cout << "Hey this is the base class!\n";

}

cout << "Hey this is in the child class!!\n";

}

Step 2: Enter this code into your favorite text editor and save it as 12-01.cpp.

#include "test.h"

#include <iostream>

using namespace std;

int main() {

Chapter 12: Advanced Object-Oriented Concepts

childclass myclass;

myclass.testfunction();

return 0;

}

Step 3: Compile your code.

Step 4: Run your application, you should see something similar to what is shown in Figure 12.1.

Figure 12.1: Overwriting an inherited function.

This example illustrates the basics of polymorphism. You can change the form of any function you inherit. As you can see in Example 12.1, the function that is called is the one in the derived class, not the one in the base class.

Although this does show you the basics of polymorphism, it does not show you the power of polymorphism. This comes into play when you have many derived classes, and each needs to implement a particular method in a slightly different way. Then, each class can overload the method inherited and have the method do what they wish. Consider this example.

Example 12.2

Step 1: Enter the following code into your favorite text editor and save it as 12-02.h.

class baseclass

cout << "Hey this is the base class!\n";

cout << price * .075f;

cout << "Hey this is the child class 1\n";

cout << price * .010f << endl;

}

class childclass2:public baseclass