User-Defined Data Types
Structure
General form of structure definition is as follow:
struct name
{
data type member1;
data type member2;
…
…
};
Structure important points are:
1. A structure is defined with struct keyword.
2. All the members of structure can be manipulated simultenously.
3. The size of a structure object is equal to the sum of the individual sizes of the member objects.
4. Stucture members are allocated distinct location.
5. Structure are not consider as memory efficient as compare to union.
Union
General form of union definition is as follow:
union name
{
data type member1;
data type member2;
…
…
};
Union important points are:
1. A union is defined with ‘union’ keyword.
2. The members of union can be manipulated only one at a time.
3. The size of a union object is equal to the size of largest member objects.
4. Union members share common memory space for the exclusive usage.
5. Union is memory efficient as compare to structure.
Derived Data Types
Array
The application of an array is similar to that in C.Only
char str[3] = “xyz”;
is valid in C. It assumes that the programmer intends to leave out the null character ‘�’ in the definition.But in C++, the size should be one larger than the number of characters in the string.
char str[4] = “xyz”; // Ok for C++.
Function
Function have undergone major changes in C++.these Changes were introduced to make the C++ program more reliable and readable.
All the features of C++ functions are discussed in later sections.
Pointer
Constant pointer:
We cannot modify the address that ptr1 is initialized to
char * const ptr1 = “GOOD”;
Pointer to constant:
ptr2 is declared as pointer to a constant.It can point to any variable of correct type, but the contents of what it points to cannot changed.
int const * ptr2 = &m;
Storage Class in C++
To fully define a variable one needs to mention not only its ‘type’ but also its ‘storage class’.Not only all variables have a data types, they also have a ‘storage class’.
Variable name identifies some physical location within the computer where the string of bits representing the variable stored value.There are basically two kinds of locations in a computer : Memory and CPU register.It is the variable’s storage class which determines in which of these two location the value is stored.
A variable storage class tells us:
[a] Where the variable would be stored.
[b] What will be the initial value of the variable(i.e. the default initial value)
[c] What is the scope of the variable i.e. in which function the value of the variable would be available
[d] what is the life of the variable i.e. how long variable exist
There are four storage classes in C++ :
1. Automatic Storage Class
2. Register Storage Class
3. Static storage class
4. External storage class
lets examine one by one
Automatic Storage Class
Default initial value: |
Garbage value |
Scope |
Local to the block in which the variable is defined. |
Life |
Till the control remains within the block in which the variable is defined. |
Storage |
Stack Segement |
Keyword |
auto |
A simple program to demonstrate automatic storage class:
/* If the variable is not initialized then it contain garbage value */
main()
{
auto int i,j;
printf(“%d %d”,i,j);
}
Output
1211 221
where, 1211 and 221 are garbage value of i and j.When you run this program you may get different values, since garbage values could be absolutely any values.So always make it a point that you initialize the automatic variables properly,otherwise you are likely to get unexpected results.
Note that the keyword for this storage class is auto and not automatic.
Register Storage Class
Default initial value: |
An unpredictable value, which is often called a garbage value. |
Scope |
Local to the block in which the variable is defined. |
Life |
Till the control remains within the block in which the variable is defined. |
Storage |
CPU registers |
Keyword |
Register |
A simple program to demonstrate register storage class:
main()
{
register int i;
for(i=1; 10 >= i; i++)
printf(“%d”,i);
}
A value stored in a CPU register can always be accessed faster than the one which is stored in memory.Therefore, if a variable is used at many places in a program it is better to declare its storage class as register.
A good example of frequently used variables are loop counter.
Static Storage Class
Default initial value: |
Zero |
Scope |
Local to the block in which the variable is defined. |
Life |
Value of the variable persist between different function calls |
Storage |
Memory |
Keyword |
static |
Difference between the automatic and static storage classes:
/* Example of automatic storage class */
main()
{
increment();
increment();
increment();
}
void increment()
{
auto int i = 1;
printf(“%d”,i);
i++;
}
Output
1 1 1
/* Example of static storage class */
main()
{
increment();
increment();
increment();
}
void increment()
{
static int i = 1;
printf(“%d”,i);
i++;
}
Output
1 2 3
The program above consist of two functions main() and increment().The function increment() gets called from main() thrice.Each time it increment the value of i and print it.The only difference between the two program is that one uses an auto storage class for variable i, whereas the other uses static storage class.
External Storage Class
Default initial value: |
Zero |
Scope |
Global |
Life |
As long as the program execution doesn’t come to an end. |
Storage |
Data Segment |
Keyword |
extern |
External variables differ from those we have already discussed in that their scope is global, not local.External variable are declared outside all functions, yet are available to all functions that care to use them.