Re: Heaven help me, I miss the C++ class
- Posted by Mathew Hounsell <mfh03 at UOW.EDU.AU> Feb 26, 2000
- 387 views
Date sent: Thu, 24 Feb 2000 21:11:15 -0500 From: SR Williamson <sr.williamson at OSHA.GOV> Subject: Heaven help me, I miss the C++ class (not a question, comment + thoughts -- long) > I can set up a sequence, say > sequence people > > and give them properties say > sequence thoughts, feelings, actions > > So far, I can duplicate what I do in C++. Just in C++ it would be approx. > class people{ > int thoughts, feelings I would actually code this as class Person { // Not people is a plural enum { THOUGH_COUNT }; enum thought_et { I_AM_HUNGRY, I_AM_SLEEPY }; thought_et thoughts[THOUGHT_COUNT]; ... virtual void behave() = 0; }; > But the EU way is better because I can decide later what thoughts, > feelings, and actions really are (though I can do that clumsily in C++ > using overloading, I guess). You should design this, besides if you are going to store the thoughts, you will use them in the simulation, thus they must be predetermined. Also the responses should be so perhaps ... class Thought { virtual void onHave(); // Respond to initial thought virtual void onSatisfy(); // Respond to satisfaction virtual bool willSatisfy( Action act ); // Check if it wil satisfy thought ... }; class Hunger : public Thought { virtual void onHave() {} // Respond to initial thought virtual void onSatisfy() {} // Respond to satisfaction virtual bool willSatisfy( Action act ) { // Check if it wil satisfy thought if( Action::EAT == Action ) return true; return false; } }; > Now here's the sticky part (speaking of which, thanks for sticking with > this wordy message so far....). In C++, I can define procedures to go with > my people. And using inheritance, I can make them different. Say I want to > initiate a subclass of people, call them Good, and another subclass, call > them Bad. > > class Good::people { //(or something like it) > void beGood(); > void strut(); > > class Bad::people { > void partyTooMuch(); > void getStuffed(); > > My Good::people and Bad::people are exactly the same except for their > actions (and maybe their thoughts and feelings if I want), but these folks > all inherit from the same generic people class. Polymorphism, the reason for inheritance. (BTW the syntax is wrong ). > Now in EU, I can see doing something similar in a different way, almost > upside down. I've already got > sequence people > > I can add > constant good = 1 > constant bad = 2 > > sequence EUMailingList, LinusT, BillG, CShultz, Satan, HellsAngels > > people[good] = {EUMailingList, LinusT, CShultz} > /*okay, I'm a suckup*/ > people[bad] = {BillG, Satan, HellsAngels} > But my problem comes in when I now have to give them behaviors and > attributes. I have to do it individually. I could use a procedure with a > sequence passed in, like this > > procedure beGood(sequence aPeople) > aPeople[good] = blah, blah, blah > > and that would help cut down on errors (but not make them almost out of the > question, like in C++). And the C++ statement is very elegant > > good.beGood() > > And it does it for all good people. (I think) The problem is in your design. The way C++ achieves different behaviours for subclass is by effectively checking what type the object is to determine what to do. class GoodPerson : public Person { virtual void behave(); }; class BadPerson : public Person { virtual void behave(); }; When C++ comes across a Person it goes well if( they are a GoodPerson ) doThis ... if( they are a BadPerson ) doThis ... That is the way to code it in Euphoria ------------------------------------------------ -- To define class like structures in Euphoria -- design a sequence and -- give each index a certain value to represent. -- class Person constant PERSON_LEN = 2, TYPE_ID = 1, THOUGTS = 2 ------------------------------------------------ -- These are the possible values for the type of person, they must be unique constant GOOD_PERSON = 1, BAD_PERSON = 2 -- These are the possible values for the type of thought, they must be unique constant BUY_EU = 1, BUY_VISCPP = 2, DONATE = 3, STEAL = 4 constant Thoughts = { "I will buy Euphoria", "I will buy Visual C++", "I will donate money", "I will steal money" } ------------------------------------------------ -- Error prevention types type person_tid_t( integer i ) -- Person Type Id return ( i = GOOD_PERSON or i = BAD_PERSON ) end type type person_t( sequence s ) -- Person -- Test the sequence is all OK return 1 -- if OK end type ------------------------------------------------ -- Create a Person in a structure function Person_New( person_tid_t typ ) sequence val -- Create the person in val -- Make sequence val = repeat( 0, PERSON_LEN ) -- Save type val[TYPE_ID] = typ if typ = GOOD_PERSON then -- Create Good elsif type = BAD_PERSON then -- Create Bad end if return val end function -- Make a person do what they want. procdure Person_Behave( person_t p ) if p[TYPE_ID] = GOOD_PERSON then -- DoGood elsif p[TYPE_ID] = BAD_PERSON then -- DoBad end if end procedure ------------------------- Sincerely, Mathew Hounsell mat.hounsell at excite.com