In Review 803
On lines 307 and 308, the driver program creates a list of two types of Partobjects 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 contra-
variance, it would...
CAUTION This listing will not compile!
LISTINGR3.2 Contravariance
0: #include <iostream>
1: using namespace std;
2: class Animal
3: {
4: public:
5: virtual void Speak()
6: { cout << “Animal Speaks” << endl; }
7: };
8:
9: class Dog : public Animal
10: {
11: public:
12: void Speak() { cout << “Dog Speaks” << endl; }
13: };
14:
15:
16: class Cat : public Animal
17: {
18: public:
19: void Speak() { cout << “Cat Speaks” << endl; }
28 0672327112_w3_wir.qxd 11/19/04 12:30 PM Page 803