Definition:
Data type means of identifying the type of data and associated operations of handling it. C++ provide various data type is represented differently within the computer's memory.You may like to store information of various data types like character, wide character, integer, floating point, double floating point, boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
Types of data type uses in C++
User defined data type:
There are some data type that are defined by user, are known as user defined data type. There are four types of use defined data type.
Structure:
A structure is a collection of different type of data element. It attempts to bring a user-defined types as close as possible to the built-in data type and also provides a facility to hide the data which is one of the main feature of object oriented programming.
In C++ structure can have both variable and function as members. It can also declare some of its members as 'private' so that they cannot be accessed directly by the external functions. Structure names are stand alone and can be used like any other type names. The keyword "struct" can be omitted in declaration of structure variable. By default, members of structure are public.
e.g., struct stud
{
int marks;
float members;
char name[15];
};
Union:
A union is same like that of structure but the little difference is that the members of union shares common memory space. Union is declared with the help of "union" keyword.
e.g., union emp
{
int emp;
char name[20];
float salary;
};
Class:
A class is a collection of data and functions in a single unit. The functions are called members of class and the data is called data members of a class. A class represents a group of similar objects. Class is a way to bind data and functions in a single unit. Class are usually grouped under two sections, one is named, private and other is named as public.
The class members that have been declared as private can be accessed only from within the class. On the other hand, public members can be accessed by the outside the class also. The data hiding ( using private declaration ) is the key feature of object oriented programming. By default, members of class are private.
syntax:
class class_name
{
private:
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
};
Enumeration:
Defining own data type and specify what values a variable of this type can take is called enumeration. An enumerated data type can be declared using 'enum' keyword. They providing way for attaching names to numbers, thereby increasing comprehensibility of the code.
Built-in data type:
Basic data type that are not composed of other data type. There are three types of built-in data type.
Integral:
There are two types of integral data type.
Integer data type:
Integer data type refers to that data which are integer such as 1,2,3,0,-1,-2,-3 etc. They have no fractional part. Integer is declared with the help of "int" data type.
e.g.,
int sum;
int x, y;
e.g.,
int sum;
int x, y;
Character data type:
An identifier declared as char becomes a character variable. It is denoted by enclosing the character in single quotes.
e.g.,
char x, y;
e.g.,
char x, y;
Void data type:
The data type void actually refers to an object that does not have a value of any type. void data type indicate the function that does not return its value. This is an empty data type which does not return value when it is called by calling function.
e.g.,
void main()
Floating type:
Floating type consist two types of data.
Float data type:
A variable declared as float becomes a floating point variable and can hold only floating point numbers. A number having function. A number having fractional part is a floating point number. It contain size of 4 bytes.
e.g.,
float average;
float x, y;
Double data type:
The data type double is also used for handling floating point numbers. It is treated as distint data type because it occupies twice as much memory as type float and store floating point numbers with much large range. It contains 8 bytes of data.
e.g.,
double average;
double x, y;
Derived data type:
The data type are derived from the built-in data type are known as derived data type. There are four types of derived data type.
Array:
Array is a collection of homogeneous type of data elements. The data type of element may be int, float, or char etc. Array may be one dimensional, two dimensional and multi-dimensional.
e.g.,
int count[10];
float matrix[2][3];
char name[10];
Function:
Function is a small program to do some specific task. The function can be called any number of times. Function is also called self contained program. Function eliminates the repeated code in the program. Once a program code is written there is no need to write the code again and again.
For e.g.,
int square(int a)
{
int b;
b=a*a;
return(b);
}
Pointer:
A pointer is a variable that contain the address of another variable. The address refers to the location of another variable in the memory. It simply uses the identifier whenever it needs to refer to the variable.
e.g.,
int i=10;
int *j;
j=&i;
References:
A reference is a alternative name for a variable. That is, a reference is an alias for a variable in a program.
Once a reference is created, it cannot be later made to reference another object; it cannot be reset. This is often done with pointers. References cannot be NULL. Pointers are often made NULL to indicate that they are not pointing to any valid thing. A reference must be initialized when declared. There is no such restriction with pointers
Due to the above limitations, references in C++ cannot be used for implementing data structures like Linked List, Tree, etc.
No comments:
Post a Comment