Polymorphism - (20 minutes)
This problem is not from your textbook.
In Lecture 31, we talked about polymorphism. Label each of the following C++ examples as using ad hoc or universal polymorphism (specify overloading, coercion, inclusion, or parametric) and explain your decision. If you're not sure exactly how C++ implements a particular form of polymorphism (whether it's overloaded or coerced, for example), take your best guess and explain it.
double sqrt( double ); float f = 1.0f; //The 'f' in 1.0f means to interpret 1.0 as a float and not a double double d = 1.0; //The 1.0 here will be interpreted as a double f += sqrt( f ); d += sqrt( d );
There are two uses of polymorphism in the above code.
class A { ...};
class B: public A { ... };
class C: public A { ... };
A a;
B b;
C c;
A array_of_A[10];
array_of_A[0] = a;
array_of_A[1] = b;
array_of_A[2] = c;
#include <vector.h> ... vector<int> v(10, 0);
char c = 'A'; int i = 10; int *p = &i; (p++) + (i++) + (c++);
There are also two polymorphic operators in this last example. (For this one, consider polymorphic operations in the last expression, not in the three declarations.)
Be careful with this one--the last line evaluates to a pointer.
Instructions on submitting homework is here
Last updated at 9:53 am on Friday, August 27, 2004.