Static Members and Static Concept
- The static concept in C and C++ is different.
- When we create the static variable in the C its scope is limited means inside its .obj file only.
- But in the C++ when we create the static member its single copy can be shared between all the objects of the class.
- Memory allocated for the static members is on the .bss or non bss section.
- We can initialize the static members inside the constructor like the non static members, or we can initialize it after the class definition like int demo::i=10;
- Value for the static member is initialized only once.
- When we create the static member function it can be accessed without creating an object of the class.
- When we create the data members static we have to declare it outside the class.
- Memory is not allocated for the static members inside the object memory, means when we check sizeof of the class an object then it only shows the sizes of the non static members only.
- This pointer is not allocated for the static member function, which is only created for the non static member functions.
- We can call the static member function using class name like demo::fun();
- we can call the static member functions by an object also or by class pointer also. Like obj.fun(); or pobj->fun();
- The non static member function can access static as well as non static members of a class, but the static member function can access only the static members of a class.
- We can not create static as well as non static member functions having same name and same prototype.
- The static member function cannot be declared with the keyword virtual, const, volatile.
Static concepts (C++) with Examples
- In C++ we can create static characteristics as well as static behaviour.
- Static behaviour means a function of a type static and static characteristics means a data members of a class by applying a’static’ keyword.
- Memory allocated for the static member is on bss or non bss section.
- Single copy of the static variable is shared by the all objects of the class.
- If there is static variable inside the class then the memory allocated for the object if that class is only for the non static members.
- we can initialize the static variables inside the constructor
- static member functions can not access non static members
class demo
{
public:
int i,j;
static int h;
demo()
{
i=20;
j=20;
}
~demo()
{
cout<<“In the destructor of demo class\n”;
}
};
int main()
{
demo var;
cout<<var.h; //Error Unable to access h
return 0;
}
/* Next Example*/
class demo
{
public:
int i,j;
static int h;
demo()
{
i=20;
j=20;
}
~demo()
{
cout<<“In destructor of the demo.\n”;
}
};
//
// This statement tells that h is a static variable of class demo
//
int demo::h;
int main()
{
demo var;
return 0;
}
/* Next Example*/
class demo
{
public:
int i,j;
static int h;
demo()
{
i=20;
j=20;
//
//we can initialize the static variables inside the constructor
//
h=10;
}
~demo()
{
cout<<” In destructor of the demo.\n”;
}
};
int i,j;
static int h;
demo()
{
i=20;
j=20;
}
~demo()
{
cout<<” In destructor of the demo.\n”;
}
};
//
// This statement tells that h is a static variable of class demo
//
int demo::h;
int main()
{
demo var;
return 0;
}
/* Next Example*/
class demo
{
public:
int i,j;
static int h;
demo()
{
i=20;
j=20;
//
//we can initialize the static variables inside the constructor
//
h=10;
}
~demo()
{
cout<<” In destructor of the demo.\n”;
}
};
int demo::h;
int main()
{
demo var;
cout<<var.h; //10
return 0;
}
/* Next Example*/
class demo
{
public:
int i,j;
static int h;
demo()
{
i=20;
j=20;
h=10;
}
~demo()
{
cout<<” In destructor of the demo.\n”;
}
};
int demo::h;
int main()
{
demo obj;
//
// We can access the static members using the class name or an object
//
cout<<demo::h; //10
cout<<obj.h; //10
return 0;
}
/* Next Example*/
class demo
{
public:
int i,j;
static int k;
demo()
{
i=99;
printf(“In constructor of the demo.\n”);
}
~demo()
{
printf(“In destructor of the demo.\n”);
}
static void fun()
{
//
//Error because static member functions can not
//access non static members
//
printf(“%d “,i);
//
// Static member function can access the static member
//
printf(“%d “,k);
}
};
int demo::k=90;
int main()
{
demo d;
demo::fun();
return;
/* Next Example*/
class demo
{
public:
int i,j;
static int k;
demo()
{
i=99;
cout<<” In constructor of the demo.\n”;
}
~demo()
{
cout<<” In destructor of the demo.\n”;
}
static void fun()
{
cout<<k;
}
void fun1()
{
printf(“%d”,k); //non static function can use the static members
}
};
int demo::k=90;
int main()
{
demo d;
demo v;
d.fun1(); //90
d.fun(); //90
cout<<d.k; //90
cout<<demo::k; //90
v.k++;
//
// This updated value of static variable which is shared by all objects
//
cout<<d.k; //91
cout<<v.k; //91
return 0;
}
/* Next Example*/
class demo
{
public:
int i,j;
static int k;
demo()
{
i=99;
cout<<” In constructor of the demo.\n”;
}
~demo()
{
cout<<” In destructor of the demo.\n”;
}
};
int demo::k=90;
int main()
{
demo d;
//
// Memory is allocated for non static members only
//
cout<<sizeof(d); //8
return 0;
}
/* Next Example*/
class demo
{
public:
int i,j;
static const int k;
demo()
{
i=99;
cout<<” In constructor of the demo.\n”;
}
~demo()
{
cout<<” In destructor of the demo.\n”;
}
};
//
// Constant member must be initialized
//
const int demo::k;
//it should be const int demo::k=30;
int main()
{
demo d;
return 0;
}
/* Next Example*/
class demo
{
public:
int i,j;
static const int k;
//
// Error we can not make constructor static
//
static demo()
{
i=99;
cout<<” In constructor of the demo.\n”;
}
//
// Error we cannot make destructor static
//
static ~demo()
{
cout<<” In destructor of the demo.\n”;
}
};
const int demo::k=90;
int main()
{
demo d;
return 0;
}
For more reading about technology news in singapore and seo to online marketing do view more about other pages.