Wednesday 12 October 2011


IMPLEMENTATION OF STATIC MEMBERS

Aim:
To write a C++ program to demonstrate the static members

Algorithm:
Step 1: Start the program.
Step 2: Create a class with static data members and static function.
Step 3: The static value is initialized to zero during object creation. The static value is incremented when a member function set is called for every object.
Step 4: We can also reinitialize the static member value during the static member definition.
Step 5: The result is displayed.
Step 6: Stop the process.

Program:

#include<iostream.h>
#include<conio.h>
class staticmember
{
 int a;
 static int count,cnt;
 public:
       void set()
       {
        a=++count;
        cnt++;
       }
       void show()
       {
            cout<<"\n";
            cout<<"Object Number:"<<a<<"\n";
       }
       static void showcount()
       {
         cout<<"Static count value:"<<count<<"\n";
         cout<<"Static cnt value:"<<cnt<<"\n";
       }
};
int staticmember::count;
int staticmember::cnt = 100;
void main()
{
  staticmember s1,s2,s3;
  clrscr();
  s1.set();
  s1.show();
  cout<<endl;
  s2.set();
  staticmember::showcount();
  s2.show();
  cout<<endl;
  s3.set();
  staticmember::showcount();
  s3.show();
  cout<<endl;
  getch();
}

Output:

Object Number:1

Static count value:2
Static cnt value:102

Object Number:2

Static count value:3
Static cnt value:103

Object Number:3

Result:
Thus the C++ program was executed and the output is verified.

0 comments:

Post a Comment