Professional Documents
Culture Documents
Static Datamembers and Member Functions
Static Datamembers and Member Functions
STATIC DATAMEMBERS
Static data members are class members that are declared using static keywords. A static member
has certain special characteristics. These are:
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 to zero when the first object of its class is created. No other initialization is
permitted
It is visible only within the class, but its lifetime is the entire program
The static member functions are special functions used to access the static data members or other
static member functions. A member function is defined using the static keyword. A static
member function shares the single copy of the member function to any number of the class'
objects. We can access the static member function using the class name or class' objects. If the
static member function accesses any non-static data member or non-static member function, it
throws an error.
Syntax
1. class_name::function_name (parameter);
#include<iostream>
using namespace std;
class Box {
}
void bookticket()
{
};
int main(void) {
// Print total number of objects before creating object.
//cout << "Inital Stage Count: " << getremainingCount() << endl;
Box::check();
Box Box1(4); // Declare box1
Box1.bookticket();
Box Box2(3); // Declare box2
Box2.bookticket();
Box Box3(3); // Declare box2
Box2.bookticket();
return 0;
}