Chapter: Programming and Data structures : Object Oriented Programming Fundamentals

Static Members

The Static Data Member of a class is like a global variable. Once it is defined the static member will be initialized to zero. When the static member is declared inside the class it should be defined outside of the class.

STATIC MEMBERS:

 

The Static Data Member of a class is like a global variable. Once it is defined the static member will be initialized to zero. When the static member is declared inside the class it should be defined outside of the class. Static Data Member of a class will be common to all the objects which are declared in the class.

 

Syntax:

 

class stat

 

{

 

static int count; //static Data Member Declaration

 

}

 

int stat::count; //static Data member Defintion

 

 

Example Program:

 

#include<iostream.h>

 

#include<conio.h>

class count

{

 

public:

 

static int countt;

void dispcount()

{

 

countt++;

 

cout<<"Object\t"<<countt<<"\n";

 

}

 

};

 

int count::countt;

void main()

{

 

count c1,c2,c3,c4,c5;

clrscr();

c1.dispcount();

c2.dispcount();

c3.dispcount();

c4.dispcount();

 

c5.dispcount();

 

getch();

 

}

 

 

In this example class count has a static data member countt.This program is used for counting the number of objects which is declared in the class.So when an object c1 access the function dispcount() the static variable has the value 1.when s5 access the function the value will be incremented to 5.

 

 

Output:

 

Object 1

 

Object 2

 

Object 3

 

Object 4

 

Object 5

 

 

NOTE:

 

Once the static data member is defined it is automatically initialized to zero.

 

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Programming and Data structures : Object Oriented Programming Fundamentals : Static Members |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.