Path: utzoo!attcan!uunet!mcvax!ukc!warwick!rlvd!cmc From: cmc@inf.rl.ac.uk (Chris Crampton) Newsgroups: comp.lang.c++ Subject: Re: Smalltalk-80 like inheritance in C++ possible ? Message-ID: <5481@rlvd.UUCP> Date: 16 Mar 89 13:40:46 GMT References: <110@honold.UUCP> Reply-To: cmc@inf.rl.ac.uk (Chris Crampton) Organization: Rutherford Appleton Laboratory, Didcot. UK. Lines: 105 In article <110@honold.UUCP> franco@honold.UUCP (Franco Monti) writes: > How can I translate the behavior of > the code outline [below] to the appropiate structures in C++ ? > ... > > class name One > superclass Object > instance methods > > test > ^1 > > result1 > ^self test > class One { public: virtual int test() { return 1; } int result1() { return test(); } }; // The "virtual" function is the key to what you are after. // Strictly speaking, the class could be a struct as there // are no private or protected declarations > > class name Two > superclass One > instance methods > > test > ^2 class Two : public One { public: int test() { return 2; } }; > > class name Three > superclass Two > instance methods > > result2 > ^self result1 > result3 > ^super test > class Three : public Two { public: int result2() { return result1(); } int result3() { return Two::test(); } }; // I am not quite sure how significant the "^super" is // in your example. Depending what you want, the "Two::" // may not be necessary. > > (*************************************) > > Now create new instances: > > example1 <- One new > example2 <- Two new > example3 <- Three new One example1; Two example2; Three example3; > > and consider scenarios like this: > > expression result > ---------- ------ > > example1 test 1 > example1 result1 1 > example2 test 2 > example2 result1 2 > example3 test 2 > example3 result2 2 > example3 result3 2 example1.test() 1 example1.result1() 1 example2.test() 2 example2.result1() 2 example3.test() 2 example3.result2() 2 example3.result3() 2 Hope this answers your question. Chris. ======================================================================= Chris M Crampton JANET: cmc@uk.ac.rl.inf Informatics Department ARPA: cmc%inf.rl.ac.uk@nss.cs.ucl.ac.uk Rutherford Appleton Labs, UUCP: ..!mcvax!ukc!rlinf!cmc Didcot, OXON, OX11 0QX, U.K. cmc@rlinf.uucp Tel. +44 235 44 6756 FAX. +44 235 44 5831