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
• type 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.

18.Static Data Members and Static member function.pptx

  • 1.
    Static Data Membersand Static member function
  • 2.
    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.
  • 3.
    Definition of staticdata members • static data_type data_member_name; // declaration • type 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.
  • 4.
    • #include<iostream> • usingnamespace 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;
  • 5.
    • cout<<"after readingdata"<<" 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);
  • 6.
    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.