home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13252 < prev    next >
Encoding:
Text File  |  1992-09-03  |  4.1 KB  |  196 lines

  1. Xref: sparky comp.lang.c++:13252 comp.object:3406 comp.std.c++:1124
  2. Path: sparky!uunet!mcsun!uknet!sersun1!essex.ac.uk!desml
  3. From: desml@essex.ac.uk (Desmond L)
  4. Newsgroups: comp.lang.c++,comp.object,comp.std.c++
  5. Subject: Operations on collections.
  6. Message-ID: <5302@sersun1.essex.ac.uk>
  7. Date: 3 Sep 92 12:39:46 GMT
  8. Sender: news@sersun1.essex.ac.uk
  9. Reply-To: desml@essex.ac.uk (Desmond L)
  10. Organization: University of Essex, Colchester, UK
  11. Lines: 183
  12.  
  13.  
  14. I am currently researching a paper on "carrying out operations
  15. on collections", and would be very grateful if you could
  16. complete and return the following questionaire on the four methods
  17. provided.
  18.  
  19. Any comments welcome.
  20.  
  21. As usual, a copy of the final paper will be sent to any contributor.
  22.  
  23. Thank you in advance for your help.
  24.  
  25.         Lewis Desmond ( desml@ossparc.essex.uk.ac )
  26.  
  27. Questionaire
  28. ------------
  29.  
  30. Please use the following key:
  31.  
  32.         1 - best
  33.         2 - above average
  34.         3 - average
  35.         4 - below average
  36.         5 - worst
  37.  
  38.  ----------------------------------------------------------------------
  39. |                                                               METHOD |
  40. |                                                              A B C D |
  41. |----------------------------------------------------------------------|
  42. |At first glance:                                             |        |
  43. |  Is the code clear ?                                        |        |
  44. |                                                             |        |
  45. |Once you understand the method:                              |        |
  46. |  How easy is it to construct similar programs ?             |        |
  47. |  How easy is it to spot this method in a program ?          |        |
  48. |  Which method is the best (ie which would you use) ?        |        |
  49.  ----------------------------------------------------------------------
  50.  
  51. Any other comments ?
  52.  
  53.  
  54.  
  55.  
  56.  
  57. Methods
  58. -------
  59. We try to find the average age of the members of a club. The code is an
  60. extract from C++, simplified for convenience.
  61.  
  62.         Method A
  63.         --------
  64.  
  65. class Person;
  66. class Club
  67. {
  68.    Person *members[MAXMEMBERS];
  69.  public:
  70.    int ageOfMember(int n)
  71.    { return members[n]->age(); }
  72. };
  73.  
  74. int AverageAge(Club *aClub)
  75. {
  76.    int total = 0;
  77.    for(int n=0;n<MAXMEMBERS;n++)
  78.         total += aClub->ageOfMember(n);
  79.    return total / MAXMEMBERS;
  80. }
  81.  
  82. main()
  83. {
  84.    average = AverageAge(aClub);
  85. }
  86.  
  87.         Method B
  88.         --------
  89.  
  90. class Person;
  91. class Club
  92. {
  93.    Person *members[MAXMEMBERS];
  94.  public:
  95.    int AverageAge();
  96. };
  97.  
  98. int Club::AverageAge()
  99. {
  100.    int total = 0;
  101.    for(int n=0;n<MAXMEMBERS;n++)
  102.         total += member[n]->age();
  103.    return total / MAXMEMBERS;
  104. }
  105.  
  106. main()
  107. {
  108.    average = aClub->AverageAge();
  109. }
  110.  
  111.         Method C
  112.         --------
  113.  
  114. class Person;
  115. class Club
  116. {
  117.    Person *members[MAXMEMBERS];
  118.  public:
  119.    int ageOfMember(int n)
  120.    { return members[n]->age(); };
  121. };
  122.  
  123. class ClubStatisticServer;
  124. class ClubAverageServer
  125.           : public ClubStatisticServer
  126. {
  127.  public:
  128.    virtual int aFunc(Club *,int)=0;
  129.    int doOn(Club *aClub)
  130.    {
  131.       int total = 0;
  132.       for(int n=0;n<MAXMEMBERS;n++)
  133.            total += aFunc(aClub,n);
  134.       return total / MAXMEMBERS;
  135.    };
  136. };
  137.  
  138. class ClubAverageAgeServer
  139.         : public Club AverageServer
  140. {
  141.  public:
  142.    int aFunc(Club *aClub,int n)
  143.    {    return aClub->ageOfMember(n); };
  144. };
  145.  
  146. main()
  147. {
  148.    average = (new ClubAverageAgeServer)->doOn(aClub);
  149. }
  150.  
  151.         Method D
  152.         --------
  153.  
  154. class Person;
  155. class Club
  156. {
  157.    Person *members[MAXMEMBERS];
  158.  public:
  159.    int apply(PersonStatisticServer *aServer)
  160.    {
  161.        for(int i=0;i<MAXMEMBERS;i++)
  162.             aServer->doOn(member[i]);
  163.        return aServer->result();
  164.    };
  165. }
  166.  
  167. class PersonStatisticServer;
  168. class PersonAverageServer
  169.           : public ClubStatisticServer
  170. {
  171.    int count,total;
  172.  public:
  173.    PersonAverageAgeServer() : (0,0) {}
  174.    virtual int aFunc(aMember)=0;
  175.    doOn(Person *aMember)
  176.    {
  177.         count++;
  178.         total += aFunc(aMember);
  179.    };
  180.    int result()
  181.    { return total/count; };
  182. };
  183.  
  184. class PersonAverageAgeServer
  185.           : public PersonAverageServer
  186. {
  187.  public:
  188.    int aFunc(Person *aMember)
  189.    { return aMember->age(); };
  190. }
  191.  
  192. main()
  193. {
  194.    average = aClub-> apply(new PersonAverageAgeServer);
  195. }
  196.