Static data members and static member function, C/C++ Programming

Static Data Members:

A data member inside the class can construct as static data member.   There are few

guidelines to be followed when declaring static data member.

  • It is declared as a private data member.
  • Only one copy of the static data member is created any numbers of objects are created from that class. Therefore the static data member will be same for all the objects created.
  • The static member must be defined outside the class like function with class name.
  • The static member can be initialized in the definition and can be used only within its class.

 

The following program will give clear difference between static data count and regular data number members.  The static data is common to all objects but regular data is unique to its object.

 

class item

{int number; static int count; public:

void getdata()

{++count;

number=count;

}

void putdata(void)

{cout<<"Count is "<

cout<<"Number is "<

}

};

int item::count=0;

int main()

 

 

{           clrscr();

item x,y,z; //Three object produced from class item x.getdata();

y.getdata();

z.getdata();

x.putdata(); The count is 3 and number is 1

y.putdata(); The count is 3 and number is 2 z.putdata(); The count is 3 and number is 3

return 0;

}

 

Static Member Function:

The static member function can be specified like static data member.  It works asc a static member data except the static member data is used in static member function.

Rules of Static Member function are given below:

  • The static member function can read static data member only from its class.
  • The static member function is called using class name not object name. class_name::static_function_name( ); It can also be called using object.static_fun( ).

 

The given program will illustrate how the static member function is worked.

 class find

{static int count;

int code;

public:

static void showcount(void)

{cout<<"Count is " <

}

void setcode(void)

{code = ++count;

}

void setcount(void)

{ cout<<"Code is " <

}

};

int find ::count=0;

int main()

{clrscr();

find x,y,z;

x.setcode(); // Code and count is 1. y.setcode(); //Code and count is 2 find::showcount(); //Count is 2

 

z.setcode(); //Code and count is 3 find::showcount(); //Count is 3

 

x.setcount(); // Code is 1 for object x.

 

 

 

 

y.setcount(); // Code is 2 for object y. z.setcount(); // Code is 3 for object z.

 

x.setcode(); // Code and count is 4. y.setcode(); //Code and count is 5 z.setcode(); //Code and count is 6 find::showcount(); //Count is 6

//The value of code will increase from its previous value of that object.

x.setcount(); // Code is 4 for object x. y.setcount(); / Code is 5 for object x. z.setcount(); // Code is 6 for object x.

return 0;

}

 

Since the count is declared as static it has only one copy irrespective of number of objects created.   When static count function called again and again it will increase count for all the objects created simultaneously therefore the count will be uniform for all object, but for the data member code it is unique for each object created because it is not a static data member.  Therfore from the above example there will only one value for count data member irrespective of number of objects constructed since it is a static data member, where as for code data member the value will be different for each object.

Posted Date: 9/20/2012 7:56:45 AM | Location : United States







Related Discussions:- Static data members and static member function, Assignment Help, Ask Question on Static data members and static member function, Get Answer, Expert's Help, Static data members and static member function Discussions

Write discussion on Static data members and static member function
Your posts are moderated
Related Questions
Flash my terminal vx670 Project Description: Have VeriFone software need it to communicate with the vx670 through com port 1 it is not talking I'm not sure why have all the r

how do i write a c++ program that will input a number and output its factorial using the while loop

how to write code for z transformer


A: 1.      By using const protects you against programming errors which inadvertently alter data. 2.      By using const allows function to procedure const and non-const actu

A parking garage charges a $2.00 minimum fee to park for up to three hours and an additional $0.50 per hour for each hour or part thereof over three hours. The maximum charge for a

Constructors By definition, a constructor function of some class is a member function that automatically gets implemented whenever an instance of the class to which the constru

C Program to check FACTORIAL   main() {             int i,j,m,fact,sum=0;           clrscr();           printf("ENTER THE NO.: ");           scanf("%d",&m);

find area uder the curve y=f(x) between x=a and x=b   #include float start_point, /* GLOBAL VARIABLES */ end_point, total_area; in

Templates permit to create generic functions that admit any data type as parameters and return value without having to overload the function with all the possible data types. Until