A data type determines the type and the operations that can be performed on the data. C++ provides various data types and each data type is represented differently within the computer’s memory. The various data types provided by C++ are built-in data types, derived data types and user-defined data types as shown in Figure.
We’ll be covering the following topics in this tutorial:
Built-In Data Types
The basic (fundamental) data types provided by c++ are integral, floating point and void data type. Among these data types, the integral and floating-point data types can be preceded by several type modifiers. These modifiers (also known as type qualifiers) are the keywords that alter either size or range or both of the data types. The various modifiers are short, long, signed and unsigned. By default the modifier is signed.
In addition to these basic data types, ANSI C++ has introduced two more data types namely, bool and wchar_t.
Integral Data Type: The integral data type is used to store integers and includes char (character) and int (integer) data types.
Char: Characters refer to the alphabet, numbers and other characters (such as {, @, #, etc.) defined in the ASCII character set. In C++, the char data type is also treated as an integer data type as the characters are internally stored as integers that range in value from -128 to 127. The char data type occupies 1 byte of memory (that is, it holds only one character at a time).
The modifiers that can precede char are signed and unsigned. The various character data types with their size and range are listed in Table
Int: Numbers without the fractional part represent integer data. In C++, the int data type is used to store integers such as 4, 42, 5233, -32, -745. Thus, it cannot store numbers such as 4.28, -62.533. The various integer data types with their size and range are listed in Table
Floating-point Data Type: A floating-point data type is used to store real numbers such as 3 .28, 64. 755765, 8.01, -24.53. This data type includes float and double’ data types. The various floating -point data types with their size and range are listed in Table
Void: The void data type is used for specifying an empty parameter list to a function and return type for a function. When void is used to specify an empty parameter list, it indicates that a function does not take any arguments and when it is used as a return type for a function, it indicates that a function does not return any value. For void, no memory is allocated and hence, it cannot store anything. As a result, void cannot be used to declare simple variables, however, it can be used to declare generic pointers.
Bool and wcha_t : The boo1data type can hold only Boolean values, that is; either true or false, where true represents 1 and false represents O. It requires only one bit of storage, however, it is stored as an integer in the memory. Thus, it is also considered as an integral data type. The bool data type is most commonly used for expressing the results of logical operations performed on the data. It is also used as a return type of a function indicating the success or the failure of the function.
In addition to char data type, C++ provides another data type wchar_t which is used to store 16- bit wide characters. Wide characters are used to hold large character sets associated with some non-English languages.
Derived Data Types: Data types that are derived from the built-in data types are known as derived data types. The various derived data types provided by C++ are arrays, junctions, references and pointers.
Array An array is a set of elements of the same data type that are referred to by the same name. All the elements in an array are stored at contiguous (one after another) memory locations and each element is accessed by a unique index or subscript value. The subscript value indicates the position of an element in an array.
Function A function is a self-contained program segment that carries out a specific well-defined task. In C++, every program contains one or more functions which can be invoked from other parts of a program, if required.
Reference A reference is an alternative name for a variable. That is, a reference is an alias for a variable in a program. A variable and its reference can be used interchangeably in a program as both refer to the same memory location. Hence, changes made to any of them (say, a variable) are reflected in the other (on a reference).
Pointer A pointer is a variable that can store the memory address of another variable. Pointers allow to use the memory dynamically. That is, with the help of pointers, memory can be allocated or de-allocated to the variables at run-time, thus, making a program more efficient.
User-Defined Data Types
Various user-defined data types provided by C++ are structures, unions, enumerations and classes.
Structure, Union andClass: Structure and union are the significant features of C language. Structure and union provide a way to group similar or dissimilar data types referred to by a single name. However, C++ has extended the concept of structure and union by incorporating some new features in these data types to support object -oriented programming.
C++ offers a new user-defined data type known as class, which forms the basis of object-oriented programming. A class acts as a template which defines the data and functions that are included in an object of a class. Classes are declared using the keyword class. Once a class has been declared, its object can be easily created.
Enumeration: An enumeration is a set of named integer constants that specify all the permissible values that can be assigned to enumeration variables. These set of permissible values are known as enumerators. For example, consider this statement.
enum country {US, UN, India, China}; // declaring an
// enum type
In this statement, an enumeration data-type country (country is a tag name) , consisting of enumerators US, UN and so on, is declared. Note that these enumerators represent integer values, so any arithmetic operation can be performed on them.
By default, the first enumerator in the enumeration data type is assigned the value zero. The value of subsequent enumerators is one greater than the value of previous enumerator. Hence, the value of US is 0, value of UN is 1 and so on. However, these default integer values can be overridden by assigning values explicitly to the enumerators
as shown here.
enum country {US, UN=3, India, china} ;
In this declaration, the value of US is O by default, the value of UN is 3, India is 4 and soon.
Once an enum type is declared, its variables can be declared using this statement.
country countryl, country2;
These variables countryl, country2 can be assigned any of the values specified in enum declaration only. For example, consider these statements.
countryl India; // valid
country2 Japan; // invalid
Though the enumerations are treated as integers internally in C++, the compiler issues a warning, if an int value is assigned to an enum type. For example, consider these statements.
Country1 = 3; //warning
Country1 = UN; / /valid
Country1 = (country) 3; / /valid
C++ also allows creating special type of enums known as anonymous enums, that is, enums without using tag name as shown in this statement.
enum {US, UN=3, India, China};
The enumerators of an anonymous enum can be used directly in the program as shown here.
int count = US;
The typedef Keyword
C++ provides a typedef feature that allows to define new data type names for existing data types that may be built-in, derived or user-defined data types. Once the new name has been defined, variables can be declared using this new name. For example, consider this declaration.
typedef int integer;
In this declaration, a new name integer is given to the data type into This new name now can be used to declare integer variables as shown here.
integer i, j, k;
Note that the typedef is used in a program to contribute to the development of a clearer program. Moreover, it also helps in making machine-dependent programs more portable.