C++ Memory allocation for objects
we have stated that the memory space for object is allocated when they are declared and not when the class is specified .
This statement is only partly true. Basically the member functions are created and placed in the memory space only once when they are defined as a part of a class specification. since all the objects belonging to that class uses the same member functions, whenever the objects are created no separate space is allocated for member functions . only space for
member variables is allocated separately for each object. separate memory locations for the objects are essential because the member variable will hold different data values for different object.
This is show below fig
STATIC DATA MEMBERS
A data members of a class can be qualified as static. The properties of a static member variable are similar to that of a C language static variable. A static member variable has certain special
characteristics These are
1. it is initialize to 0(zero) when first object of its class is created. No other initialization is permitted.
2. Only single copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.
3. it’s visible only within the class, but its lifetime is the entire program
Static variable are normally used to maintain the common values to the entire class. lets see for example a static data member can be used as a counter that records the occurrences of all the objects.
in the below program show static data member
<iostream>
using namespace std;
class items
{
count;
number;
:
getdata(int a)
{
number=a;
count++;
}
getcount()
{
std::cout<<"count is"<<count <<"\n";
}
};
items:: count;
main()
{
items x,y,z;
x.getcount();
y.getcount();
z.getcount();
x.getdata(100);
y.getdata(200);
z.getdata(300);
cout << "After reading data" << "\n";
x.getcount();
y.getcount();
z.getcount();
0;
}
|
The output will be
count is 0
count is 0
count is 0
After reading data
count is 3
count is 3
count is 3
|
Notice The following statement in the program
Note that The type and scope of each static member variable must be defined outside the
class definition. This is necessary because the static data members are stored separately rather than as a part of an object. Since they are associated with the class itself rather than with any class object, they are also known as class variables.
The static variable count is initialized to zero when the object are created. The count is
incremented whenever the data is read into an object. since the data is read into objects three times,
the variable count is incremented three times. because there is only one copy of count shared by all the three objects,
all the three output statements cause the value 3 to be displayed
in the below fig show
static variables are like non line member functions as they are declared
in a class declaration and defined in the source file. while defining a static variable, some initial value can also be assigned to the variable.
for instance the following definition gives count the initial value 10