8/3/2019 Computer Notes - Static Data Member
1/35
Static Data MemberStatic Data Member
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
2/35
ReviewReview
Constant data membersConstant data members
Constant objectsConstant objects
Static data membersStatic data members
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
3/35
Static Data MemberStatic Data Member
DefinitionDefinition
A variable that is part of a class, yet is not partA variable that is part of a class, yet is not part
of an object of that class, is called static dataof an object of that class, is called static data
membermember
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
4/35
Static Data MemberStatic Data Member
They are shared by all instances ofThey are shared by all instances ofthe classthe class
They do not belong to anyThey do not belong to anyparticular instance of a classparticular instance of a class
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
5/35
Class vs. Instance VariableClass vs. Instance Variable
Student sStudent s11,, s2s2,, s3s3;;
Class Space
s1(rollNo,)
s2(rollNo,)
s3(rollNo,)
Instance VariableClass
Variable
8/3/2019 Computer Notes - Static Data Member
6/35
Static Data Member (Syntax)Static Data Member (Syntax)
Keyword static is used to make a dataKeyword static is used to make a datamember staticmember static
classclass ClassNameClassName{{
staticstatic DataTypeDataTypeVariableNameVariableName;;
};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
7/35
Defining Static Data MemberDefining Static Data Member
Static data member is declared inside theStatic data member is declared inside theclassclass
But they are defined outside the classBut they are defined outside the class
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
8/35
Defining Static Data MemberDefining Static Data Member
classclass ClassNameClassName{{
staticstatic DataTypeDataTypeVariableNameVariableName;;
};};
DataTypeDataType ClassName::VariableNameClassName::VariableName;;
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
9/35
Initializing Static Data MemberInitializing Static Data Member
Static data members should beStatic data members should beinitialized once at file scopeinitialized once at file scope
They are initialized at the time ofThey are initialized at the time ofdefinitiondefinition
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
10/35
ExampleExampleclass Student{class Student{
private:private:
static intstatic int noOfStudentsnoOfStudents;;
public:public:
};};
intint Student::noOfStudentsStudent::noOfStudents = 0;= 0;
/*private static member cannot be accessed outside the/*private static member cannot be accessed outside theclass except for initialization*/class except for initialization*/
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
11/35
Initializing Static Data MemberInitializing Static Data Member
If static data members are notIf static data members are notexplicitly initialized at the time ofexplicitly initialized at the time of
definition then they are initializeddefinition then they are initialized
to 0to 0
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
12/35
ExampleExampleintint Student::noOfStudentsStudent::noOfStudents;;
is equivalent tois equivalent to
intint Student::noOfStudentsStudent::noOfStudents=0;=0;
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
13/35
Accessing Static Data MemberAccessing Static Data Member
To access a static data member there are twoTo access a static data member there are two
waysways
Access like a normal data memberAccess like a normal data member
Access using a scope resolution operatorAccess using a scope resolution operator::::
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
14/35
ExampleExampleclass Student{class Student{
public:public:
static intstatic int noOfStudentsnoOfStudents;;
};};
intint Student::noOfStudentsStudent::noOfStudents;;
int main(){int main(){
StudentStudent aStudentaStudent;;
aStudent.noOfStudentsaStudent.noOfStudents = 1;= 1;
Student::noOfStudentsStudent::noOfStudents = 1;= 1;
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
15/35
Life of Static Data MemberLife of Static Data Member
They are created even when there is no objectThey are created even when there is no object
of a classof a class
They remain in memory even when all objectsThey remain in memory even when all objects
of a class are destroyedof a class are destroyed
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
16/35
ExampleExampleclass Student{class Student{
public:public:static intstatic int noOfStudentsnoOfStudents;;
};};
intint Student::noOfStudentsStudent::noOfStudents;;int main(){int main(){
Student::noOfStudentsStudent::noOfStudents = 1;= 1;
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
17/35
ExampleExampleclass Student{class Student{
public:public:
static intstatic int noOfStudentsnoOfStudents;;};};
intint Student::noOfStudentsStudent::noOfStudents;;
int main(){int main(){
{{
StudentStudent aStudentaStudent;;aStudent.noOfStudentsaStudent.noOfStudents = 1;= 1;
}}
Student::noOfStudentsStudent::noOfStudents = 1;= 1;
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
18/35
UsesUses
They can be used to storeThey can be used to store
information that is required by allinformation that is required by all
objects, like global variablesobjects, like global variables
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
19/35
ExampleExample
Modify the class Student such thatModify the class Student such thatone can know the number ofone can know the number ofstudent created in a systemstudent created in a system
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
20/35
ExampleExampleclass Student{class Student{
public:public:
static intstatic int noOfStudentsnoOfStudents;;
Student();Student();
~Student();~Student();
};};
intint Student::noOfStudentsStudent::noOfStudents = 0;= 0;
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
21/35
ExampleExampleStudent::Student(){Student::Student(){
noOfStudentsnoOfStudents++;++;
}}
Student::~Student(){Student::~Student(){noOfStudentsnoOfStudents----;;
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
22/35
ExampleExampleintint Student::noOfStudentsStudent::noOfStudents = 0;= 0;
intintmain(){main(){
cout
8/3/2019 Computer Notes - Static Data Member
23/35
ProblemProblemnoOfStudentsnoOfStudents is accessible outside the classis accessible outside the class
Bad design as the local data member is keptBad design as the local data member is keptpublicpublic
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
24/35
Static Member FunctionStatic Member Function
Definition:Definition:
The function that needs access to theThe function that needs access to the
members of a class, yet does not need to bemembers of a class, yet does not need to be
invoked by a particular object, is called staticinvoked by a particular object, is called staticmember functionmember function
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
25/35
Static Member FunctionStatic Member FunctionThey are used to access static data membersThey are used to access static data members
Access mechanism for static member functionsAccess mechanism for static member functionsis same as that of static data membersis same as that of static data members
They cannot access any nonThey cannot access any non--static membersstatic members
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
26/35
ExampleExampleclass Student{class Student{
static intstatic int noOfStudentsnoOfStudents;;
intint rollNorollNo;;public:public:
static int getTotalStudent(){static int getTotalStudent(){
returnreturn noOfStudentsnoOfStudents;;
}}
};};
int main(){int main(){
int i = Student::getTotalStudents();int i = Student::getTotalStudents();
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
27/35
Accessing non static data membersAccessing non static data membersint Student::getTotalStudents(){int Student::getTotalStudents(){
returnreturn
rollNorollNo
;;
}}
int main(){int main(){
intint i = Student::getTotalStudents();i = Student::getTotalStudents();
/*Error: There is no instance of Student,/*Error: There is no instance of Student,rollNorollNo cannot be accessed*/cannot be accessed*/
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
28/35
this Pointerthis Pointerthisthis pointer is passed implicitly to memberpointer is passed implicitly to member
functionsfunctionsthisthis pointer is not passed to static memberpointer is not passed to static member
functionsfunctions
Reason is static member functions cannotReason is static member functions cannot
access non static data membersaccess non static data members
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
29/35
Global Variable vs. Static MembersGlobal Variable vs. Static Members
Alternative to static member is to useAlternative to static member is to use
global variableglobal variable
Global variables are accessible to allGlobal variables are accessible to all
entities of the programentities of the programAgainst information hidingAgainst information hiding
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
30/35
Array of ObjectsArray of ObjectsArray of objects can only be created if an objectArray of objects can only be created if an object
can be created without supplying an explicitcan be created without supplying an explicitinitializerinitializer
There must always be a default constructor ifThere must always be a default constructor if
we want to create array of objectswe want to create array of objects
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
31/35
ExampleExampleclass Test{class Test{
public:public:
};};
intintmain(){main(){Test array[2]; // OKTest array[2]; // OK
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
32/35
ExampleExampleclass Test{class Test{
public:public:Test();Test();
};};
intintmain(){main(){Test array[2]; // OKTest array[2]; // OK
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
33/35
ExampleExampleclass Test{class Test{
public:public:Test(intTest(int i);i);
};};
intintmain(){main(){Test array[2]; // ErrorTest array[2]; // Error
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
34/35
ExampleExampleclass Test{class Test{
public:public:Test(intTest(int i);i);
}}
intintmain(){main(){
Test array[2] =Test array[2] ={Test(0),Test(0)};{Test(0),Test(0)};
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Static Data Member
35/35
ExampleExampleclass Test{class Test{
public:public:Test(intTest(int i);i);
}}
intintmain(){main(){
Test a(1),b(2);Test a(1),b(2);
Test array[2] = {Test array[2] = {a,ba,b};};
}}
http://ecomputernotes.com