Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!wuarchive!kuhub.cc.ukans.edu!markv From: markv@kuhub.cc.ukans.edu Newsgroups: comp.lang.c++ Subject: Re: Inheritance vs. Composition Message-ID: <22269.25dc000d@kuhub.cc.ukans.edu> Date: 16 Feb 90 19:28:45 GMT References: Followup-To: comp.lang.c++ Organization: University of Kansas Academic Computing Services Lines: 46 All philosophy and religion aside (I know, SACRILEGE...) heres my thoughts on Inheritance -vs- composition. I am working one my first "serious" C++ project (personal interest). It is a program that uses 2D structured graphics which seemed like a natural. I kind of like the idea that programming reality makes a lot of the decisions. Kind of a form follows function argument. I am building this program around and object called Object. An Object is defined (via virtual functions) to have a common interface so it can Show() itself, Erase(), do various geometric transformations (like FlipV() and FlipH()). All of these Objects are stored in a linked list, so an Object also can Insert(), Delete(), etc itself too. Then Object is derived into things like Line, Circle, Box, etc. Now, to make my life easy I need to be able to pass pointers to an Object all over, and I don't know until runtime what kind of Object it will be so Inheritance (and virtual functions) are the only solution. On the other hand I also have objects like a Point and a LineSeg etc that are used to build the specific representation of an Object. I know which of these are going into an Object (like a FreePolygon) at compile time. So one (simplisitic but pratical argument) is that when you need to have common "methods" that must be resolved at run-time (ie: Late binding) you must (in C++) use Inheritance. On the otherhand when you know at compile time what you need to be dealing with, composition works. Being new to C+ (and OOP) this may be a simplistic explanation, but it is a rationale that works for me. This whole argument first took shape back (a month ago) when I first started working with C++ and I got confused with the difference between overloaded functions, and vitual functions. Well, have fun, -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Mark Gooderum Only... \ Good Cheer !!! Academic Computing Services /// \___________________________ University of Kansas /// /| __ _ Bix: markgood \\\ /// /__| |\/| | | _ /_\ makes it Bitnet: MARKV@UKANVAX \/\/ / | | | | |__| / \ possible... Internet: mark@kuhub.cc.ukans.edu ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~