C and C++

Program to illustrate a static data member & create 3 object & count the no. of object using static data member count

C++
#include<iostream.h>

#include<conio.h> 

class item{

    ststic int count;

    int number;

    public:

    void getdata(){

        cin>>number;

        count++;

    }

    void getcount(void)

    {

        cout<<"count:"<<count<<endl;

    }

};

int item::count;

//-------Main Program---

int main(){

    item a,b,c;

    clrscr();

    cout<<"----------\n";

    cout<<"Before reading data\n";

    a.getcount();

    b.getcount();

    c.getcount();

    cout<<"----------------------------------\n";

    cout<<"Enter value to object a\n";

    a.getdata();

    cout<<"Enter value to object b\n";

    b.getdata();

    cout<<"Enter value to object b\n";

    c.getdata();

    cout<<"----------------------------------\n";

    cout<<"After reading data\n";

    a.getcount();

    b.getcount();

    c.getcount();

    cout<"\n--------------------------------------\n":

    getch();

    return 0;

}

[wp_ad_camp_1]

OUTPUT 

----------------------

Before reading data

count:0

count:0

count:0

---------------------------

Enter value to object a

20

Enter value to object b

30

Enter value to object c

40

---------------------------

After reading data

count:3

count:3

count:3

-----------------------

Leave a comment

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