You are on page 1of 2

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

Static Member Functions

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);  

MOVIE TICKET RESERVATION PROGRAM

#include<iostream>
using namespace std;
class Box {

static int ta;


int ntb;
public:
// Constructor definition
Box(int t)
{
cout<<"Constructor called." << endl;
ntb=t;

}
void bookticket()
{

cout<<"Total number of tickets avilable="<<ta<<endl;


ta=ta-ntb;
cout<<"Total number of tickets booked="<<ntb<<endl;
cout<<"available tickets after booking"<<ta<<endl;
}

static void check()


{
if(ta<=0)
{
cout<<"housefull";
}
else
{
cout<<"tickets available";
}

};

// Initialize static member of class Box


int Box::ta = 10;

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;
}

You might also like