Notes on Sebesta's Chapter 15 Functional programming organizes your thinking one way: data data \ / function / \ data data Data abstraction makes it possible to think like this: operation operation \ / data / \ operation operation However different data have similar kinds of operations: + - \ / int / \ * / and + - \ / double / \ * / These operations even have some common assumptions about them 0*x=0 for instance. So in C six data types are numeric: int, double, char, long, short, and float. However each abstract data type descritpion and implementation would have to start all over again. Objects can inherit common operations from several classes. + - \ / Numeric_Data_Types / \/ \ * || / || --- ---- | | | | int double In Ada 9X, C++ and SIMULA the inheritance is static ( Set up by the compiler). In Smalltalk complete system is dynamic. 15.2 Smalltalk. Keywords message method class object protocol. Scan Sebesta section 15.2 section first. The examples can not be just typed in on a terminal, they are parts of a longer more complicated dialogue. Instead try my $CS320 smalltalk labs ($CS320/lab1[5678]). Bear this in mind: In Smalltalk every thing you input is parsed and stored as an object. EVERYTHING. All objects are accessed and manipulated the same way. To find out about class called XX XX objects To see the methods in a class called XX XX methods To see the method for message mm sent to class XX XX viewMethod: #mm To edit them XX editMethod: #mm Keep your manual open in the lab. after the first hour of SmalTalk Lab reread the book's section 15.2, and then read 15.3. Bad news: We don't have the stuff in 15.3.11. Well not yet. Do some more smalltalk/lab and then read 15.4 Objects in C++ Inheritance class new_class:mode parent_class { new_stuff }; Look at the examples in $CS320/c++. These can be compiled and run on both the Suns and Blaze/IBMs. C++ files should look like this foo.cc or foo.C Borland C++ files look like this FOO.CPP and will be aliased/linked to a FOO.cc file before they are compiled (until I fix this). We have the syntax of C++ so you can "lookup c++ 'public'" and "define c++ class". I have also filed some UseNet Electronic Mail Discussions $CS320/c++/{mbox, news, ...}. On the Suns use elm -f $CS320/news to brouse thru these... On Blaze use "mailx -f $CS320/news". *** Objects in LISP. (not in Sebesta) There is now a moderately standard object oriented LISP called CLOS. Our LISP was specifically created to experiment with objects. See the files $CS320/lisp/*obj* or try grep -i object $CS320/lisp/manual Also look for *.lsp files in $CS320/stacks $CS320/sieve $CS320/stats 15.5 Smalltalk vs C++ vs ??? Examples are in these directories $CS320/stacks $CS320/sieve $CS320/stats Also read the following rather rude messages from Usenet: $CS320/c++/or.smalltalk and $CS320/smalltalk/or.c++ In one of the labs had this experiemnt .X 1 Simple programs and functions in Smalltalk Find out how Smalltalk finds the square of a number. Find examples in the labs and also the code for the Method. What kind of numbers can it handle? Now add a new method 'cubed' that uses the above to work out the cube of a Number. Number addMethod Test/debug your method. Now add this method to Array squared ^ self collect: [ :i | i squared ] What does it do to arrays like #(1 2 3 4) #(1.4 3.2 1234567) Create a simlilar method that cubes all numbers in an Array of Numbers. What does this do: a<-#(1 2 3 4 5 6 7 8 9) a squared cubed I'd like you to have done the same thing in C++, but if you did... Firstly, there is no squared function so we have to declare it: int square(int x) { return x*x; } Secondly, this only works for integers, so we have to go to an idea that is not in the earlier C++ versions and so may not compile correctly on our Gnu C++ version... template square( x){ return x*x; } (Warning: The above code may have grossly incorrect syntax...} Now, our C++ program will happily compile and run the following square(2); square(1.234); I have a feeling that this square("cat"); will not compile because it needs char * operator*(char *, char*) to work. Something similar happens with int cubed(int); Life for the C++ programmer is even more interesting if they try to define Array square( Array); because there is no predefined Array data type in C++. If you write int [] square(int [] array) { int answer[??}; for(int i=0; i