• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
1 view
Static Members ( 31)
by (320 points)  
reshown by

1 Answer

0 votes
Welcome to Module 16 of Programming in C++.In this module, we will talk about static
members; specifically we will try to understandthe static data members and static member function.The outline will comprise these two kinds
of members, and we will have a discussionabout singleton classes.The outline will be visible on the left of
your every slide.A static data member, we have learnt about
data members.So, a data member is a component of a class
structure.So, whenever a class is instantiated in terms
of an object, the object will have the datamembers, and those data members we know can be accessed from the pointer of the objector from within the member functions of the
object.In terms of the static data member, the things
are very different.Static data members is are not associated
with the object; like the normal data membersare associated with the object, but the static
data members are in contrast are associatedwith the class of which the array member.So, the critical property of static data member
is that no matter how many instances of aclass is created there will be only one instance
of the static data member of a class.So, it is shared by all objects of the class.In fact, the interesting fact that we will
see over the different examples is that astatic data member may exist, will exist even
when no object has been created for the class.So, the purpose of the static member is primarily
to maintain information to maintain data thatis specific to the class, but not specific
to each and every instance.And we will see there are several programming
situations, there are several problems forwhich having such a static data member would
really help.For those of few who have some familiarity
with java, the static data member is a classvariable in java, in contrast to the instance
variable which is the ordinary non-staticdata member in C++.For the static data member, we will see some
additional constraints; we will see that thestatic data member is declared within the
class like the ordinary data members.But it needs to be defined outside the class
that is the memory binding for the staticdata member has to be done further outside
the class typically in a source file, whichcan be initialized.And the interesting fact is this static member of course, is any data member could be anobject.So, static data member is also an object.The static data member gets constructed before the main function starts; this is somethingwhich probably you have not seen before in
earlier cases.We always thought that when we start executing
,the main is when objects is start gettingconstructed, but the static members can get
created or will get created before the mainstarts and symmetrically they will get destructed
after the main ends after the main returns.So, we will see this through the example,
static data members can be public or privateany of the access specifier is possible.Now in terms of accessing a static data member
we will need to use the class name becauseas I have already mentioned here that it is
associated with a class and not with the object.So, the typical object dot kind of notation
is not the best way to access the static datamembers.They will be accessed from the class name
using the colon-colon or scope resolutionoperator.And in terms of usage, the static data members
are nothing other than they behave exactlylike the global variables.The only difference being or the significant
difference being that the global variablesexist in the global scope, but static data
members are within the scope of the classin which the static data member belongs.So, they virtually eliminate the requirement
of having any global data in the program.So, with this introduction, let me take you
through a simple example and then these pointswill become more and more clears.This is an example of a static data member;
it is very simple case.So, all that I have is a class, it is some
this class does not do anything very meaningful,it is called by my class and it has ordinary
data member here int x.So, on the left column, what you see, does
not use static member it is what you havebeen introduced to earlier in terms of the
data members and member function.So, x is a data member, there is a get member
function which initializes x with 15.And there is a print member function, which takes the current value of x, increments itby or adds 10 to that, and then prints that
value.So, these are simple thing we are trying to
do.So, now if we look into the application code
here, we are constructing two such objectsobject 1 and object 2, as you can see that
my class does not have any constructor.So, we know the free default constructor will
be used; and by that constructor, x will notbe assigned in a specific value.So, we do invoke get on object 1.So, if we invoke get on object 1 in here,
then certainly for object 1, so if I can drawin this is my object 1, this is my object
2, then I have x in both.And as I execute get for object 1, this will
become 15; as I execute get for object 2,this will become 15.And then if I do print, invoke print for object
one, so it will print will take this value15 as in here add 10 to it.So, this will become 25, and therefore, it
will print 25.It prints x is equal to 25.Similarly, print is again invoked for object
2, it again takes this value which is 15 adds10 to it, because I have called print, the
value becomes 25 and this is what is printed.So, this behavior if you look at then this
behavior is already very clear to you.So, you will know that I am doing exactly
the same thing with both the objects object1 and object 2, and therefore, both of them
print the same value 25 for x.Now, look at the right column, where we are
trying to use the static data member.So, how do I use a static member, earlier
I was saying this is int x, now I am sayingit is static int x.So, I have prefixed the keyword static in
front of the data member declaration.So, this is a non-static declaration, this
now becomes a static declaration.Now with this the difference that that happens
is if I redraw what I had on this side witha non-static data I had obj 1, obj 2, data
member x, data member x, here when I willconstruct the objects at this line, I will
have obj 1, I will have obj 2, but none ofthese objects obj 1 and obj 2 has x as a data
member, because x is a static data memberand it is not associated with any specific
object.So, there is another object whose name is
MyClass colon-colon x, my class colon-colonx, which is the static data member x of this
class.So, you can see that the moment I define this
to be static my data member is not a partof the object neither a part of object 1 nor
a part of object 2, but it becomes a separatelocation, it becomes a separate object.Now the only thing is a name of that object
is not x, it is MyClass colon-colon x thatit is qualified by the class name and that
is how I access it.So, let us see when I tried to use it using
the same application that I had here thisthere is no difference between this application
and this application in main.The objects have got constructed as we have
seen.I invoked get, so x is assigned 15.So, what does this mean what is x, x here
is the x of MyClass, so it is this variable.So, this becomes 15.This static value becomes 15.I again execute get for invoke I get for obj
2, the same x is assigned 15, because x isstatic it is common between two objects.So, x is set to 15 twice.Now I invoke print for obj 1, so what will
happen the value of x is 15 and 10 is addedto it, so the value of x will now become 25,
because 10 has got added, and that value iswhat will get printed.So, when I do obj 1 dot print x is printed
as 25.Till this point, there is no difference in
the behavior from the non-static case.But consider the next one when the print is
invoked for obj 2.When print is invoked for obj 2, the value
of x now is 25, it is not fifteen anymore.So, this will, 10 will get added to this,
and now it will become 35 and x now becomes35.And so, when I print according to this print
method then the value that I get printed is35, because the static object is shared by
the both objects.So, when I have invoked print twice, once
through object 1, and once through object2, the value 10 has been added to x twice.And therefore, earlier it was 25; in the second
case now with the static data member, it becomes35, so that is the basic behavior of static
data members that we will have to learn.So, we have noted that x is a static data
member it is shared by the two objects wehave seen how they are shared.And this next point is very important, let
me explain this point.If you compare the two programs other than
the static keyword here, there is one additionalline that has been included in this.We say that this when saying that this data
member is static, this is nearly a declaration,which means that it is defining it is saying
that the name of this variable is x, it isa class variable.It will have one instance for the class it
is of type int, but it does not associateany memory with this variable.When it is non-static, I do not care about
this, because if it is non-static I know atsome point some object will get instantiated
like obj 1; and wherever obj 1 gets instantiated,x will be a part of it.So, x will get its memory through the instantiation
of obj 1, but in case of static this is justa declaration, because when obj 1 will get
instantiated the class variable x is not apart of this object.So, I need to create the memory or bind the
memory for this data member - static datamember separately and this is what is known
as a definition through which I create that.So, that definition says that it is done in
the in the global scope, I say that this isthe name of the variable MyClass colon-colon
x, class name colon-colon variable name.The type is int.So, I say the type is int and I put the initialization
which I have put here as 0.So, this is to define the static data member.So, if you write a program which has static
data member, and if you do not do this, donot put this then the compiler will start
crying saying that this particular staticdata member MyClass colon-colon x has not
been defined, and therefore will not get amemory.So, this is something special that you will
have to remember for the static data memberthat they will require a definition separately
and they will need to be initialized whenthe program starts working.So, when does this get executed, when does
this x gets the value 0, it gets the value0 before you start on the beginning of main.So, as we start executing the program, first
all static data members of all classes willget constructed and initialized, and only
then main will start working.Similarly when main ends only after that all
the static data members of all classes willstart getting destructed in the reverse order
in which they were constructed.Of course, in this particular example, you
do not see that construction, destructionprocess, because in this case the static data
member is an object of the built in type,where we know there is no explicit constructor
or destructor available.So, with this let us move on, and look at
a little bigger example, a more realisticexample of using the static data member.So, here what we are trying to do we have
we are creating a class print jobs, whichhas every printing job has a number of pages
to print.And when I construct the print job object,
then I know what are the number of pages thatneed to be printed in this job.Now what I want to track through this is I
want to track that certain
by  
...