Networking, Programming and Graphics Tutorials

Static data members - C++ [1/1]

Type:
Static data members - C++
Level: Intermediate Networking, Programming and Graphics Tutorials Networking, Programming and Graphics Tutorials 
Static data members - C++
Date: 2006-Dec-05
Static data members - C++
Visited: 1122 times
Static data members - C++
Rating: Static data members - C++
Static data members - C++
Author: Alex Plumpton

The code shown in the figure, illustrates the use of static data members. In the code, the static variable id is initialized to zero when the objects are created. The id is incremented whenever the data is read into an object. Since, the data is read into the object thrice, the variable id is incremented three times.

Example:

#include <conlo.h>
#include<iostream.h>

class student
{
static int id;
static int mark;
public:
void getdata(int m)
{
mark=m;
id++;
}
void dispdata()
{
cout«"student id"<<" "<<id«endl;
}
};
int student:: id;
int student:: mark;
void main()
{
clrscr();
student s1,s2,s3;
si dispdata():
s2.dispdala():
s3.dispdata():
sl.getdata(78).
s2.getdata(89).
s3.getdata(98).
cout«"Atter getting the data"«endl;
si dispdata():
s2.dispdata():
s3.dispdaia():
getch();
}

The code shown in the figure, illustrates the usage of static member functions. The static function dispid() displays the objects created thus far. The static variable id maintains the count of number of objects created.

Example:

#include <conlo.h>
#nclude<iostream.h>

class stud
{
int i;
static int id;

public:
void setdata()
{
i=++id;
}
void dispi()
{
cout<< "The value of i:"<<" "<<i«endl;
}
static void dispid(void)
{
cout« "The value of id:"«" "<<id«endl:
}
};


int stud:: id;
void main()
{ clrscr();
stud id1.id2;
idl.setdata();
id2.setdata();
stud:: dispid();
stud id3;
stud:: dispid();
id3.setdata();
idl.dispi();
id2.dispi();
id3.dispi();
getch():
}

Rate this tutorial:                    
Networking, Programming and Graphics Tutorials - Static data members - C++ [1/1] - Networking, Programming and Graphics Tutorials

Need a specific tutorial? Do not hesitate and submit a request!
Related Tags: data members in c++  pointer-to-members c++  What are pointer-to-members in C++?  C++ pointer-to-members  static polymorphism c++  polymorphism static c++  Static Polymorphism+C++  Static polymorphism c++  static+ polymorphism + C++  excluding members from copy constructor  data functions in C++  what is data abstraction in c++   example of data and functions in C++  c++ data casting  data abstraction-c++  c++ data ranges  update data using php  data abstraction c++  definition data abstraction c++  update data using mysql php