Thursday, February 4, 2021

Program to Count the number of objects using the Static member function in c++

Design a class having static member function name show count() which has the property of displaying the number of objects creator of the class.


#include<iostream.h>
#ionclude<conio.h>
class test 
{
  int code;
  static int count;
  public:
  void set code(void)
  {
     code=++count;
  }
  void showcode(void)
  {
    count<<"object number:"<<code<<"\n";
  }
  static void show count(void)
  {
     cout<<"count"<<count<<"\n";
  }
};
int test ::count;
void main()
{
   test t1,t2;
   t1.setcode();
   t2.setcode();
   test t3;
   t3.setcode();
   test::showcount();
   t1.showcode();
   t2.showcode();
   t3.showcode();
   getch();
}

Output :


count = 3;
object1 = 1;
object2 = 2;
object3 = 3;

No comments:

Post a Comment