rickers@drexel.UUCP (Rick Wargo) (10/09/87)
Here is somehting I wrote a long while ago to show some of the differences between these two languages. I thought it would contribute to the discussion... ..!drexel!rickers ------CUT HERE-------------CUT HERE-------------CUT HERE------------------ : to unbundle, "sh" this file -- DO NOT use csh : SHAR archive format. Archive created Fri Oct 9 15:08:16 EDT 1987 echo x - LinkedList.m sed 's/^X//' > LinkedList.m <<'+FUNKY+STUFF+' X#include "objc.h" X#include <stdio.h> X X= LinkedList : Object (Lists, Primitive) { X // instance variable declarations X id next; // pointer to next link X id head; // head of this linked list X id what; // what this link holds X} X X// factory methods X X+ new { X head = self = [super new]; X next = nil; X return self; X} X X+ create:data { X self = [self new]; X [self append:data]; X return self; X} X X X// instance methods X X- free { X [what free]; X [super free]; X} X X- insert:data { X id last = head; X id here = last->next; X id element; X X while (here && [here->what compare:data] < 0) { X last = here; X here = here->next; X } X X element = [LinkedList new]; X element->what = data; X element->next = here; X last->next = element; X return self; X} X X- append:data { X id here = head; X id element; X X while (here->next) X here = here->next; X X element = [LinkedList new]; X element->what = data; X element->next = nil; X here->next = element; X return self; X} X X- push:data { X id element = [LinkedList new]; X element->what = data; X element->next = head->next; X head->next = element; X} X X- remove:data { X id last = head; X id here = last->next; X id element; X X while (here && [here->what compare:data]) { X last = here; X here = here->next; X } X X if (here) { X last->next = here->next; X [here free]; X return data; X } else X return nil; X} X X- print { X id here = head->next; X X while (here) { X [here->what print]; X here = here->next; X } X printf("\n"); X return self; X} X X=: +FUNKY+STUFF+ echo '-rw-r--r-- 1 rickers 1434 Oct 9 14:41 LinkedList.m (as sent)' chmod u=rw,g=r,o=r LinkedList.m ls -l LinkedList.m echo x - Makefile sed 's/^X//' > Makefile <<'+FUNKY+STUFF+' XMAKE = /bin/make X Xall: X @echo Make-ing c++ sample program X $(MAKE) -f c++.mk X @echo Make-ing objc sample.program X $(MAKE) -f objc.mk +FUNKY+STUFF+ echo '-rw-r--r-- 1 rickers 133 Oct 9 14:44 Makefile (as sent)' chmod u=rw,g=r,o=r Makefile ls -l Makefile echo x - README sed 's/^X//' > README <<'+FUNKY+STUFF+' XHere are some do-nothing programs I wrote while I was trying to teach Xa class on the principles of Object-Oriented Programming. I think that Xit would be of great interest to the C++ vs. ObjC discussion to include Xthese, anyway, I have had multiple request for these, or at least to Xinclude them. X XThere really do not do anything much at all. I wanted to show how similar Xand dis-similar the languages are. They try to implement a linked list. XTry to get a feel for each. I have not looked at these for over a year. XPlease no flames. These are from my earlier days of programming. I also Xhave somewhere a much better example of the differences between the two Xlanguages, but it a lot more source than the stuff I am posting. If Xanyone would like to see it, just leave me a message. What this other Xsource does is a menu-driven example to the Rectangle and Point classes, Xvery, very similar to those from the Objective-C class library. These Xexamples really show the difference between STATIC and DYNAMIC binding, Xand why Objective-C really makes a difference here. X XOne last thing that I have to offer are the class notes in a bullet-style Xformat (created with that wonderful Macintosh program: More). These notes Xbriefly describe a lot of different areas (or point you in a certain Xdirection) about many areas of o-o programming. Free to all who ask... X XOh, one more note. I have re-written the C++ script so that it looks Xfor C++ files ending in .C instead of .c (yuck!) Sorry, but you'll have to Xmake the appropriate changes. X X X Rickers X ..!drexel!rickers X +FUNKY+STUFF+ echo '-rw-r--r-- 1 rickers 1585 Oct 9 15:07 README (as sent)' chmod u=rw,g=r,o=r README ls -l README echo x - Stuff.m sed 's/^X//' > Stuff.m <<'+FUNKY+STUFF+' X#include "objc.h" X#include <stdio.h> X X= Stuff : Object (Lists, Primitive) X { int value; char *name; } X Xchar *deflt = "default"; X X X// factory methods X X+ new { X self = [super new]; X value = 0; X name = deflt; X return self; X} X X+ value:(int)aValue { X self = [self new]; X value = aValue; name = deflt; X return self; X} X X+ name:(char*)aName { X self = [self new]; X name = aName; value = 0; X return self; X} X X+ value:(int)aValue name:(char*)aName { X self = [self new]; X value = aValue; name = aName; X return self; X} X X// instance methods X X- name:(char*)aName { X name = aName; X return self; X} X X- value:(int)aValue { X value = aValue; X return self; X} X X- value:(int)aValue name:(char*)aName { X value = aValue; name = aName; X return self; X} X X- (int)compare:anObject { X return value - anObject->value; X} X X- printOn:(IOD)anIOD { X fprintf(anIOD, "%5d: %s\n", value, name); X return self; X} X X=: +FUNKY+STUFF+ echo '-rw-r--r-- 1 rickers 873 Oct 9 14:41 Stuff.m (as sent)' chmod u=rw,g=r,o=r Stuff.m ls -l Stuff.m echo x - c++.mk sed 's/^X//' > c++.mk <<'+FUNKY+STUFF+' X.SUFFIXES: X.SUFFIXES: .o .C X XSAMPOBJ= sample.o llist.o X X.C.o: X CC -c $< X Xsample: $(SAMPOBJ) llist.h X CC -o c++.sample $(SAMPOBJ) X +FUNKY+STUFF+ echo '-rw-r----- 1 rickers 130 Oct 9 14:45 c++.mk (as sent)' chmod u=rw,g=r,o= c++.mk ls -l c++.mk echo x - llist.C sed 's/^X//' > llist.C <<'+FUNKY+STUFF+' X#ifndef STREAMH X#include <stream.h> X#endif X X#include "llist.h" X X Xvoid llist::kill() X{ X llist* last = head->next; X llist* here = last; X X while (last) { X here = here->next; X delete last; X last = here; X } X} X X Xvoid llist::insert(void* a) X{ X llist* last = head; X llist* here = last->next; X X while (here && less(here->what, a)) { X last = here; X here = here->next; X } X X /* if a and here->what are the same then it is found */ X llist* item = new llist; X item->what = a; X item->next = here; X last->next = item; X} X X Xvoid llist::append(void* a) X{ X llist* here = head; X X while (here->next) X here = here->next; X X here->next = new llist; X here->next->what = a; X} X X Xvoid llist::push(void* a) X{ X llist* b = new llist; X b->what = a; X b->next = head->next; X head->next = b; X} X X Xint llist::remove(void* a) X{ X llist* last = head; X llist* here = last->next; X X while (here && !equal(here->what, a)) { X last = here; X here = here->next; X } X X if (here) { X last->next = here->next; X delete here; X return 1; X } else /* did not find it, return failure */ X return 0; X} +FUNKY+STUFF+ echo '-rw-r----- 1 rickers 1056 Oct 9 14:40 llist.C (as sent)' chmod u=rw,g=r,o= llist.C ls -l llist.C echo x - objc.mk sed 's/^X//' > objc.mk <<'+FUNKY+STUFF+' X.SUFIXXES: X.SUFFIXES: .o .m X XSAMPOBJ= LinkedList.o Stuff.o sample.o X X.m.o: X objcc -c -q $< X Xsample: $(SAMPOBJ) X objcc -o objc.sample $(SAMPOBJ) X Xclean: X @rm -f $(SAMPOBJ) X @rm -f [cp]_* +FUNKY+STUFF+ echo '-rw-r--r-- 1 rickers 186 Oct 9 14:45 objc.mk (as sent)' chmod u=rw,g=r,o=r objc.mk ls -l objc.mk echo x - sample.C sed 's/^X//' > sample.C <<'+FUNKY+STUFF+' X#include <stream.h> X#include <string.h> X#include "llist.h" X Xstruct stuff { X int value; X char* name; X X stuff() { name = "default"; } X stuff(int a) { value = a; name = "default"; } X stuff(char* a) { name = a; } X stuff(int a, char* b) { value = a; name = b; } X}; X X Xstruct sample : llist { X void insert(stuff* a) { llist::insert((void*)a); } X void append(stuff* a) { llist::append((void*)a); } X void push(stuff* a) { llist::push((void*)a); } X void print(); X int remove(stuff* a) { return llist::remove((void*)a); } X int less(void*, void*); X int equal(void*, void*); X X sample() { } X sample(stuff* a) : ((void*)a) { } X ~sample() { } X}; X X Xint sample::less(void* a, void* b) X{ X stuff* aa = (stuff*)a; X stuff* bb = (stuff*)b; X return aa->value < bb->value; X} X X Xint sample::equal(void* a, void* b) X{ X stuff* aa = (stuff*)a; X stuff* bb = (stuff*)b; X return aa->value == bb->value; X} X X Xvoid sample::print() X{ X llist* p; X stuff* a; X X p = head->next; X while (p) { X a = (stuff*)p->what; X cout << form("%5d: %s\n", a->value, a->name); X p = p->next; X } X} X X Xmain(int argc, char **argv) X{ X sample mylist; X char which = **++argv; X stuff *thing; X X if (argc%2) { X cerr << "usage: sample a|i|p [value name] ...\n"; X exit(2); X } X X while (argc -= 2) X switch (which) { X case 'a': X thing = new stuff(atoi(*++argv), *++argv); X mylist.append(thing); X break; X case 'i': X thing = new stuff(atoi(*++argv), *++argv); X mylist.insert(thing); X break; X case 'p': X thing = new stuff(atoi(*++argv), *++argv); X mylist.push(thing); X break; X } X X cout << "list...\n"; X mylist.print(); X} +FUNKY+STUFF+ echo '-rw-r----- 1 rickers 1617 Oct 9 14:40 sample.C (as sent)' chmod u=rw,g=r,o= sample.C ls -l sample.C echo x - sample.m sed 's/^X//' > sample.m <<'+FUNKY+STUFF+' X#include "objc.h" X#include <stdio.h> X X= (Lists, Primitive) X Xmain(argc, argv) Xint argc; Xchar **argv; X{ X extern id LinkedList, Stuff; X id mylist, thing; X char which = **++argv; X int aValue; X char *aName; X X if (argc % 2) { X fprintf(stderr, "usage: sample a|i|p [value name] ...\n"); X exit(2); X } X X mylist = [LinkedList new]; X X while (argc -= 2) { X aValue = atoi(*++argv); X aName = *++argv; X thing = [Stuff value:aValue name:aName]; X X switch (which) { X case 'a': X [mylist append:thing]; X break; X case 'i': X [mylist insert:thing]; X break; X case 'p': X [mylist push:thing]; X break; X } X } X X printf("list...\n"); X [mylist print]; X X printf("removing 3...\n"); X [[[mylist remove:[Stuff value:3]] print] free]; X} X X@class(LinkedList, Stuff) X@phyla(Lists, Primitive) +FUNKY+STUFF+ echo '-rw-r--r-- 1 rickers 777 Oct 9 14:41 sample.m (as sent)' chmod u=rw,g=r,o=r sample.m ls -l sample.m exit 0