Nov 19, 2003 ------------- - In Smalltalk, everything is an object - even a class is an object! - e.g., class FinancialHistory is the only object of class Meta-FinancialHistory - All classes are automatically placed into - an existing inheritance hierarchy - the root class is called "Object" - Be careful to distinguish between - Object: root class (capital O) - object: just some object - Messages in Smalltalk - read like English - More on Smalltalk - comparing C++ code to Smalltalk code - Highlights of correspondences between C++ and Smalltalk - see difference in message format - *this corresponds to self - ^ is used when you want to return something - ifTrue: ifFalse: is a conditional expression - Use of super for defining "new" - new ^super new setInitialBalance: 0 - initialize: amount ^super new setInitialBalance: amount - Take a look at the following correspondences: class FinancialHistory { int cashOnHand; Dictionary incomes, expenditures; public: FinancialHistory(int); void receiveFrom(int, char*); void spendfor(int, char*); int cash(); int totalReceivedFrom(char*); int totalSpentFor(char*); } FinancialHistory::FinancialHistory(int amount) { cashOnHand = amount; } void FinancialHistory::receiveFrom(int amount, char* source) { incomes.atPut(source, (*this).totalReceivedFrom(source) + amount); cashOnHand = cashOnHand + amount; } void FinancialHistory::spendFor(int amount, char* reason) { expenditures.atPut(reason, (*this).totalSpentFor(reason) + amount); cashOnHand = cashOnHand - amount; } int FinancialHistory::cash() { return cashOnHand; } int FinancialHistory::totalReceivedFrom(char* source) { if (incomes.includesKey(source)) return incomes.at(source) else return 0; } int FinancialHistory::totalSpentFor(char* reason) { if (expenditures.includesKey(reason)) return expenditures.at(reason) else return 0; } class name FinancialHistory superclass Object instance variable names cashOnHand incomes expenditures receive: amount from: source incomes at: source put: (self totalReceivedFrom: source) + amount. cashOnHand <- cashOnHand + amount spend: amount for: reason expenditures at: reason put: (self totalSpentFor: reason) + amount. cashOnHand <- cashOnHand - amount cashOnHand ^cashOnHand totalReceivedFrom: source (incomes includesKey: source) ifTrue: [^incomes at: source] ifFalse: [^0] totalSpentFor: reason (expenditures: includesKey: reason) ifTrue: [^expenditures at: reason] ifFalse: [^0] setInitialBalance: amount cashOnHand <- amount. incomes <- Dictionary new. expenditures <- Dictionary new