Page 88 - DCAP404 _Object Oriented Programming
P. 88
Unit 4: Classes and Objects
Notes
Example: Consider the class account as follows:
class account
{
private:
int acc _no;
static int balance; //static data declaration
public:
void disp(int acc _no);
void getinfo( );
};
The static variable balance is initialized outside main() as follows:
int account::balance = 0; //static data definition
Consider the following Program which demonstrates the use of static data member count. The
variable count is declared static in the class but initialized to 0 outside the class.
#include <iostream.h>
#include <conio.h>
class counter
{
private:
static int count;
public:
void disp();
};
int counter::count = 0;
void counter::disp()
{
count++
cout << “The present value of count is “ << count << “\n”;
}
main()
{
counter cntl ;
for(int i=0; i<5; i++)
cnt1.disp();
getche();
LOVELY PROFESSIONAL UNIVERSITY 81