Monday, December 17, 2018

Static Member Function

CODE:


#include<iostream>

using namespace std;


class test
 {
    int code;
      static int count;               //static member variable
   public:
      void setcode()
      {
      code=++count;
  }
  
  void showcode()
  {
  cout<<"object member: "<<code<<endl;
  }
  
  static void showcount()           //static member function
  {
  cout<<"count= "<<count<<endl;
  }
};

int test::count;

int main()
{
test t1,t2;

t1.setcode();
t2.setcode();

test::showcount();                 //acccessing static function

test t3;
t3.setcode();
test::showcount();

t1.showcode();
t2.showcode();
t3.showcode();

return 0;
}



OUTPUT:




Static Data Member

CODE:

#include<iostream>

using namespace std;

class item
 {
      static int count;
      int number;
      int a;
   public:
      void getdata()
      {
        cout<<"enter number to count: "<<endl;
        cin>>a;
          number=a;
          count++;
      }
      void getcount()
      {
          cout<<"count: "<<count<<endl;
      }
 };
 int item :: count;           //definition of static data member

 int main()
 {
    item a,b,c;             //count is initialized to zero
    a.getcount();           //display count
    b.getcount();
    c.getcount();

    a.getdata();         //getting data into object a
    b.getdata();         //getting data into object b
    c.getdata();         //getting data into object c

    cout<<"After reading data"<<endl;

    a.getcount();             //display count
    b.getcount();
    c.getcount();
    
    return 0;
 }



OUTPUT:






















Static Member Function

CODE : #include<iostream> using namespace std; class test  {     int code;       static int count;               //static...