C++ program – Classes with static member functions

0
663

C++ program – Classes with static member functions

CONSTRUCTION:

  1. Start the program.
  2. Include the header files.
  3. Create class test with data member objnum,static data member count, member function as setobjnum(),showcount().
  4. In main function create object t1,t2,t3 for class.
  5. Access member functions using class.
  6. Print objnum and count values.
  7. Stop the program.

PROGRAM:

 #include<iostream.h>
#include<conio.h>
class test
{
int objnum;
static int count;
public:
void setobjnum()
{
objnum=++count;
}
void showobjnum()
{
cout<<"OBJECT NUM="<<objnum<<"\n";
}
static void showcount()
{
cout<<"COUNT:"<<count<<"\n";
}
};
int test::count;
void main()
{
clrscr();
test t1,t2,t3;
t1.setobjnum();
t2.setobjnum();
t3.setobjnum();
t1.showobjnum();
test::showcount();
t2.showobjnum();
test::showcount();
t3.showobjnum();
test::showcount();
getch();
}

 

Leave a Reply