Static is a keyword in C++ used to give special characteristics to an element. Static elements are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime.

Static variables when used inside function are initialized only once, and then they hold there value even through function calls.

Static data members of class are those members which are shared by all the objects. Static data member has a single piece of storage, and is not available as separate copy with each object, like other non-static data members.

C++ program to implement static variable and function

#include<iostream.h>
#include<conio.h>
 
class stat
{
    int code;
    static int count;
 
   public:
    stat()
    {
      code=++count;
    }
    void showcode()
    {
      cout<<"\n\tObject number is :"<<code;
    }
    static void showcount()
    {
              cout<<"\n\tCount Objects :"<<count;
    }
};

int stat::count;
 
void main()
{
   clrscr();
   stat obj1,obj2;
 
   obj1.showcount();
   obj1.showcode();
   obj2.showcount();
   obj2.showcode();
   getch();
}

Check out – All CSE Basic Practicals

Thanks for visiting us. Do share us among your friends.

C++ program to implement Static Variable and Function – Computer Practical
Tagged on:     

One thought on “C++ program to implement Static Variable and Function – Computer Practical

  • August 4, 2016 at 8:49 pm
    Permalink

    Interesante información, voy a leer mas al respecto.

    Reply

Leave a Reply to Shanna Halstrom Cancel reply

Your email address will not be published. Required fields are marked *