static member and inheritance

T

Tony Johansson

Hello!

Assume you have three classes.
At the top you have a class called Base which is an abstract class.
In this Base class is there for example one static int.called number
Just below this Base class you have two derived classes called Derived_1 and
Derived_2.

Now to my question this class variable number does all classes in this case
the Base class, Derived_1 and Derived_2
share this variable?

So if you create 3 object of class Derived_1 and 3 object of class Derived_2
you still have only one instance of this variable number that all 6 object
will share. Is that correct ?

Many thanks.

//Tony
 
K

Karl Heinz Buchegger

Tony said:
Hello!

Assume you have three classes.
At the top you have a class called Base which is an abstract class.
In this Base class is there for example one static int.called number
Just below this Base class you have two derived classes called Derived_1 and
Derived_2.

Now to my question this class variable number does all classes in this case
the Base class, Derived_1 and Derived_2
share this variable?

So if you create 3 object of class Derived_1 and 3 object of class Derived_2
you still have only one instance of this variable number that all 6 object
will share. Is that correct ?

Yes.
That's what static member variables are for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,666
Latest member
selsetu

Latest Threads

Top