A simple program with inheritance

Examine the example program named VIRTUAL1.CPP for the basic program outline we will use for all discussion in this chapter.

Since this program has nothing to do with virtual functions, the name may be somewhat misleading. It is named VIRTUAL1.CPP because it is part of a series of programs intended to illustrate the use of virtual functions. The last program in this chapter will illustrate the proper use of virtual functions.

The first program is very simple and you will recognize it as being somewhat similar to the programs studied in the last chapter except that this program is greatly simplified in order to effectively instruct you in the use of a virtual function. You will notice that many of the methods from the last chapter have been completely dropped from this example for simplicity, and a new method has been added to the parent class, the method named message() in line 8.

Throughout this chapter we will be studying the operation of the method named message() in the base class and the derived classes.

For that reason, there is a method named message() in the car class as well as in the new class named boat in lines 27 through 32.

You will also notice that there is a lack of a method named message() in the truck class. This has been done on purpose to illustrate the use of the virtual method, or if you prefer, you can refer to it as a virtual function. You will recall that the method named message() from the base class is available in the truck class because the method from the base class is inherited with the keyword public included in line 19. You will also notice that the use of the keyword public in lines 12 and 27 actually do nothing because the only method available in the base class is also available in the derived classes. There are no methods actually inherited. Leaving the keyword in the header poses no problem however, so it will be left there for your study.

The method named message() in the base class and in the derived classes has been kept very simple on purpose. Once again, we are interested in the technique of the virtual method rather than a long complicated example.

The main program is as simple as the classes, one object of each of the classes is declared in lines 37 through 40 and the method named message() is called once for each object. The result of executing the program indicates that the method for each is called except for the object named semi, which has no method named message(). As discussed in the last chapter, the method named message() from the parent class is called and the data output to the monitor indicates that this did happen since it displays “Vehicle message” for the object named semi.

The data for the objects is of no concern in this chapter so all data is allowed to default to private type and none is inherited into the derived classes. Some of the data is left in the example program simply to make the classes look like classes. Based on your experience with C++ by now, you realize that the data could be removed since it is not used.

After you understand this program, compile and execute it to see if your compiler gives the same result of execution.