Subject: Intermediate concept between class variable and instance variable

From: Omer Zak <[email-masked]>
Date: Tue, 27 Sep 2005 15:29:29 +0300 (EET DST)

Several object oriented languages have the concept of class variables
(static in C++) and instance variables.
When declaring a class and constructing objects, which are instances of
this class - each object has its own values of instance variables.
However, all the objects share the same class variables (for example, a
class variable can hold a count of all objects instantiated from that
class).

I wonder whether there is an OO language, which has the concept of 50%
class variable/50% instance variable.

For example, we have the class "Student".  We want to count the total
number of students, hence we have, as a class variable, countOfStudents.

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?
                                             --- 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

---

From: "Nadav Har'El" <[email-masked]>
Date: Tue, 27 Sep 2005 15:51:34 +0300

> 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
[email-masked]             |-----------------------------------------
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

---

From: Omer Zak <[email-masked]>
Date: Tue, 27 Sep 2005 16:05:41 +0300 (EET DST)

On Tue, 27 Sep 2005, Nadav Har'El wrote:

> > year students, etc.Such counters are not instance variables, because
> > they are common to more than one object.However, they are not class
> > animal?If yes, how is it represented in those languages?
>

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