Monday, 10 June 2013

Data Types

Basic Data Types

Both C and C++ compilers support all the built in basic data types. With the exception of void, the basic data types may have several modifiers preceding them to serve the needs of various situations. The modifiers signed, unsigned long and short may be applied to character and integer basic data type.
However the modifier long may also be applied to double. 

Here is a list of all the combinations of basic data types with their size and range for a 16 bit machine.


User Defined Data Types

If you have used C you all must have used user defined datatypes such as struct and union in C. While these data types are legal in C++, some more features have been added to make them suitable for object oriented programming. C++ also permits us to define another user defined data type known as class which can be used just like any other basic data type, to declare variables. The class variables are known as objects, which are of central importance in OOP.

Enumerated Data Type

An enumerated data type is another user defined data type which provides a way for attaching names to numbers, thereby increasing the comprehensibility of the code. The enum keyword automatically enumerates a list of words by assigning them values 0,1,2 and so on. This facility provides an alternative means for creating symbolic constants. The syntax of an enum statement is similar to that of the struct statement.

Derived Data Type

  • Arrays :- The application of arrays in C++ is similar to that in C. And they only exception is that the size of the array should be one larger than the number of characters in the string.
  • Functions :- Functions are a lot different in C++ as compared to C. While some of these changes are simple while others require a new of thinking when organising our programs. Many of these modifications and improvements were driven by the requirements of the Object oriented concepts of C++ . We will discuss them in details later.
  • Pointers :- Pointers are declared and initialised as in C. Examples :- int *ip; ip= &x; *ip=10. C++ adds the concept of constant pointer and pointer to a constant. 
          char * const ptr1 = "GOOD";           //constant pointer
         we  cannot modify the address that ptr1 is initialized to 
         int  const * ptr2 = &m;  //pointer to a constant.

Symbolic Constants

There are two ways of creating symbolic constants in C++
  • Using the qualifier const, and
  • Defining a set of integer constants using enum keyword.
In C++ as in C , any value declared as const cannot be modified by the program in anyway. However there are some differences in implementation. In C++ , we can use const in a constant expression such as :

const int size = 10;
char name [size];
this would be illegal in C. 

Declaration of Variables

We know that in C all variables must be declared before they are used in executable statements. This is true in case of C++ as well. However there is a significant difference between C and C++ with regard to the place of their declaration in the program. C requires all the variables to be defined at the beginning of a scope. When we read a C program, we usually come across a group of variable declaration at the beginning of each scope level. Their actual use appears elsewhere in the scope, sometimes far away from the place of declaration. Before using a variable, we should go back to the beginning of the program to see whether it has been declared and if so, of what type.

C++ allows the declaration of a variable anywhere in the scope. This means that a variable can be declared right at the place of its first use. This makes the program much easier to write and reduces the errors that maybe caused because the variable are declared in the context of their use. The only disadvantage of this style of declaration is that we cannot see all the variables used in the program at a glance.

Thats all for today friends get back to you tomorrow with lots more :)