0% found this document useful (0 votes)
13 views45 pages

Static Variable Function

The document explains static member variables and functions in C++, highlighting that static member variables are shared across all instances of a class and exist independently of class objects. It also discusses how static member functions can be called without instantiating an object and can only access static members. Additionally, it emphasizes the need to define static member variables outside the class definition, as they are not stored as part of individual objects.

Uploaded by

safinahmmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views45 pages

Static Variable Function

The document explains static member variables and functions in C++, highlighting that static member variables are shared across all instances of a class and exist independently of class objects. It also discusses how static member functions can be called without instantiating an object and can only access static members. Additionally, it emphasizes the need to define static member variables outside the class definition, as they are not stored as part of individual objects.

Uploaded by

safinahmmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

STATIC MEMBER VARIABLE

AND
STATIC MEMBER FUNCTION
Static variable
Static variable

1
2
3

Note that s_id has kept its value across multiple function
calls.
member variable
member variable

2
1

we have declared two Something class objects, we end up with two copies
of m_value: first.m_value, and second.m_value. first.m_value is distinct
from second.m_value.
Static member variable
 Member variables of a class can be
made static by using the static keyword.
 Unlike normal member variables, static
member variables are shared by all
objects of the class.
Static member variable
2
2
Because s_value is a static member
variable, s_value is shared between all
objects of the class. Consequently,
first.s_value is the same variable as
second.s_value. The above program
shows that the value we set using first
can be accessed using second!
Static members are not associated with
class objects

 Although you can access static members


through objects of the class (as shown
with first.s_value and second.s_value in
the example above)
 it turns out that static members exist
even if no objects of the class have been
instantiated! Much like global variables,
they are created when the program
starts, and destroyed when the program
ends.
Static members are not associated with
class objects

 Consequently, it is better to think of static


members as belonging to the class itself,
not to the objects of the class. Because
s_value exists independently of any class
objects, it can be accessed directly using the
class name and the scope resolution
operator (in this case, Something::s_value):
Static member variable
Static member variable
 When we declare a static member variable
inside a class, we’re telling the compiler about
the existence of a static member variable, but
not actually defining it (much like a forward
declaration).
 Because static member variables are not part
of the individual class objects (they are
treated similarly to global variables, and get
initialized when the program starts), you must
explicitly define the static member outside of
the class, in the global scope.
int Something::s_value = 1; // defines the static member
variable
Static member variable

int Something::s_value = 1; // defines the static member


variable

 This line serves two purposes:


 it instantiates the static member variable.
 Optionally initializes it. In this case, we’re
providing the initialization value 1. If no
initializer is provided, C++ initializes the
value to 0.
Static member variable
 Why use static variables inside classes?
 One great example is to assign a unique
ID to every instance of the class. Here’s
an example of that:
Static member variable
Static member variable
1
2
3

Because s_idGenerator is shared by


all Something objects, when a new
Something object is created, the
constructor grabs the current value
out of s_idGenerator and then
increments the value for the next
object. This guarantees that each
instantiated Something object
receives a unique id (incremented in
Static member function
Static member function
 In this case, we can’t access
Something::s_value directly from main(),
because it is private.
 Normally we access private members through
public member functions.
 While we could create a normal public
member function to access s_value, we’d
then need to instantiate an object of the class
type to use the function!
 We can do better. It turns out that we can also
make functions static.
Static member function

static member functions are not attached to a particular object, they can
be called directly by using the class name and the scope resolution
operator.

Like static member variables, they can also be called through objects of
the class type, though this is not recommended.
Static member functions have no *this
pointer

 static member functions are not attached


to an object, they have no this pointer!
This makes sense when you think about it
-- the this pointer always points to the
object that the member function is working
on. Static member functions do not work
on an object, so the this pointer is not
needed.
Static member functions have no *this
pointer

 static member functions can directly


access other static members (variables
or functions), but not non-static
members. This is because non-static
members must belong to a class object,
and static member functions have no
class object to work with!
Static member function
 Static member functions can also be defined outside of the class
declaration. This works the same way as for normal member functions.
Static member function

all the data and functions in this class are static, we don’t need to
instantiate an object of the class to make use of its functionality!
C++ program for static data member
C++ program for static data member
C++ program for static data member
C++ program for static data member
C++ program for static data member
C++ program for static data member
C++ program for static data member
C++ program for static data member
C++ program for static data member
Static data member
 The type and scope of each static
member variable must be defined
outside the class definition.
 This is necessary because the static data
members are stored separately rather
than as a part of an object.
 Since they are associated with the class
itself rather than with any class object,
they are also known as class variables.
 int Item :: countNum = 10 ;
Static member function
Static member function
Static member function
Static member function
Static member function
Static member function
Static member function
Static member function
Static member function
Static member function
Static member function
Static member function
References
 http://www.learncpp.com/cpp-tutorial/
 “Object oriented programming with C+
+” – E balagurusamy, second edition.
 http://www.cplusplus.com/doc/tutorial/
inheritance/
 Web.

You might also like