Loading ...
Sorry, an error occurred while loading the content.

Re: [hackers-il] Intermediate concept between class variable and instance variable

Expand Messages
  • Nadav Har'El
    ... If you create a subclass of Student called Freshman , this new class can have its own class variables, which are seperate from that of the ordinary
    Message 1 of 3 , Sep 27, 2005
    • 0 Attachment
      On Tue, Sep 27, 2005, Omer Zak wrote about "[hackers-il] Intermediate concept between class variable and instance variable":
      > But we want to also count separately the number of 1st year students, 2nd
      > year students, etc. Such counters are not instance variables, because
      > they are common to more than one object. However, they are not class
      > variables, because they are common to disjoint subsets of the set of
      > instances of the class "Student".
      >
      > Are there OO languages, which have special notation for this kind of
      > animal? If yes, how is it represented in those languages?

      If you create a subclass of "Student" called "Freshman", this new class can
      have its own class variables, which are seperate from that of the ordinary
      students.

      The question is, though, what if you want to have 3 subclasses, "student1",
      "student2", "student3", and each should have a "count" variable which belongs
      to that class (not a particular instance). In C++, you'll need to declare
      this count variable in student1, student2 and student3, and you can't
      declare it in the superclass student.

      But this is not a real problem, because in C++ you're not encouraged to
      use public fields anyway, and certainly not to have "virtual fields".
      Virtual functions are the way to go. So, you can have something like this:

      class Student {
      private:
      string name;
      virtual int getNumPeers();
      virtual int incrNumPeers();
      }

      class Student1 : Student {
      static int count; /* count of first-year students */
      int getNumPeers(){ return count; }
      int incrNumPeers(){ count++; }
      }
      ...

      Now, if you get a Student object, you can use its getNumPeers() function to
      get the number of students in its particular year, rather than of all Students.

      I'm not sure that's exactly what you hoped for, though. It's not exactly a
      special C++ syntax.

      --
      Nadav Har'El | Tuesday, Sep 27 2005, 23 Elul 5765
      nyh@... |-----------------------------------------
      Phone +972-523-790466, ICQ 13349191 |"Arguing with nyh just doesn't pay off."
      http://nadav.harel.org.il |-- Muli Ben-Yehuda, Linux-il list
    • Omer Zak
      ... I want to avoid having to declare a separate class for each group of objects. For example, suppose I declared Student_year1, Student_year2, Student_year3
      Message 2 of 3 , Sep 27, 2005
      • 0 Attachment
        On Tue, 27 Sep 2005, Nadav Har'El wrote:

        > On Tue, Sep 27, 2005, Omer Zak wrote about "[hackers-il] Intermediate concept between class variable and instance variable":
        > > But we want to also count separately the number of 1st year students, 2nd
        > > year students, etc.Such counters are not instance variables, because
        > > they are common to more than one object.However, they are not class
        > > variables, because they are common to disjoint subsets of the set of
        > > instances of the class "Student".
        > >
        > > Are there OO languages, which have special notation for this kind of
        > > animal?If yes, how is it represented in those languages?
        >
        > If you create a subclass of "Student" called "Freshman", this new class can
        > have its own class variables, which are seperate from that of the ordinary
        > students.
        >
        > The question is, though, what if you want to have 3 subclasses, "student1",
        > "student2", "student3", and each should have a "count" variable which belongs
        > to that class (not a particular instance). In C++, you'll need to declare
        > this count variable in student1, student2 and student3, and you can't
        > declare it in the superclass student.
        >
        > But this is not a real problem, because in C++ you're not encouraged to
        > use public fields anyway, and certainly not to have "virtual fields".
        > Virtual functions are the way to go. So, you can have something like this:
        >
        > class Student {
        > private:
        > string name;
        > virtual int getNumPeers();
        > virtual int incrNumPeers();
        > }
        >
        > class Student1 : Student {
        > static int count; /* count of first-year students */
        > int getNumPeers(){ return count; }
        > int incrNumPeers(){ count++; }
        > }
        > ...
        >
        > Now, if you get a Student object, you can use its getNumPeers() function to
        > get the number of students in its particular year, rather than of all Students.
        >
        > I'm not sure that's exactly what you hoped for, though. It's not exactly a
        > special C++ syntax.

        I want to avoid having to declare a separate class for each group of
        objects.
        For example, suppose I declared Student_year1, Student_year2,
        Student_year3 and then the university decided to extend math studies from
        3 years to 4 years? Then I'd have to declare also Student_year4. I want
        to avoid this.

        Of course, there are many ways to deal with this (for example, in Perl or
        Python you can construct a hash whose keys are the years and the values
        are the counters). But my question is whether there is a notational way
        to deal with it inside the class/instance framework without introduction
        of extraneous entities.
        --- Omer
        My opinions, as expressed in this E-mail message, are mine alone.
        They do not represent the official policy of any organization with which
        I may be affiliated in any way.
        WARNING TO SPAMMERS: at http://www.zak.co.il/spamwarning.html
      Your message has been successfully submitted and would be delivered to recipients shortly.