Week 3. In Review

The following program (as shown in Listing R3.1) brings together many of the advanced techniques you’ve learned during the past three weeks of hard work. Week 3 in Review provides a template-based linked list with exception handling. Examine it in detail; if you understand it fully, you are a C++ programmer.

Caution

If your compiler does not support templates, or if your compiler does not support try and catch, you will not be able to compile or run this listing.

Listing R3.1. Week 3 in Review Listing

Image

Image

Image

Image

Image

Image

Image

Image

Image

Image

Image

Image

Image

(0)Quit (1)Car (2)Plane: 1
New PartNumber?: 2837
Model Year? 90

(0)Quit (1)Car (2)Plane: 2
New PartNumber?: 378
Engine Number?: 4938

(0)Quit (1)Car (2)Plane: 1
New PartNumber?: 4499
Model Year? 94

(0)Quit (1)Car (2)Plane: 1
New PartNumber?: 3000
Model Year? 93

(0)Quit (1)Car (2)Plane: 0

Part Number: 378
Engine No. 4938

Part Number: 2837
Model Year: 90

Part Number: 3000
Model Year: 93

Part Number 4499
Model Year: 94

Image

The Week 3 in Review listing modifies the program provided in Week 2 to add templates, ostream processing, and exception handling. The output is identical.

On lines 36–40, a number of exception classes are declared. In the somewhat primitive exception handling provided by this program, no data or methods are required of these exceptions; they serve as flags to the catch statements, which print out a very simple warning and then exit. A more robust program might pass these exceptions by reference and then extract context or other data from the exception objects in an attempt to recover from the problem.

On line 45, the abstract base class Part is declared exactly as it was in Week 2. The only interesting change here is in the nonclass member operator<<(), which is declared on lines 70–74. Note that this is neither a member of Part nor a friend of Part, it simply takes a Part reference as one of its arguments.

You might want to have operator<< take a CarPart and an AirPlanePart in the hopes that the correct operator<< would be called, based on whether a car part or an airplane part is passed. Because the program passes a pointer to a part, however, and not a pointer to a car part or an airplane part, C++ would have to call the right function based on the real type of one of the arguments to the function. This is called contravariance and is not supported in C++.

You can only achieve polymorphism in C++ in two ways: function polymorphism and virtual functions. Function polymorphism won’t work here because in every case you are matching the same signature: the one taking a reference to a Part.

Virtual functions won’t work here because operator<< is not a member function of Part. You can’t make operator<< a member function of Part because you want to invoke


cout << thePart

and that means that the actual call would be to cout.operator<<(Part&), and cout does not have a version of operator<< that takes a Part reference!

To get around this limitation, the Week 3 program uses just one operator<<, taking a reference to a Part. This then calls Display(), which is a virtual member function, and thus the right version is called.

On lines 130–143, Node is defined as a template. It serves the same function as Node did in the Week 2 Review program, but this version of Node is not tied to a Part object. It can, in fact, be the node for any type of object.

Note that if you try to get the object from Node, and there is no object, this is considered an exception, and the exception is thrown on line 175.

On lines 182 and 183, a generic List class template is defined. This List class can hold nodes of any objects that have unique identification numbers, and it keeps them sorted in ascending order. Each of the list functions checks for exceptional circumstances and throws the appropriate exceptions as required.

On lines 307 and 308, the driver program creates a list of two types of Part objects and then prints out the values of the objects in the list by using the standard streams mechanism.

FAQ

In the comment above line 70, you mention that C++ does not support contravariance. What is contravariance?

Answer: Contravariance is the ability to assign a pointer to a base class to a pointer to a derived class.

If C++ did support contravariance, you could override the function based on the real type of the object at runtime. Listing R3.2 won’t compile in C++, but if it supported contravariance, it would…

Caution

This listing will not compile!

Listing R3.2. Contravariance

Image

What you can do, of course, is to use a virtual function as shown in Listing R3.3, which partially solves the problem.

Listing R3.3. Using Virtual Functions

Image

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.76.72