Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!cs.uoregon.edu!ns.uoregon.edu!milton!ogicse!pdxgate!parsely!percy!nosun!hilbert!jeff From: jeff@hilbert.uucp (Jeff Freedman) Newsgroups: comp.object Subject: Re: Readability of Ada Message-ID: <1991May3.181917.7553@hilbert.uucp> Date: 3 May 91 18:19:17 GMT References: <1991Apr25.170356.21237@odin.diku.dk> <1991Apr27.012355.29911@neon.Stanford.EDU> Organization: Cypress Semiconductor Northwest, Beaverton Oregon Lines: 47 >In article jls@rutabaga.Rational.COM (Jim Showalter) >writes: > Consider the shapes example. Suppose that the given example goes > out in binary form, and arrives at my site, and I want to add a new > shape. Suppose that the initial shapes were limited to triangles and > squares. Suppose that I now add a Circle, which has a new method > defined for it that does NOT apply to triangles or squares and which > was never previously defined in the base class for shapes--radius. > This method returns the radius as some floating point number from 0 > to whatever. >(I used integers to save typing - ignore the details). > Now, I want to take a heterogeneous list of shapes, including triangles, > circles, and squares, and I want to iterate over the list and print > out all of the radii. This is easy under the library which we're using. I would be able to write, without any changes to the Shapes class or methods, the following: class Circle : public Shape { float radius; public: ..... (constructor, destructor, etc.) float get_radius() { return radius; } }; /* * Print the radii of all the circles in a list of shapes. */ void Print_radii(Shape_list *shapes) { // Iterate through list. Shape_list_iterator next(shapes); Shape *each; while (next(each)) { // A circle? Print radius if so. Circle *circle = AS_A(each, Circle); if (circle) cout << circle->get_radius() << '\n'; } } Our library and environment were designed in-house, but I believe that ET++ and NIH provide similar facilities.