home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c++:13252 comp.object:3406 comp.std.c++:1124
- Path: sparky!uunet!mcsun!uknet!sersun1!essex.ac.uk!desml
- From: desml@essex.ac.uk (Desmond L)
- Newsgroups: comp.lang.c++,comp.object,comp.std.c++
- Subject: Operations on collections.
- Message-ID: <5302@sersun1.essex.ac.uk>
- Date: 3 Sep 92 12:39:46 GMT
- Sender: news@sersun1.essex.ac.uk
- Reply-To: desml@essex.ac.uk (Desmond L)
- Organization: University of Essex, Colchester, UK
- Lines: 183
-
-
- I am currently researching a paper on "carrying out operations
- on collections", and would be very grateful if you could
- complete and return the following questionaire on the four methods
- provided.
-
- Any comments welcome.
-
- As usual, a copy of the final paper will be sent to any contributor.
-
- Thank you in advance for your help.
-
- Lewis Desmond ( desml@ossparc.essex.uk.ac )
-
- Questionaire
- ------------
-
- Please use the following key:
-
- 1 - best
- 2 - above average
- 3 - average
- 4 - below average
- 5 - worst
-
- ----------------------------------------------------------------------
- | METHOD |
- | A B C D |
- |----------------------------------------------------------------------|
- |At first glance: | |
- | Is the code clear ? | |
- | | |
- |Once you understand the method: | |
- | How easy is it to construct similar programs ? | |
- | How easy is it to spot this method in a program ? | |
- | Which method is the best (ie which would you use) ? | |
- ----------------------------------------------------------------------
-
- Any other comments ?
-
-
-
-
-
- Methods
- -------
- We try to find the average age of the members of a club. The code is an
- extract from C++, simplified for convenience.
-
- Method A
- --------
-
- class Person;
- class Club
- {
- Person *members[MAXMEMBERS];
- public:
- int ageOfMember(int n)
- { return members[n]->age(); }
- };
-
- int AverageAge(Club *aClub)
- {
- int total = 0;
- for(int n=0;n<MAXMEMBERS;n++)
- total += aClub->ageOfMember(n);
- return total / MAXMEMBERS;
- }
-
- main()
- {
- average = AverageAge(aClub);
- }
-
- Method B
- --------
-
- class Person;
- class Club
- {
- Person *members[MAXMEMBERS];
- public:
- int AverageAge();
- };
-
- int Club::AverageAge()
- {
- int total = 0;
- for(int n=0;n<MAXMEMBERS;n++)
- total += member[n]->age();
- return total / MAXMEMBERS;
- }
-
- main()
- {
- average = aClub->AverageAge();
- }
-
- Method C
- --------
-
- class Person;
- class Club
- {
- Person *members[MAXMEMBERS];
- public:
- int ageOfMember(int n)
- { return members[n]->age(); };
- };
-
- class ClubStatisticServer;
- class ClubAverageServer
- : public ClubStatisticServer
- {
- public:
- virtual int aFunc(Club *,int)=0;
- int doOn(Club *aClub)
- {
- int total = 0;
- for(int n=0;n<MAXMEMBERS;n++)
- total += aFunc(aClub,n);
- return total / MAXMEMBERS;
- };
- };
-
- class ClubAverageAgeServer
- : public Club AverageServer
- {
- public:
- int aFunc(Club *aClub,int n)
- { return aClub->ageOfMember(n); };
- };
-
- main()
- {
- average = (new ClubAverageAgeServer)->doOn(aClub);
- }
-
- Method D
- --------
-
- class Person;
- class Club
- {
- Person *members[MAXMEMBERS];
- public:
- int apply(PersonStatisticServer *aServer)
- {
- for(int i=0;i<MAXMEMBERS;i++)
- aServer->doOn(member[i]);
- return aServer->result();
- };
- }
-
- class PersonStatisticServer;
- class PersonAverageServer
- : public ClubStatisticServer
- {
- int count,total;
- public:
- PersonAverageAgeServer() : (0,0) {}
- virtual int aFunc(aMember)=0;
- doOn(Person *aMember)
- {
- count++;
- total += aFunc(aMember);
- };
- int result()
- { return total/count; };
- };
-
- class PersonAverageAgeServer
- : public PersonAverageServer
- {
- public:
- int aFunc(Person *aMember)
- { return aMember->age(); };
- }
-
- main()
- {
- average = aClub-> apply(new PersonAverageAgeServer);
- }
-