Wednesday, February 24, 2010

Sample Code for Static Data Member

An Example from our class lecture

// static data

#include<iostream.h>

#include<conio.h>


 

class foo

{

private:

static int t;

int count;

public:


 

    foo()

     {

     t++;

     count = 0;

     }

    int get_cnt()

     {

     return t;

     }

     int get_count()

     {

     return count;

     }

};


 

int foo:: t;


 

void main()

{


 

clrscr();

// cout << "No object of foo created " << foo::t;


 

foo f1, f2, f3;


 

cout << "\nStatic Count is " << f1.get_cnt();

cout << "\nstatic Count is " << f2.get_cnt();

cout << "\nstatic Count is " << f3.get_cnt();


 

cout << "\nCount is " << f1.get_count();

cout << "\nCount is " << f2.get_count();

cout << "\nCount is " << f3.get_count();


 

/* foo f4, f5;


 

cout << "\nCount is " << f4.get_count();

cout << "\nCount is " << f5.get_count();

*/ getch();

}

No comments:

Post a Comment