0% found this document useful (0 votes)
32 views2 pages

C++ Static Data Members

static data members

Uploaded by

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

C++ Static Data Members

static data members

Uploaded by

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

C++ Static Data Members

Static data members are class members that are declared


using static keywords. A static member has certain special characteristics
which are as follows:
 Only one copy of that member is created for the entire class and is
shared by all the objects of that class, no matter how many objects are
created.
 It is initialized before any object of this class is created, even before the
main starts outside the class itself.
 It is visible can be controlled with the class access specifiers.
 Its lifetime is the entire program.

Syntax
className {
static data_type data_member_name;
.....
}
#include <iostream>
using namespace std;

// class definition
class A {
public:
// static data member here
static int x;
A() { cout << "A's constructor called " << endl; }
};

// we cannot initialize the static data member inside the


// class due to class rules and the fact that we cannot
// assign it a value using constructor
int A::x = 2;

// Driver code
int main()
{
// accessing the static data member using scope
// resultion operator
cout << "Accessing static data member: " << A::x
<< endl;

return 0;
}
Defining Static Data Member:
As told earlier, the static members are only declared in the class
declaration. If we try to access the static data member without an explicit
definition, the compiler will give an error.
To access the static data member of any class we have to define it first
and static data members are defined outside the class definition. The only
exception to this are static const data members of integral type which can
be initialized in the class declaration.

Syntax
datatype class_name::var_name = value...;

For example, in the above program, we have initialized the static data
member using the following statement:
int A::x = 10

Accessing a Static Member:


We can access the static data member without creating the instance of
the class. Just remember that we need to initialize it beforehand. There
are 2 ways of accessing static data members:

1. Accessing static data member using Class Name and Scope


Resolution Operator
The class name and the scope resolution operator can be used to access
the static data member even when there are no instances/objects of the
class present in the scope.
Syntax
Class_Name :: var_name
ExampleA::x
2. Accessing static data member through Objects
We can also access the static data member using the objects of the class
using dot operator.
Syntax
object_name . var_name
Example
obj.x

You might also like