Monday, October 3, 2016

STATIC AND NON-STATIC VARIABLES:

Tags

Program to Show Difference between static and non-static member variables.

#include <iostream>
using namespace std;
class sample
{
    static int c; //static variable
    int x; //non-static variable

public:
    void reset()
    {
        x=0;
    }

    void count()
    {
        ++c;
        ++x;
        cout<<"\n\nStatic Variable:\n";
        cout<<"Value of c="<<c;
        cout<<"\nAddress of c="<<(unsigned)&c; //address of c
        cout<<"\n\nNon-Static Variable:\n";
        cout<<"Value of x="<<x;
        cout<<"\nAddress of x="<<(unsigned)&x; //address of x
    }
};
int sample::c=0; //initialising static variable

int main()
{
    sample s1,s2,s3;
    s1.reset();
    s2.reset();
    s3.reset();
    s1.count();
    s2.count();
    s3.count();
    return 0;
}