A type for a variable in C++ does three main things. First of all it is used to determine the actual amount of memory for a variable. The second thing it does is to determine the operations that are supported for the variable. The third thing it does is to determine how data is transformed from other types to the type of the variable and whether such a transformation is possible.
So an int
which is a built in type has a certain size that an int
variable occupies in memory. For a number of historical, backwards compatibility reasons the size of an int
will vary between different kinds of hardware. On a 16 bit CPU it may be 16 bits, on a 32 bit CPU it may be 32 bits, and on a 64 bit CPU it may be 64 bits. However it is always signed. If you want to specify the size no matter what the hardware you need to use one of the more specific types such as short
or long
or long long
.
An int
variable also has certain operators and operations that can be used with the variable such as the plus operator (+) for addition and the multiplication operator (*) for multiplication. For an int
variable these operators are defined to provide specific behaviors just as for a string
variable they have different behaviors, e.g. the plus operator does string concatenation.
Finally an int
variable has certain transformations that the compiler will generate for you. If you assign a float
to an int
variable then the compiler will transform the value of the float
into an int
by truncating any decimal portion of the value. So a floating point value of 3.456 will be truncated to 3 and a floating point value of 0.123 will be truncated to 0.
And the compiler will not allow some types of transformations because those transformations are not defined for an int
. If you define a class which does not have a method for transforming the class to an int
and then try to assign a variable of that type to an int
the compiler will generate an error message.
The word "type" in C++ or C or most languages that use static type variables is a kind of variable classification used by the compiler to help programmers avoid mistakes and to help the compiler allocate memory more efficiently and to generate more efficient machine code.
A type is a way to classify variables. Languages such as JavaScript do not use static type but rather dynamic type. In JavaScript or php the type of a variable will depend on the value last assigned to the variable and the languages do a lot of conversions for you automatically.
In C++, as in C and Java, there are built in types and there are programmer defined types. Variable types such as int
or char
are built in types also known as Plain Old Data as these types have direct hardware representations.
A programmer can create defined types in C++ in a number of ways. The most common is by using the class
keyword or the struct
keyword to define a class which can then be used as a type when defining variables or functions.
With the new standards, C++11 and later, there is a new keyword auto
which can be used when defining a variable. This is a way to declare a variable's type by using an expression on the right hand side of the assignment operator to generate a value with a particular type and assign both the value and the type to variable being defined. Once this is done, the variable's type can not be changed however using auto
can be really helpful.
class
. Skipping that step is an exercise in futility.class
,struct
andunion
, and uses the keywordclass
in situations beyond declaring and defining classes.Object
" as there is in Java or C#. Even two user-defined classes are unrelated in that there is no common base type, unlike Java or C#. There is no attempt to make primitive types likeint
fit into a common class hierarchy, no automatic boxing and unboxing.