static-keyword-in-C++ .

In this session i have discussed static keyword in C++ .


The data members of a class can be qualified as static . A static member has following special characterstics .

1-It is initialized to zero when object of the class is created . There is no any other initialzation permitted .

2-There is only one copy is created for entire class and shared by all object of that class .

3-It is visible in class . It its life time is entire program .


Static variable :

The static variable value common to all the object of the class .

Example : Write a program that demonstrate the static variable .

    #include<iostream>
    using namespace std;  
    class Employee
    {
        static int count;
        int empid;
        string empname;
        public :
                void getdata()
                {
                    cout<<"\nEnter the Employee ID : ";
                    cin>>empid;
                    cout<<"\nEnter the Employee Name : ";
                    cin>>empname;
                    count++;
                }
                void Total()
                {
                    cout<<"\nTotal No of Employee : "<<count;
                }
     };
     int Employee::count; //define the static count
     int main()
     {
        Employee obj1,obj2,obj3;
        obj1.Total(); 
        obj1.getdata();
        obj1.Total();
        //-------------------------------------------
        obj2.getdata();
        obj2.Total();
        //-----------------------------------------
        obj3.getdata();
        obj3.Total();
        return 0;
     }
  OUTPUT :  Total No of Employee : 0
            Enter the Employee ID : 1
            Enter the Employee Name : Rakesh
            Total No of Employee : 1
            Enter the Employee ID : 2
            Enter the Employee Name : Ramesh
            Total No of Employee : 2
            Enter the Employee ID : 3
            Enter the Employee Name : Chanchal
            Total No of Employee : 3
    

In the above program i have declare the count as static variable . The value of count increment 1 inside the getdata() when the getdata() is called . In main() i have created three object of Employee class . After created object the value of count is initialized to 0 and i have called the Total() that will display the count value as 0 . After calling the getdata() it will increment 1 value of count and again called the Total() that will display the count value as 1 . Next i have called the getdata() it will increment the value of count as 1 that means the value of count is 2 . Next i have called the Total that will display the count value as 2 and so on .


Static Function :

A member function as qualified as static . It is called static memeber function . The static function has following properties .

1-A static function can not have any non static members . That means it always contains static memebers not any non static member .

2-A static member function can not be called using object . The way of calling static function as follows .

class-name :: function-name

Example : Write a program that demonstrate the use of static function .

    #include<iostream>
    using namespace std;  
    class A
    {
        int ticketno;
        static int count;
        public :
                void get_ticket()
                {
                    ticketno=++count;
                }  
                void show_ticket()
                {
                    cout<<"\nTicket No : "<<ticketno;
                }
                static void show_count()
                {
                    cout<<"\nTotal No of Ticket : "<<count;
                }
     
    };
    int A :: count;//define the static count
    int main()
    {
        A obj1 ,obj2;
        obj1.get_ticket();
        obj1.show_ticket();
        obj2.get_ticket();
        obj2.show_ticket();
        A :: show_count();//called the static show_count()

    }
  OUTPUT :  Ticket No  : 1
            Ticket No  : 2
            Total No of Ticket  : 2
  

Welcome to AndroidATPoint.

back image                     next image