0% found this document useful (0 votes)
7 views12 pages

Static

Uploaded by

Anchal Pandey
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)
7 views12 pages

Static

Uploaded by

Anchal Pandey
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/ 12

Static Keyword in C++

• Static keyword has different meanings


when used with different types. We can use
static keyword with:
• Static Variables : Variables in a function,
Variables in a class
Static Members of Class : Class objects
and Functions in a class
Static variables in a Function
When a variable is declared as static, space
for it gets allocated for the lifetime of
the program.
Even if the function is called multiple times,
space for the static variable is allocated only
once and the value of variable in the
previous call gets carried through the next
function call.
#include <iostream>
#include <string>
using namespace std;
void demo()
{
// static variable
static int count = 0;
cout++;
cout << count << " ";
}
int main()
{
for (int i=0; i<5; i++)
demo();
return 0;
}
Static variables in a class:
• As the variables declared as static are initialized only
once as they are allocated space in separate static
storage so, the static variables in a class are shared by
the objects.
• There can not be multiple copies of same static
variables for different objects.
• Also because of this reason static variables can not be
initialized using constructors.
#include<iostream>
using namespace std;
class GfG
{
public:
static int i;
GfG()
{

}
};
int GfG::i = 1;
int main()
{
GfG obj;
cout <<obj.i;
return 0;
}
Class objects as static:
• Just like variables, objects also when declared as static
have a scope till the lifetime of program.
• If the object is declared inside the if block as non-
static.
• So, the scope of variable is inside the if block only.
• So when the object is created the constructor is
invoked and soon as the control of if block gets over
the destructor is invoked as the scope of object is
inside the if block only where it is declared.
#include<iostream>
using namespace std;
class GfG
{
int i = 0;
public:
GfG()
{
i = 0;
cout << "Inside Constructor\n";
}
~GfG()
{
cout << "Inside Destructor\n";
}
};
int main()
{
int x = 0;
if (x==0)
{
static GfG obj;
}
cout << "End of main\n";
}
output
Inside Constructor
End of main
Inside Destructor
Static functions in a class
• Just like the static data members or static variables inside the
class, static member functions also does not depend on
object of class.
• It is recommended to invoke the static members using the
class name and the scope resolution operator.
• Static member functions are allowed to access only the static
data members or other static member functions, they can
not access the non-static data members or member
functions of the class.
#include<iostream>
using namespace std;
class GfG
{
public:
static void printMsg()
{
cout<<"Welcome to GfG!";
}
};
int main()
{
// invoking a static member function
GfG::printMsg();
}
Output

#include<iostream>
using namespace std;
class Test
{
static int i;
int j;
};
int main()
{
cout << sizeof(Test);
return 0;
}
Output: 4 (size of integer)
static data members do not contribute in size of
an object. So ‘i’ is not considered in size of Test.
Also, all functions (static and non-static both) do
not contribute in size.

You might also like