18.Static Data Members and Static member function.pptx

sanketkashyap2023 12 views 6 slides Feb 10, 2025
Slide 1
Slide 1 of 6
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6

About This Presentation

cpp


Slide Content

Static Data Members and Static member function

Static Data members Static member variable are similar to that of C static variable. Characteristic of static variable: It is initialized to zero when the first object of the class is created. Only one copy of that member is created for the entire class and it is shared by all the objects of the class. It is visible only within the class, but its lifetime is the entire program. Static variables are normally used to maintain values common to the entire class.

Definition of static data members static data_type data_member_name ; // declaration t ype class name : : static datamember ; // definition Eg : int item :: count; Static member variable must be defined outside the class definition. They are also known as class variables.

#include< iostream > using namespace std ;   class item { static int count; int number; public: void getdata ( int a) { number=a; count++; } void getcount (void) { cout <<count<<"\n"; } }; int item::count;

cout <<"after reading data"<<"\n"; a.getcount (); b.getcount (); c.getcount (); return 0; } int main() { item a,b,c ; a.getcount (); b.getcount (); c.getcount (); a.getdata (100); b.getdata (200); c.getdata (300);

Static Member functions Static member function can have access to other static members( functions or variables) declared in the same class. Static member function can also be called using the class name Class-name :: function-name;  Just like a static variable once declared is allocated with memory that can’t be changed every object points to the same memory.
Tags