C++ supports a variety of data types and you can select the appropriate types as per the need of the application. C++ supports following data types :
- Fundamental data types
- Derived data types
- User defined data types
Basic or fundamental data type : Basic data types are built in data types and can be classified as follows :
Integer data type: An integer is a whole number without a decimal point. A integer requires 2 bytes of memory space. The range of interest data type is -32768 to +32767.
C++ has following into data types :
Short int, unsigned short int, signed short int, unsigned int, signed int, long int, int, unsigned long int, signed int.
The int variable can be declared as follows :
Char data type: The keyword “char” is short for “character” data type in C++. The char variables are used to store ASCII characters, whose integer value lies in range of -128 to +127.
The char variable can be declared as char Ch= ‘X’; where ch is character variable and X is a character content.
Float data type: The float data type is used to represent real or floating point values. They generally require 4 bytes of memory for their storage. The floating point variables can be declared as follows:
Where X is a floating point variable. Float values can store values with 7 precision.
Void data type: The void specifies an empty set of values. This is used in following situations.
- To specify the return type of a function which does not return any values.
- To indicate empty argument list of a function.
- To declare generic pointers.
Derived Data type: The various data types are:
Arrays: An Array is a collection of related data items with similar data types and having a common name. They are used to represent a group of related data items. Elements of an array is accessed by its index number.
Pointers: Pointer is a variable that contains the address of another variable in memory. The address is the location of the variable.
Function: A function is self contained block of statements that perform some specific task. A function is a group of program statements into a unit and gives it a name. These can also be called sub programs.
User defined data types: The various user defined data type are as follows:
Class: A class is a group of objects with same attributes and common behavious. Objects of same type constitute a class e.g. Scooter, Motor cycle, Car etc. Are all the members of same class Vehicle. Objects are variables of type class. Once a class is defined, you can create number of objects.
Structure: A structure is a method of grouping data of different data types. A structure may contain integer elements, characters elements and floating point elements. These elements are called members. Structure is declared in C++ as follows:
Union: Union is similar to structure. A structure contains members of different data types and each member has its own memory location, where as the members with in a union all share the same storage area. The Union is used to conserve memory. Union is declared in C++ as follows: