Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!ut-emx!slcs.slb.com!asc.slb.com!hargrove From: hargrove@asc.slb.com (Jim Hargrove) Newsgroups: comp.object Subject: C++ vs. C Message-ID: <1991Apr24.125926.5146@asc.slb.com> Date: 24 Apr 91 12:59:26 GMT Sender: news@asc.slb.com Organization: Schlumberger Austin Systems Center Lines: 100 Nntp-Posting-Host: nobelium This note is prompted by several postings by Scott Guthery, beginning with The Emperor Strikes Lethe. I am struck by how different SBG's experience with object oriented programming differs from my own. I began programming in C++ about 18 months ago. Prior to that, I worked mainly in C, though I have used many different languages over the years. I believe that I am writing better programs today after switching to C++. Here is an example: Several years ago, I was faced with the problem of writing display routines for a number of different types of "objects." Each of these objects could be a block of text, an appointment page, a calendar, or a "folder" containing other objects. I wrote the routine as follows: void DisplayObject(Object * Obj, Window * W) { switch(ClassOf(Obj)) { case Text: DisplayText(Obj,W); break; case ApptPage: DisplayAppt(Obj,W); break; case Calendar: DisplayCalendar(Obj,W); break; case Folder: DisplayFolder(Obj,W); break default: Error("Invalid Object"); } } That is a nice, simple routine. Of course we can improve it quite easily, but there is little incentive to do so. This works; it's easy to understand; and so long as we don't invent a large number of objects, it is relatively easy to maintain. Today, I would write the same routine in C++ as Obj->Display(W); which is better both technically and aesthetically. I could have written the routine the same way five years ago. I could have created a table of routines containing the address of the function to call to display each kind of object. The ClassOf function provides a convenient index into the table. The routine would be simply: (*DisplayFn[ClassOf(Obj)])(Obj,W); In ANSI C, I could even dispense with the (*) notation. So, I have to agree with Scott. There is really nothing new here. The syntax is a bit different, granted, but we probably don't really have a "paradigm shift." Or do we? The language we speak/write/code in influences the way we think. In C, I was used to thinking of switch statements. Indeed, this style of programming is taught extensively. Look at any book on Window programming, for example. But the use of a virtual function is much better. It is easier to maintain, since I never have to modify a DisplayObject routine at all. Moreover, it is more efficient. Notice that the code represented by the original DisplayObject routine has virtually disappeared! All of the code necessary to invoke the proper routine is generated by the compiler. In C++, the use of a virtual function in this case is obvious. If I were to code the routine using a switch statement [and dynamic type checking :-)] it would look very strange. So, the mere use of an object oriented langauge has led me to a better overall program. The fact that the technique used, a jump vector, has been around since the days of the earliest computers is largely irrelevant. C++ appears to be a significantly better tool than C. +--------------------------------------------------------------------+ |These opinions are my own, of course. But you know that. | +--------------------------------------------------------------------+ |Internet: hargrove@asc.slb.com | |Compuserve: ID 74000,1010 | |Mail: Schlumberger Austin Systems Center | | P.O. Box 200015 | | Austin, TX 78720-0015 | +--------------------------------------------------------------------+ |Every great scientific truth goes through three stages: | | First, people say it conflicts with the Bible. | | Next they say it had been discovered before. | | Lastly, they say they always believed it. | |--Jean Louis Agassiz (1807-1883) | +--------------------------------------------------------------------+ -- jwh -- -- jwh