C++: Basic Data Types

Data types in c++ can be categorized in three categories:
1) User-Defined type
2) Built-in type
3) Derived type
These Data Type can be further categorized.
User-Defined type:
a) Structure
b) Union
c) Class
d) Enumeration
Built-in type:
a)Integral type
          i) int
          ii) char
b)Void
c)Floating type
          i) Float
          ii) Double
Derived type:
a) Array
b) Function
c) Pointer
d) Reference
The type void was introduced in ANSI C.
Interesting use of void is in the declaration of generic pointers.
A Generic Pointer can be assigned a pointer value of any basic data type,but it may not be dereferenced.
    
       int *ip; //int pointer
       gp= ip; //assign int pointer to void
                                  pointer
        *ip=*gp; // illegal
 
It would not make sense to dereference a pointer to a void value.
Enumerated Data type: It is a user defined data type which is used to attach the number to the names.It is used only when we know all possible values (a variable can take) in advance.
eg.     enum operation{off,on};
By default ,the numbering starts from 0, therefore 0 is assigned to OFF and 1 is assigned to ON.
         const int OFF=0;
         const int ON=1;
is same as the above example.
Other eg.
         enum colour{red=5,yellow,green=2};
Pointers:
           int *ip; // int pointer
           ip = &x; //address of x assigned to
                               ip
           *ip=10 //10 assigned to x indirectly
C++ adds the concept of constant pointer and pointer to a constant.
    char * const ptr1="Geek"; //constant
                                                       pointer
We cannot modify the address that ptr1 is initialized to.
   int const * ptr2=&m; //pointer to a
                                             constant
ptr2 can point to any variable of correct type, but the contents of what it points to cannot be changed.
  const char * const cp="Code";
This statement declares cp as a constant pointer to the string which has been declared a constant.Here neither the address assigned to the pointer cp nor the contents it points to can be changed.
Remaining topics will be discussed later.Thanks for reading this.Share with your friends.
 
                   < Happy coding >
Reference:
OOP by BALAGURUSWAMY
In Association with Amazon.in

Comments

Popular Posts

Top 10 Web Sites Every Programmer Should Visit Daily

C++: Huffman Coding using STL

What is "namespace" and why do we use it?