Data types are means to identify the type of data and associated operations
of handling it. C++ provides a predefined set of data types for handling the
data it uses. When variables are declared of a particular data type then the
variable becomes the place where the data is stored and data types is the type
of value(data) stored by that variable.
Data can be of may types such as character, integer, real etc. since the data
to be dealt with are of may types, a programming language must provide different
data types.
In C++ data types are of two types:-
- Fundamental Data Types: As the name suggests these are
the atomic or the fundamental data types in C++. Earlier there was five of
these (int, char, float, double and void) but later two new data types namely
bool and wchar_t have been added. Int stores integer data or whole numbers
such as 10, -340 etc., char stores any of the ASCII characters, float is used
to store numbers having fractional part such as 10.097, double stores the
same data as float but with higher range and precision, bool can only store
true and false values.//Program to illustrate various fundamental data type
#include<iostream.h>
void main(void)
{
int age;
float salary;
char code;
cout<<"Enter age, salary and code:";
cin>>age;
cin>>salary;
cin>>code;
cout<<endl;//goto next line
cout<<"DETAILS"<<endl;//short for cout<<"DETAILS";cout<<endl;
cout<<"Age:"<<age<<endl;
cout<<"Salary:"<<salary<<endl; cout<<"Code:"<<code<<endl;
}
- Derived Data Types: These are the data types which are
derived from the fundamental data types. It is further divide into two categories
i)Built-In and ii)User-defined, which are discussed below as seperate topics.
Built-In Derived Data Type
- Arrays: Arrays refer to a list of finite number of same
data types. The data in the array can be accessed by an index number ranging
from 0 to n(where n is the number of data element it can store). Ex- if arr[3]
is an array of int(egers) then the different values in the array can be accessed
as shown below.
arr[0], arr[1],arr[2]
when we declare an array such as the one sown above then by arr[3] we mean
that we want three elements in the array and hence while accessing arr[2]
is the last element.//Program to illustrate arrays
#include<iostream.h>
void main(void)
{
int arr[3];//it will store 3 integer elements
cout<<"enter 3 numbers:";
cin>>arr[0]>>arr[1]>>arr[2];//this statement is same as using three cin's
cout<<endl;//goto next line
cout<<arr[0]<<arr[1]<<arr[2];
} - Pointer: A pointer is a variable that holds the memory
address of other variable. It is also of different data types, ex- char pointer
can store address of only char variables, int pointer can store address of
int variables and so on. - Reference: A reference in the simplest sense is an alias
or alternate name for a previously defined variable.
//Program to illustrate References
#include<iostream.h>
void main(void)
{
int var;
int &refvar=var;//here a reference variable to var is declared remember var was previously declared
var=10;//var is given the value 10
cout<<var<<endl;
refvar=100;//reference variable of var is changed
cout<<var;//but var also gets changed
}
User-Defined Derived Data Types
- Class: A class is a collection of variables and function
under one reference name. it is the way of separating and storing similar
data together. Member functions are often the means of accessing, modifying
and operating the data members (i.e. variables). It is one of the most important
features of C++ since OOP is usually implemented through the use of classes. - Structure: In C++ structure and class same except for
some very minor differences. - Union: A union is a memory location shared by two or more
different variables, generally of different data types. Giving more details
here would only confuse you; I’ll leave it for future articles. - Enumerations: It can be used to assign names to integer
constants.
//Program to illustrate Enumerators
#include<iostream.h>
void main(void)
{
enum type{POOR,GOOD,EXCELLENT};//this is the syntax of enumerator
int var;
var=POOR;//this makes programs more understandable
cout<<var<<endl;
var=GOOD;
cout<<var<<endl;
var=EXCELLENT;
cout<<var;
}
Data Types Modifiers
- signed
- unsigned
- short
- long
Int, char, float, double data types can be preceded with these modifiers to
alter the meaning of the base type to fit various situations properly.
Every data type has a limit of the larges and smallest value that it can store
known as the range. An integer (usually 4 bytes long) can store any value ranging
from -2147483648 to 2147483647. Data Type modifiers usually alter the upper
and lower limit of a data type.
Unsigned modifier makes a variable only to store positive values. Ex- if a data
type has a range from –a to a then unsigned variable of that type has
a range from 0 to 2a. Preceding any data type with signed is optional because
every data type is signed by default. Short integers are 2 bytes long and long
integers are 8 bytes long.
Related Articles: