Fruit orange;
Fruit * p;
p=&apple;
p->peel();
p=&orange;
p->peel();
}
At runtime, the results will be:
% a.out
peeling an apple
peeling a base class fruit
Software Dogma
Deep Thought—Polymorphism Has Something in Common with Interposing
Polymorphism and interposing both allow multiple functions to have the same one
identifier. Interposing is a bit of a blunt instrument, as it binds every occurrence of the name
to the same one definition at compile time. Polymorphism is a little more discerning, as it
makes the binding decision on an object-by-object basis at runtime.
Other Corners of C++
There are plenty of smaller C++ concepts not covered in this brief review of the high points. And
there are plenty more detailed rules that apply to the concepts mentioned here. However, if you master
the material in this chapter you will have a basic understanding of the OOP concepts and how they are
expressed in C++. You will have enough of a head start to begin writing experimental C++ programs.
And that is the real way, the only way, to learn any programming language.
Among the concepts not covered here, C++ also has:
- Exceptions: borrowed from Ada and also from Clu (an experimental language developed at
MIT, in which the key idea is a "cluster"). These are for changing the flow of control for
error-handling. They simplify some kinds of error-handling by automatically diverting
processing to a part of the program that can process the error. - Templates: support parameterized types. Like the class/object relationship, a
template/function can be thought of as providing a "cookie-cutter" approach to algorithms.
Once you get the basic algorithm down, you can plug different types into it. They are similar
to the generic facility in Ada and parameterized modules in Clu. They have quite
complicated semantics. This code: