Path: utzoo!utgpu!news-server.csri.toronto.edu!torsqnt!geac!alias!bmcphers From: bmcphers@alias.UUCP (Brent McPherson) Newsgroups: comp.object Subject: Re: inheritance and `type loss' Message-ID: <1990Jul11.142439.26870@alias.uucp> Date: 11 Jul 90 14:24:39 GMT References: <1130012@gore.com> Sender: news@alias.uucp (USENET News) Organization: Alias Research Inc., Toronto ON Canada Lines: 49 In article <1130012@gore.com> jacob@gore.com (Jacob Gore) writes: >/ comp.object / sra@ecs.soton.ac.uk (Stephen Adams) / Jul 10, 1990 / > >The key is that roman is a subclass of integer, and not vice versa. > > [stuff omitted...] > >> (V+1).image "6" or "VI"? > >"VI", because a roman + an integer -> a roman >Why? A roman is an integer, so any place an integer can be used, a roman >can be used just as well. Thus, "a roman + an integer" automatically gets >the type of "a roman + a roman". > I agree with you on the first and last case but not the second. The second case is incorrect since you cannot automatically convert from a base class to a derived class. Here is a simple example that illustrates the point. Dog is a subclass of mammal (therefore a dog is a mammal). A dog is a mammal, so any place a mammal can be used, a dog can be used just as well. The fundamental flaw in the logic is that every mammal is a dog when only the reverse is true. The solution will vary depending on the facilities offered by your favorite OOPS language. The first solution is to provide an overloaded '+' operator that takes an integer as a second argument (to explicity define what happens in that case). The second solution (allowed by C++) is to define a conversion method to allow the compiler to convert the roman to an integer before performing the addition. ie. C++ first looks for a method/routine where the types of the arguments exactly match the formal parameters. If an exact match is not found, then the compiler will look for conversions to apply to the argument in order to generate an exact match. Finally, if all else fails, an error message is generated. > > >Jacob >-- >Jacob Gore Jacob@Gore.Com boulder!gore!jacob -- Brent McPherson bmcphers%alias@csri.toronto.edu